Code with Go
Ask or search…

Command-Line Subcommands

This Go program demonstrates the use of subcommands with different sets of flags using the flag package. It allows you to run the program with either a "foo" or "bar" subcommand, each having its own set of flags. Let's break down the code with inline comments:
package main
import (
func main() {
// Define flags for the 'foo' subcommand
fooCmd := flag.NewFlagSet("foo", flag.ExitOnError)
fooEnable := fooCmd.Bool("enable", false, "enable")
fooName := fooCmd.String("name", "", "name")
// Define flags for the 'bar' subcommand
barCmd := flag.NewFlagSet("bar", flag.ExitOnError)
barLevel := barCmd.Int("level", 0, "level")
// Check if there are enough arguments
if len(os.Args) < 2 {
fmt.Println("expected 'foo' or 'bar' subcommands")
// Determine the subcommand and parse its specific flags
switch os.Args[1] {
case "foo":
fmt.Println("subcommand 'foo'")
fmt.Println(" enable:", *fooEnable)
fmt.Println(" name:", *fooName)
fmt.Println(" tail:", fooCmd.Args())
case "bar":
fmt.Println("subcommand 'bar'")
fmt.Println(" level:", *barLevel)
fmt.Println(" tail:", barCmd.Args())
fmt.Println("expected 'foo' or 'bar' subcommands")
  1. 1.
    Defining Flags for Subcommands:
    • The program defines two sets of flags using flag.NewFlagSet for the 'foo' and 'bar' subcommands.
  2. 2.
    Parsing Subcommands:
    • The program checks the number of command-line arguments. If there are not enough arguments, it prints an error message and exits.
  3. 3.
    Switching Between Subcommands:
    • The program uses a switch statement to determine whether the first argument is "foo" or "bar."
  4. 4.
    Parsing Subcommand-Specific Flags:
    • Depending on the subcommand, it parses the specific set of flags for that subcommand using fooCmd.Parse or barCmd.Parse.
  5. 5.
    Printing Results:
    • After parsing the subcommand-specific flags, the program prints the results.
You can run the program with commands like:
go run main.go foo -enable -name John arg1 arg2
go run main.go bar -level 5 arg1 arg2
This allows you to have different flags for different subcommands.