Add function: usage

This commit is contained in:
Reza Behzadan 2024-02-15 14:31:50 +03:30
parent 61878fd4d1
commit 5253d3c121

18
main.go
View File

@ -16,6 +16,12 @@ type Args struct {
Help bool
}
// Custom usage function to display help
func usage() {
fmt.Println("Usage: mycli [options]")
flag.PrintDefaults()
}
// ParseArgs parses the command-line arguments into a Args struct
func ParseArgs() Args {
var versionShortFlag bool
@ -25,11 +31,11 @@ func ParseArgs() Args {
var helpShortFlag bool
var helpLongFlag bool
flag.BoolVar(&versionShortFlag, "v", false, "Print version information")
flag.BoolVar(&versionShortFlag, "v", false, "")
flag.BoolVar(&versionLongFlag, "version", false, "Print version information")
flag.BoolVar(&debugShortFlag, "d", false, "Enable debug mode")
flag.BoolVar(&debugShortFlag, "d", false, "")
flag.BoolVar(&debugLongFlag, "debug", false, "Enable debug mode")
flag.BoolVar(&helpShortFlag, "h", false, "Show help")
flag.BoolVar(&helpShortFlag, "h", false, "")
flag.BoolVar(&helpLongFlag, "help", false, "Show help")
flag.Parse()
@ -46,6 +52,7 @@ func init() {
func main() {
args := ParseArgs()
flag.Usage = usage
// Handle version flag
if args.Version {
@ -55,9 +62,8 @@ func main() {
// Handle help
if args.Help {
fmt.Println("Usage: mycli [options]")
flag.PrintDefaults()
os.Exit(0)
flag.Usage()
return
}
fmt.Println("Hello, World!")