You can do that using a combination of command line arguments and flags.
Eg.
configFilePtr := flag.String("filename", "", "Config Filename")
flag.Parse()
n := len(os.Args)
if n > 1 {
switch os.Args[1] {
case "init":
initApp(*configFilePtr)
case "check":
checkApp(*configFilePtr)
case "run":
runApp()
}
}
Another option is using something like spf13's cobra
.
Update :
If you require the use of different flags as per command you can use a FlagSet as mentioned in Kare Nuorteva
's answer.
Eg.
f1 := flag.NewFlagSet("f1", flag.ContinueOnError)
silent := f1.Bool("silent", false, "")
f2 := flag.NewFlagSet("f2", flag.ContinueOnError)
loud := f2.Bool("loud", false, "")
switch os.Args[1] {
case "apply":
if err := f1.Parse(os.Args[2:]); err == nil {
fmt.Println("apply", *silent)
}
case "reset":
if err := f2.Parse(os.Args[2:]); err == nil {
fmt.Println("reset", *loud)
}
}
Reference