> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/71-command-line-subcommands.md).

# 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:

```go
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:

```bash
go run main.go foo -enable -name John arg1 arg2
```

or

```bash
go run main.go bar -level 5 arg1 arg2
```

This allows you to have different flags for different subcommands.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/71-command-line-subcommands.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
