> 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/70-command-line-flags.md).

# Command-Line Flags

This Go program showcases the usage of the `flag` package to parse command-line arguments. Let's go through the code with inline comments:

```go
package main

import (
	"flag"
	"fmt"
)

func main() {
	// Define flags using the flag package
	wordPtr := flag.String("word", "foo", "a string")
	numbPtr := flag.Int("numb", 42, "an int")
	forkPtr := flag.Bool("fork", false, "a bool")

	// You can also define a flag and bind it to an existing variable using flag.StringVar
	var svar string
	flag.StringVar(&svar, "svar", "bar", "a string var")

	// Parse the command-line arguments
	flag.Parse()

	// Print the values of the flags and other non-flag arguments
	fmt.Println("word:", *wordPtr)
	fmt.Println("numb:", *numbPtr)
	fmt.Println("fork:", *forkPtr)
	fmt.Println("svar:", svar)
	fmt.Println("tail:", flag.Args())
}
```

Explanation:

1. **Defining Flags:**
   * The program defines four flags using the `flag` package: `wordPtr`, `numbPtr`, `forkPtr`, and `svar`.
2. **Setting Default Values:**
   * Default values are specified for each flag using the `String`, `Int`, and `Bool` functions.
3. **Binding to Existing Variables:**
   * The program demonstrates how to bind a flag to an existing variable using `flag.StringVar`.
4. **Parsing Flags:**
   * `flag.Parse()` is called to parse the command-line arguments.
5. **Printing Values:**
   * The program prints the values of the flags and any non-flag arguments (referred to as "tail").

When you run this program with various command-line arguments, the values of the flags and the remaining arguments will be printed. For example:

```bash
go run main.go -word=hello -numb=7 -fork -svar=world arg1 arg2
```

Output:

```
word: hello
numb: 7
fork: true
svar: world
tail: [arg1 arg2]
```

You can experiment with different command-line arguments and see how the values of the flags change.


---

# 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/70-command-line-flags.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.
