This is how you would pass variable FlagSet variables and print defaults.

#go #GoLang

func PrintHelp(cmds ...*flag.FlagSet) {
	fmt.Println("Available commands :")
	for _, cmd := range cmds {
		fmt.Printf("\t%s\n", cmd.Name())
	}
	fmt.Println("====== * * * ======")

	for _, cmd := range cmds {
		fmt.Printf("%s\n", cmd.Name())
		cmd.PrintDefaults()
	}
}
func ProcessArgs() string {
        var what string
        var speed string

	// start option set
	startCmd := flag.NewFlagSet("start", flag.ContinueOnError)
	startCmd.StringVar(&what, "w", "anything", "start some object")
	startCmd.StringVar(&speed, "s", "max", "speed at which to start something")

	// stop option set
	stopCmd := flag.NewFlagSet("stop", flag.ContinueOnError)
	stopCmd.StringVar(&what, "w", "anything", "stop some object")

	if len(os.Args) < 2 {
		PrintHelp(startCmd, stopCmd)
		os.Exit(-1)
	}

	strOption := strings.ToLower(os.Args[1])

	switch strOption {
	case startCmd.Name():
		startCmd.Parse(os.Args[2:])

	case stopCmd.Name():
		stopCmd.Parse(os.Args[2:])

	default:
		PrintHelp(startCmd, stopCmd)
		os.Exit(-2)
	}

	return strOption
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s