Package [flag](<https://golang.org/pkg/flag/>) in standard library is for parsing cmd-line arguments:

https://codeeval.dev/gist/31b5364c35801027a2929b17e9155425

Output above is a result of calling go run $file -echo echo-arg additional arg.

Defining arguments

Let’s say your program has an integer -retries option.

You register such option with flag package using:

var flgRetries int
defaultRetries := 0
usage := "retries specifies number of times to retry the operation"
flag.IntVar(&flgRetries, "retries", defaultRetries, usage)

There are functions for other common types:

If you register int argument with name retries, the way to provide it on cmd-line is -retries ${value} or -retries=${value}.

POSIX variant --retries or Windows variant /retries are not recognized.

For boolean values you can say: -help (implicitly true), -help=true or -help=false.

-help false is not a valid form for boolean variables.

Parsing and accessing remaining arguments

After parsing arguments, call flag.Parse().

Parsing fails if: