package main import ( "flag" "fmt" "os" ) // Version of the program const VERSION = "1.0.0" // Config holds all command-line arguments type Args struct { Version bool Debug bool Help bool } // ParseArgs parses the command-line arguments into a Args struct func ParseArgs() Args { var versionShortFlag bool var versionLongFlag bool var debugShortFlag bool var debugLongFlag bool var helpShortFlag bool var helpLongFlag bool flag.BoolVar(&versionShortFlag, "v", false, "Print version information") flag.BoolVar(&versionLongFlag, "version", false, "Print version information") flag.BoolVar(&debugShortFlag, "d", false, "Enable debug mode") flag.BoolVar(&debugLongFlag, "debug", false, "Enable debug mode") flag.BoolVar(&helpShortFlag, "h", false, "Show help") flag.BoolVar(&helpLongFlag, "help", false, "Show help") flag.Parse() return Args{ Version: versionShortFlag || versionLongFlag, Debug: debugShortFlag || debugLongFlag, Help: helpShortFlag || helpLongFlag, } } // init is called before main. Use it to set up your app func init() { } func main() { args := ParseArgs() // Handle version flag if args.Version { fmt.Println(VERSION) os.Exit(0) } // Handle help if args.Help { fmt.Println("Usage: mycli [options]") flag.PrintDefaults() os.Exit(0) } fmt.Println("Hello, World!") if args.Debug { fmt.Println("Debug mode is on!") } }