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 (
	"flag"
	"fmt"
	"os"
)

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")
		os.Exit(1)
	}

	// Determine the subcommand and parse its specific flags
	switch os.Args[1] {
	case "foo":
		fooCmd.Parse(os.Args[2:])
		fmt.Println("subcommand 'foo'")
		fmt.Println("  enable:", *fooEnable)
		fmt.Println("  name:", *fooName)
		fmt.Println("  tail:", fooCmd.Args())
	case "bar":
		barCmd.Parse(os.Args[2:])
		fmt.Println("subcommand 'bar'")
		fmt.Println("  level:", *barLevel)
		fmt.Println("  tail:", barCmd.Args())
	default:
		fmt.Println("expected 'foo' or 'bar' subcommands")
		os.Exit(1)
	}
}

Explanation:

  1. Defining Flags for Subcommands:

    • The program defines two sets of flags using flag.NewFlagSet for the 'foo' and 'bar' subcommands.

  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. Switching Between Subcommands:

    • The program uses a switch statement to determine whether the first argument is "foo" or "bar."

  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. 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

or

go run main.go bar -level 5 arg1 arg2

This allows you to have different flags for different subcommands.

Last updated