summaryrefslogtreecommitdiff
path: root/devdocs/go/flag%2Findex.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/go/flag%2Findex.html
new repository
Diffstat (limited to 'devdocs/go/flag%2Findex.html')
-rw-r--r--devdocs/go/flag%2Findex.html452
1 files changed, 452 insertions, 0 deletions
diff --git a/devdocs/go/flag%2Findex.html b/devdocs/go/flag%2Findex.html
new file mode 100644
index 00000000..e8f9a4df
--- /dev/null
+++ b/devdocs/go/flag%2Findex.html
@@ -0,0 +1,452 @@
+<h1> Package flag </h1> <ul id="short-nav">
+<li><code>import "flag"</code></li>
+<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
+<li><a href="#pkg-index" class="indexLink">Index</a></li>
+<li><a href="#pkg-examples" class="examplesLink">Examples</a></li>
+</ul> <h2 id="pkg-overview">Overview </h2> <p>Package flag implements command-line flag parsing. </p>
+<h3 id="hdr-Usage">Usage</h3> <p>Define flags using <a href="#String">flag.String</a>, <a href="#Bool">Bool</a>, <a href="#Int">Int</a>, etc. </p>
+<p>This declares an integer flag, -n, stored in the pointer nFlag, with type *int: </p>
+<pre data-language="go">import "flag"
+var nFlag = flag.Int("n", 1234, "help message for flag n")
+</pre> <p>If you like, you can bind the flag to a variable using the Var() functions. </p>
+<pre data-language="go">var flagvar int
+func init() {
+ flag.IntVar(&amp;flagvar, "flagname", 1234, "help message for flagname")
+}
+</pre> <p>Or you can create custom flags that satisfy the Value interface (with pointer receivers) and couple them to flag parsing by </p>
+<pre data-language="go">flag.Var(&amp;flagVal, "name", "help message for flagname")
+</pre> <p>For such flags, the default value is just the initial value of the variable. </p>
+<p>After all flags are defined, call </p>
+<pre data-language="go">flag.Parse()
+</pre> <p>to parse the command line into the defined flags. </p>
+<p>Flags may then be used directly. If you're using the flags themselves, they are all pointers; if you bind to variables, they're values. </p>
+<pre data-language="go">fmt.Println("ip has value ", *ip)
+fmt.Println("flagvar has value ", flagvar)
+</pre> <p>After parsing, the arguments following the flags are available as the slice <a href="#Args">flag.Args</a> or individually as <a href="#Arg">flag.Arg</a>(i). The arguments are indexed from 0 through <a href="#NArg">flag.NArg</a>-1. </p>
+<h3 id="hdr-Command_line_flag_syntax">Command line flag syntax</h3> <p>The following forms are permitted: </p>
+<pre data-language="go">-flag
+--flag // double dashes are also permitted
+-flag=x
+-flag x // non-boolean flags only
+</pre> <p>One or two dashes may be used; they are equivalent. The last form is not permitted for boolean flags because the meaning of the command </p>
+<pre data-language="go">cmd -x *
+</pre> <p>where * is a Unix shell wildcard, will change if there is a file called 0, false, etc. You must use the -flag=false form to turn off a boolean flag. </p>
+<p>Flag parsing stops just before the first non-flag argument ("-" is a non-flag argument) or after the terminator "--". </p>
+<p>Integer flags accept 1234, 0664, 0x1234 and may be negative. Boolean flags may be: </p>
+<pre data-language="go">1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
+</pre> <p>Duration flags accept any input valid for time.ParseDuration. </p>
+<p>The default set of command-line flags is controlled by top-level functions. The <a href="#FlagSet">FlagSet</a> type allows one to define independent sets of flags, such as to implement subcommands in a command-line interface. The methods of <a href="#FlagSet">FlagSet</a> are analogous to the top-level functions for the command-line flag set. </p> <h4 id="example_"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">// These examples demonstrate more intricate uses of the flag package.
+package flag_test
+
+import (
+ "errors"
+ "flag"
+ "fmt"
+ "strings"
+ "time"
+)
+
+// Example 1: A single string flag called "species" with default value "gopher".
+var species = flag.String("species", "gopher", "the species we are studying")
+
+// Example 2: Two flags sharing a variable, so we can have a shorthand.
+// The order of initialization is undefined, so make sure both use the
+// same default value. They must be set up with an init function.
+var gopherType string
+
+func init() {
+ const (
+ defaultGopher = "pocket"
+ usage = "the variety of gopher"
+ )
+ flag.StringVar(&amp;gopherType, "gopher_type", defaultGopher, usage)
+ flag.StringVar(&amp;gopherType, "g", defaultGopher, usage+" (shorthand)")
+}
+
+// Example 3: A user-defined flag type, a slice of durations.
+type interval []time.Duration
+
+// String is the method to format the flag's value, part of the flag.Value interface.
+// The String method's output will be used in diagnostics.
+func (i *interval) String() string {
+ return fmt.Sprint(*i)
+}
+
+// Set is the method to set the flag value, part of the flag.Value interface.
+// Set's argument is a string to be parsed to set the flag.
+// It's a comma-separated list, so we split it.
+func (i *interval) Set(value string) error {
+ // If we wanted to allow the flag to be set multiple times,
+ // accumulating values, we would delete this if statement.
+ // That would permit usages such as
+ // -deltaT 10s -deltaT 15s
+ // and other combinations.
+ if len(*i) &gt; 0 {
+ return errors.New("interval flag already set")
+ }
+ for _, dt := range strings.Split(value, ",") {
+ duration, err := time.ParseDuration(dt)
+ if err != nil {
+ return err
+ }
+ *i = append(*i, duration)
+ }
+ return nil
+}
+
+// Define a flag to accumulate durations. Because it has a special type,
+// we need to use the Var function and therefore create the flag during
+// init.
+
+var intervalFlag interval
+
+func init() {
+ // Tie the command-line flag to the intervalFlag variable and
+ // set a usage message.
+ flag.Var(&amp;intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
+}
+
+func Example() {
+ // All the interesting pieces are with the variables declared above, but
+ // to enable the flag package to see the flags defined there, one must
+ // execute, typically at the start of main (not init!):
+ // flag.Parse()
+ // We don't call it here because this code is a function called "Example"
+ // that is part of the testing suite for the package, which has already
+ // parsed the flags. When viewed at pkg.go.dev, however, the function is
+ // renamed to "main" and it could be run as a standalone example.
+}
+</pre> <h2 id="pkg-index">Index </h2> <ul id="manual-nav">
+<li><a href="#pkg-variables">Variables</a></li>
+<li><a href="#Arg">func Arg(i int) string</a></li>
+<li><a href="#Args">func Args() []string</a></li>
+<li><a href="#Bool">func Bool(name string, value bool, usage string) *bool</a></li>
+<li><a href="#BoolFunc">func BoolFunc(name, usage string, fn func(string) error)</a></li>
+<li><a href="#BoolVar">func BoolVar(p *bool, name string, value bool, usage string)</a></li>
+<li><a href="#Duration">func Duration(name string, value time.Duration, usage string) *time.Duration</a></li>
+<li><a href="#DurationVar">func DurationVar(p *time.Duration, name string, value time.Duration, usage string)</a></li>
+<li><a href="#Float64">func Float64(name string, value float64, usage string) *float64</a></li>
+<li><a href="#Float64Var">func Float64Var(p *float64, name string, value float64, usage string)</a></li>
+<li><a href="#Func">func Func(name, usage string, fn func(string) error)</a></li>
+<li><a href="#Int">func Int(name string, value int, usage string) *int</a></li>
+<li><a href="#Int64">func Int64(name string, value int64, usage string) *int64</a></li>
+<li><a href="#Int64Var">func Int64Var(p *int64, name string, value int64, usage string)</a></li>
+<li><a href="#IntVar">func IntVar(p *int, name string, value int, usage string)</a></li>
+<li><a href="#NArg">func NArg() int</a></li>
+<li><a href="#NFlag">func NFlag() int</a></li>
+<li><a href="#Parse">func Parse()</a></li>
+<li><a href="#Parsed">func Parsed() bool</a></li>
+<li><a href="#PrintDefaults">func PrintDefaults()</a></li>
+<li><a href="#Set">func Set(name, value string) error</a></li>
+<li><a href="#String">func String(name string, value string, usage string) *string</a></li>
+<li><a href="#StringVar">func StringVar(p *string, name string, value string, usage string)</a></li>
+<li><a href="#TextVar">func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)</a></li>
+<li><a href="#Uint">func Uint(name string, value uint, usage string) *uint</a></li>
+<li><a href="#Uint64">func Uint64(name string, value uint64, usage string) *uint64</a></li>
+<li><a href="#Uint64Var">func Uint64Var(p *uint64, name string, value uint64, usage string)</a></li>
+<li><a href="#UintVar">func UintVar(p *uint, name string, value uint, usage string)</a></li>
+<li><a href="#UnquoteUsage">func UnquoteUsage(flag *Flag) (name string, usage string)</a></li>
+<li><a href="#Var">func Var(value Value, name string, usage string)</a></li>
+<li><a href="#Visit">func Visit(fn func(*Flag))</a></li>
+<li><a href="#VisitAll">func VisitAll(fn func(*Flag))</a></li>
+<li><a href="#ErrorHandling">type ErrorHandling</a></li>
+<li><a href="#Flag">type Flag</a></li>
+<li> <a href="#Lookup">func Lookup(name string) *Flag</a>
+</li>
+<li><a href="#FlagSet">type FlagSet</a></li>
+<li> <a href="#NewFlagSet">func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet</a>
+</li>
+<li> <a href="#FlagSet.Arg">func (f *FlagSet) Arg(i int) string</a>
+</li>
+<li> <a href="#FlagSet.Args">func (f *FlagSet) Args() []string</a>
+</li>
+<li> <a href="#FlagSet.Bool">func (f *FlagSet) Bool(name string, value bool, usage string) *bool</a>
+</li>
+<li> <a href="#FlagSet.BoolFunc">func (f *FlagSet) BoolFunc(name, usage string, fn func(string) error)</a>
+</li>
+<li> <a href="#FlagSet.BoolVar">func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)</a>
+</li>
+<li> <a href="#FlagSet.Duration">func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration</a>
+</li>
+<li> <a href="#FlagSet.DurationVar">func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)</a>
+</li>
+<li> <a href="#FlagSet.ErrorHandling">func (f *FlagSet) ErrorHandling() ErrorHandling</a>
+</li>
+<li> <a href="#FlagSet.Float64">func (f *FlagSet) Float64(name string, value float64, usage string) *float64</a>
+</li>
+<li> <a href="#FlagSet.Float64Var">func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string)</a>
+</li>
+<li> <a href="#FlagSet.Func">func (f *FlagSet) Func(name, usage string, fn func(string) error)</a>
+</li>
+<li> <a href="#FlagSet.Init">func (f *FlagSet) Init(name string, errorHandling ErrorHandling)</a>
+</li>
+<li> <a href="#FlagSet.Int">func (f *FlagSet) Int(name string, value int, usage string) *int</a>
+</li>
+<li> <a href="#FlagSet.Int64">func (f *FlagSet) Int64(name string, value int64, usage string) *int64</a>
+</li>
+<li> <a href="#FlagSet.Int64Var">func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string)</a>
+</li>
+<li> <a href="#FlagSet.IntVar">func (f *FlagSet) IntVar(p *int, name string, value int, usage string)</a>
+</li>
+<li> <a href="#FlagSet.Lookup">func (f *FlagSet) Lookup(name string) *Flag</a>
+</li>
+<li> <a href="#FlagSet.NArg">func (f *FlagSet) NArg() int</a>
+</li>
+<li> <a href="#FlagSet.NFlag">func (f *FlagSet) NFlag() int</a>
+</li>
+<li> <a href="#FlagSet.Name">func (f *FlagSet) Name() string</a>
+</li>
+<li> <a href="#FlagSet.Output">func (f *FlagSet) Output() io.Writer</a>
+</li>
+<li> <a href="#FlagSet.Parse">func (f *FlagSet) Parse(arguments []string) error</a>
+</li>
+<li> <a href="#FlagSet.Parsed">func (f *FlagSet) Parsed() bool</a>
+</li>
+<li> <a href="#FlagSet.PrintDefaults">func (f *FlagSet) PrintDefaults()</a>
+</li>
+<li> <a href="#FlagSet.Set">func (f *FlagSet) Set(name, value string) error</a>
+</li>
+<li> <a href="#FlagSet.SetOutput">func (f *FlagSet) SetOutput(output io.Writer)</a>
+</li>
+<li> <a href="#FlagSet.String">func (f *FlagSet) String(name string, value string, usage string) *string</a>
+</li>
+<li> <a href="#FlagSet.StringVar">func (f *FlagSet) StringVar(p *string, name string, value string, usage string)</a>
+</li>
+<li> <a href="#FlagSet.TextVar">func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)</a>
+</li>
+<li> <a href="#FlagSet.Uint">func (f *FlagSet) Uint(name string, value uint, usage string) *uint</a>
+</li>
+<li> <a href="#FlagSet.Uint64">func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64</a>
+</li>
+<li> <a href="#FlagSet.Uint64Var">func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)</a>
+</li>
+<li> <a href="#FlagSet.UintVar">func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string)</a>
+</li>
+<li> <a href="#FlagSet.Var">func (f *FlagSet) Var(value Value, name string, usage string)</a>
+</li>
+<li> <a href="#FlagSet.Visit">func (f *FlagSet) Visit(fn func(*Flag))</a>
+</li>
+<li> <a href="#FlagSet.VisitAll">func (f *FlagSet) VisitAll(fn func(*Flag))</a>
+</li>
+<li><a href="#Getter">type Getter</a></li>
+<li><a href="#Value">type Value</a></li>
+</ul> <div id="pkg-examples"> <h3>Examples</h3> <dl> <dd><a class="exampleLink" href="#example_">Package</a></dd> <dd><a class="exampleLink" href="#example_BoolFunc">BoolFunc</a></dd> <dd><a class="exampleLink" href="#example_Func">Func</a></dd> <dd><a class="exampleLink" href="#example_TextVar">TextVar</a></dd> <dd><a class="exampleLink" href="#example_Value">Value</a></dd> </dl> </div> <h3>Package files</h3> <p> <span>flag.go</span> </p> <h2 id="pkg-variables">Variables</h2> <p>CommandLine is the default set of command-line flags, parsed from <span>os.Args</span>. The top-level functions such as <a href="#BoolVar">BoolVar</a>, <a href="#Arg">Arg</a>, and so on are wrappers for the methods of CommandLine. </p>
+<pre data-language="go">var CommandLine = NewFlagSet(os.Args[0], ExitOnError)</pre> <p>ErrHelp is the error returned if the -help or -h flag is invoked but no such flag is defined. </p>
+<pre data-language="go">var ErrHelp = errors.New("flag: help requested")</pre> <p>Usage prints a usage message documenting all defined command-line flags to <a href="#CommandLine">CommandLine</a>'s output, which by default is <span>os.Stderr</span>. It is called when an error occurs while parsing flags. The function is a variable that may be changed to point to a custom function. By default it prints a simple header and calls <a href="#PrintDefaults">PrintDefaults</a>; for details about the format of the output and how to control it, see the documentation for <a href="#PrintDefaults">PrintDefaults</a>. Custom usage functions may choose to exit the program; by default exiting happens anyway as the command line's error handling strategy is set to <a href="#ExitOnError">ExitOnError</a>. </p>
+<pre data-language="go">var Usage = func() {
+ fmt.Fprintf(CommandLine.Output(), "Usage of %s:\n", os.Args[0])
+ PrintDefaults()
+}</pre> <h2 id="Arg">func <span>Arg</span> </h2> <pre data-language="go">func Arg(i int) string</pre> <p>Arg returns the i'th command-line argument. Arg(0) is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist. </p>
+<h2 id="Args">func <span>Args</span> </h2> <pre data-language="go">func Args() []string</pre> <p>Args returns the non-flag command-line arguments. </p>
+<h2 id="Bool">func <span>Bool</span> </h2> <pre data-language="go">func Bool(name string, value bool, usage string) *bool</pre> <p>Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag. </p>
+<h2 id="BoolFunc">func <span>BoolFunc</span> <span title="Added in Go 1.21">1.21</span> </h2> <pre data-language="go">func BoolFunc(name, usage string, fn func(string) error)</pre> <p>BoolFunc defines a flag with the specified name and usage string without requiring values. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error. </p> <h4 id="example_BoolFunc"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">fs := flag.NewFlagSet("ExampleBoolFunc", flag.ContinueOnError)
+fs.SetOutput(os.Stdout)
+
+fs.BoolFunc("log", "logs a dummy message", func(s string) error {
+ fmt.Println("dummy message:", s)
+ return nil
+})
+fs.Parse([]string{"-log"})
+fs.Parse([]string{"-log=0"})
+
+</pre> <p>Output:</p> <pre class="output" data-language="go">dummy message: true
+dummy message: 0
+</pre> <h2 id="BoolVar">func <span>BoolVar</span> </h2> <pre data-language="go">func BoolVar(p *bool, name string, value bool, usage string)</pre> <p>BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag. </p>
+<h2 id="Duration">func <span>Duration</span> </h2> <pre data-language="go">func Duration(name string, value time.Duration, usage string) *time.Duration</pre> <p>Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag. The flag accepts a value acceptable to time.ParseDuration. </p>
+<h2 id="DurationVar">func <span>DurationVar</span> </h2> <pre data-language="go">func DurationVar(p *time.Duration, name string, value time.Duration, usage string)</pre> <p>DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. </p>
+<h2 id="Float64">func <span>Float64</span> </h2> <pre data-language="go">func Float64(name string, value float64, usage string) *float64</pre> <p>Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag. </p>
+<h2 id="Float64Var">func <span>Float64Var</span> </h2> <pre data-language="go">func Float64Var(p *float64, name string, value float64, usage string)</pre> <p>Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag. </p>
+<h2 id="Func">func <span>Func</span> <span title="Added in Go 1.16">1.16</span> </h2> <pre data-language="go">func Func(name, usage string, fn func(string) error)</pre> <p>Func defines a flag with the specified name and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error. </p> <h4 id="example_Func"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">fs := flag.NewFlagSet("ExampleFunc", flag.ContinueOnError)
+fs.SetOutput(os.Stdout)
+var ip net.IP
+fs.Func("ip", "`IP address` to parse", func(s string) error {
+ ip = net.ParseIP(s)
+ if ip == nil {
+ return errors.New("could not parse IP")
+ }
+ return nil
+})
+fs.Parse([]string{"-ip", "127.0.0.1"})
+fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
+
+// 256 is not a valid IPv4 component
+fs.Parse([]string{"-ip", "256.0.0.1"})
+fmt.Printf("{ip: %v, loopback: %t}\n\n", ip, ip.IsLoopback())
+
+</pre> <p>Output:</p> <pre class="output" data-language="go">{ip: 127.0.0.1, loopback: true}
+
+invalid value "256.0.0.1" for flag -ip: could not parse IP
+Usage of ExampleFunc:
+ -ip IP address
+ IP address to parse
+{ip: &lt;nil&gt;, loopback: false}
+</pre> <h2 id="Int">func <span>Int</span> </h2> <pre data-language="go">func Int(name string, value int, usage string) *int</pre> <p>Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag. </p>
+<h2 id="Int64">func <span>Int64</span> </h2> <pre data-language="go">func Int64(name string, value int64, usage string) *int64</pre> <p>Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag. </p>
+<h2 id="Int64Var">func <span>Int64Var</span> </h2> <pre data-language="go">func Int64Var(p *int64, name string, value int64, usage string)</pre> <p>Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag. </p>
+<h2 id="IntVar">func <span>IntVar</span> </h2> <pre data-language="go">func IntVar(p *int, name string, value int, usage string)</pre> <p>IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag. </p>
+<h2 id="NArg">func <span>NArg</span> </h2> <pre data-language="go">func NArg() int</pre> <p>NArg is the number of arguments remaining after flags have been processed. </p>
+<h2 id="NFlag">func <span>NFlag</span> </h2> <pre data-language="go">func NFlag() int</pre> <p>NFlag returns the number of command-line flags that have been set. </p>
+<h2 id="Parse">func <span>Parse</span> </h2> <pre data-language="go">func Parse()</pre> <p>Parse parses the command-line flags from <span>os.Args</span>[1:]. Must be called after all flags are defined and before flags are accessed by the program. </p>
+<h2 id="Parsed">func <span>Parsed</span> </h2> <pre data-language="go">func Parsed() bool</pre> <p>Parsed reports whether the command-line flags have been parsed. </p>
+<h2 id="PrintDefaults">func <span>PrintDefaults</span> </h2> <pre data-language="go">func PrintDefaults()</pre> <p>PrintDefaults prints, to standard error unless configured otherwise, a usage message showing the default settings of all defined command-line flags. For an integer valued flag x, the default output has the form </p>
+<pre data-language="go">-x int
+ usage-message-for-x (default 7)
+</pre> <p>The usage message will appear on a separate line for anything but a bool flag with a one-byte name. For bool flags, the type is omitted and if the flag name is one byte the usage message appears on the same line. The parenthetical default is omitted if the default is the zero value for the type. The listed type, here int, can be changed by placing a back-quoted name in the flag's usage string; the first such item in the message is taken to be a parameter name to show in the message and the back quotes are stripped from the message when displayed. For instance, given </p>
+<pre data-language="go">flag.String("I", "", "search `directory` for include files")
+</pre> <p>the output will be </p>
+<pre data-language="go">-I directory
+ search directory for include files.
+</pre> <p>To change the destination for flag messages, call <a href="#CommandLine">CommandLine</a>.SetOutput. </p>
+<h2 id="Set">func <span>Set</span> </h2> <pre data-language="go">func Set(name, value string) error</pre> <p>Set sets the value of the named command-line flag. </p>
+<h2 id="String">func <span>String</span> </h2> <pre data-language="go">func String(name string, value string, usage string) *string</pre> <p>String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag. </p>
+<h2 id="StringVar">func <span>StringVar</span> </h2> <pre data-language="go">func StringVar(p *string, name string, value string, usage string)</pre> <p>StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag. </p>
+<h2 id="TextVar">func <span>TextVar</span> <span title="Added in Go 1.19">1.19</span> </h2> <pre data-language="go">func TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)</pre> <p>TextVar defines a flag with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p. </p> <h4 id="example_TextVar"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">fs := flag.NewFlagSet("ExampleTextVar", flag.ContinueOnError)
+fs.SetOutput(os.Stdout)
+var ip net.IP
+fs.TextVar(&amp;ip, "ip", net.IPv4(192, 168, 0, 100), "`IP address` to parse")
+fs.Parse([]string{"-ip", "127.0.0.1"})
+fmt.Printf("{ip: %v}\n\n", ip)
+
+// 256 is not a valid IPv4 component
+ip = nil
+fs.Parse([]string{"-ip", "256.0.0.1"})
+fmt.Printf("{ip: %v}\n\n", ip)
+
+</pre> <p>Output:</p> <pre class="output" data-language="go">{ip: 127.0.0.1}
+
+invalid value "256.0.0.1" for flag -ip: invalid IP address: 256.0.0.1
+Usage of ExampleTextVar:
+ -ip IP address
+ IP address to parse (default 192.168.0.100)
+{ip: &lt;nil&gt;}
+</pre> <h2 id="Uint">func <span>Uint</span> </h2> <pre data-language="go">func Uint(name string, value uint, usage string) *uint</pre> <p>Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag. </p>
+<h2 id="Uint64">func <span>Uint64</span> </h2> <pre data-language="go">func Uint64(name string, value uint64, usage string) *uint64</pre> <p>Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag. </p>
+<h2 id="Uint64Var">func <span>Uint64Var</span> </h2> <pre data-language="go">func Uint64Var(p *uint64, name string, value uint64, usage string)</pre> <p>Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. </p>
+<h2 id="UintVar">func <span>UintVar</span> </h2> <pre data-language="go">func UintVar(p *uint, name string, value uint, usage string)</pre> <p>UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag. </p>
+<h2 id="UnquoteUsage">func <span>UnquoteUsage</span> <span title="Added in Go 1.5">1.5</span> </h2> <pre data-language="go">func UnquoteUsage(flag *Flag) (name string, usage string)</pre> <p>UnquoteUsage extracts a back-quoted name from the usage string for a flag and returns it and the un-quoted usage. Given "a `name` to show" it returns ("name", "a name to show"). If there are no back quotes, the name is an educated guess of the type of the flag's value, or the empty string if the flag is boolean. </p>
+<h2 id="Var">func <span>Var</span> </h2> <pre data-language="go">func Var(value Value, name string, usage string)</pre> <p>Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type <a href="#Value">Value</a>, which typically holds a user-defined implementation of <a href="#Value">Value</a>. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of <a href="#Value">Value</a>; in particular, <a href="#Set">Set</a> would decompose the comma-separated string into the slice. </p>
+<h2 id="Visit">func <span>Visit</span> </h2> <pre data-language="go">func Visit(fn func(*Flag))</pre> <p>Visit visits the command-line flags in lexicographical order, calling fn for each. It visits only those flags that have been set. </p>
+<h2 id="VisitAll">func <span>VisitAll</span> </h2> <pre data-language="go">func VisitAll(fn func(*Flag))</pre> <p>VisitAll visits the command-line flags in lexicographical order, calling fn for each. It visits all flags, even those not set. </p>
+<h2 id="ErrorHandling">type <span>ErrorHandling</span> </h2> <p>ErrorHandling defines how <a href="#FlagSet.Parse">FlagSet.Parse</a> behaves if the parse fails. </p>
+<pre data-language="go">type ErrorHandling int</pre> <p>These constants cause <a href="#FlagSet.Parse">FlagSet.Parse</a> to behave as described if the parse fails. </p>
+<pre data-language="go">const (
+ ContinueOnError ErrorHandling = iota // Return a descriptive error.
+ ExitOnError // Call os.Exit(2) or for -h/-help Exit(0).
+ PanicOnError // Call panic with a descriptive error.
+)</pre> <h2 id="Flag">type <span>Flag</span> </h2> <p>A Flag represents the state of a flag. </p>
+<pre data-language="go">type Flag struct {
+ Name string // name as it appears on command line
+ Usage string // help message
+ Value Value // value as set
+ DefValue string // default value (as text); for usage message
+}
+</pre> <h3 id="Lookup">func <span>Lookup</span> </h3> <pre data-language="go">func Lookup(name string) *Flag</pre> <p>Lookup returns the <a href="#Flag">Flag</a> structure of the named command-line flag, returning nil if none exists. </p>
+<h2 id="FlagSet">type <span>FlagSet</span> </h2> <p>A FlagSet represents a set of defined flags. The zero value of a FlagSet has no name and has <a href="#ContinueOnError">ContinueOnError</a> error handling. </p>
+<p><a href="#Flag">Flag</a> names must be unique within a FlagSet. An attempt to define a flag whose name is already in use will cause a panic. </p>
+<pre data-language="go">type FlagSet struct {
+ // Usage is the function called when an error occurs while parsing flags.
+ // The field is a function (not a method) that may be changed to point to
+ // a custom error handler. What happens after Usage is called depends
+ // on the ErrorHandling setting; for the command line, this defaults
+ // to ExitOnError, which exits the program after calling Usage.
+ Usage func()
+ // contains filtered or unexported fields
+}
+</pre> <h3 id="NewFlagSet">func <span>NewFlagSet</span> </h3> <pre data-language="go">func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet</pre> <p>NewFlagSet returns a new, empty flag set with the specified name and error handling property. If the name is not empty, it will be printed in the default usage message and in error messages. </p>
+<h3 id="FlagSet.Arg">func (*FlagSet) <span>Arg</span> </h3> <pre data-language="go">func (f *FlagSet) Arg(i int) string</pre> <p>Arg returns the i'th argument. Arg(0) is the first remaining argument after flags have been processed. Arg returns an empty string if the requested element does not exist. </p>
+<h3 id="FlagSet.Args">func (*FlagSet) <span>Args</span> </h3> <pre data-language="go">func (f *FlagSet) Args() []string</pre> <p>Args returns the non-flag arguments. </p>
+<h3 id="FlagSet.Bool">func (*FlagSet) <span>Bool</span> </h3> <pre data-language="go">func (f *FlagSet) Bool(name string, value bool, usage string) *bool</pre> <p>Bool defines a bool flag with specified name, default value, and usage string. The return value is the address of a bool variable that stores the value of the flag. </p>
+<h3 id="FlagSet.BoolFunc">func (*FlagSet) <span>BoolFunc</span> <span title="Added in Go 1.21">1.21</span> </h3> <pre data-language="go">func (f *FlagSet) BoolFunc(name, usage string, fn func(string) error)</pre> <p>BoolFunc defines a flag with the specified name and usage string without requiring values. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error. </p>
+<h3 id="FlagSet.BoolVar">func (*FlagSet) <span>BoolVar</span> </h3> <pre data-language="go">func (f *FlagSet) BoolVar(p *bool, name string, value bool, usage string)</pre> <p>BoolVar defines a bool flag with specified name, default value, and usage string. The argument p points to a bool variable in which to store the value of the flag. </p>
+<h3 id="FlagSet.Duration">func (*FlagSet) <span>Duration</span> </h3> <pre data-language="go">func (f *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration</pre> <p>Duration defines a time.Duration flag with specified name, default value, and usage string. The return value is the address of a time.Duration variable that stores the value of the flag. The flag accepts a value acceptable to time.ParseDuration. </p>
+<h3 id="FlagSet.DurationVar">func (*FlagSet) <span>DurationVar</span> </h3> <pre data-language="go">func (f *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string)</pre> <p>DurationVar defines a time.Duration flag with specified name, default value, and usage string. The argument p points to a time.Duration variable in which to store the value of the flag. The flag accepts a value acceptable to time.ParseDuration. </p>
+<h3 id="FlagSet.ErrorHandling">func (*FlagSet) <span>ErrorHandling</span> <span title="Added in Go 1.10">1.10</span> </h3> <pre data-language="go">func (f *FlagSet) ErrorHandling() ErrorHandling</pre> <p>ErrorHandling returns the error handling behavior of the flag set. </p>
+<h3 id="FlagSet.Float64">func (*FlagSet) <span>Float64</span> </h3> <pre data-language="go">func (f *FlagSet) Float64(name string, value float64, usage string) *float64</pre> <p>Float64 defines a float64 flag with specified name, default value, and usage string. The return value is the address of a float64 variable that stores the value of the flag. </p>
+<h3 id="FlagSet.Float64Var">func (*FlagSet) <span>Float64Var</span> </h3> <pre data-language="go">func (f *FlagSet) Float64Var(p *float64, name string, value float64, usage string)</pre> <p>Float64Var defines a float64 flag with specified name, default value, and usage string. The argument p points to a float64 variable in which to store the value of the flag. </p>
+<h3 id="FlagSet.Func">func (*FlagSet) <span>Func</span> <span title="Added in Go 1.16">1.16</span> </h3> <pre data-language="go">func (f *FlagSet) Func(name, usage string, fn func(string) error)</pre> <p>Func defines a flag with the specified name and usage string. Each time the flag is seen, fn is called with the value of the flag. If fn returns a non-nil error, it will be treated as a flag value parsing error. </p>
+<h3 id="FlagSet.Init">func (*FlagSet) <span>Init</span> </h3> <pre data-language="go">func (f *FlagSet) Init(name string, errorHandling ErrorHandling)</pre> <p>Init sets the name and error handling property for a flag set. By default, the zero <a href="#FlagSet">FlagSet</a> uses an empty name and the <a href="#ContinueOnError">ContinueOnError</a> error handling policy. </p>
+<h3 id="FlagSet.Int">func (*FlagSet) <span>Int</span> </h3> <pre data-language="go">func (f *FlagSet) Int(name string, value int, usage string) *int</pre> <p>Int defines an int flag with specified name, default value, and usage string. The return value is the address of an int variable that stores the value of the flag. </p>
+<h3 id="FlagSet.Int64">func (*FlagSet) <span>Int64</span> </h3> <pre data-language="go">func (f *FlagSet) Int64(name string, value int64, usage string) *int64</pre> <p>Int64 defines an int64 flag with specified name, default value, and usage string. The return value is the address of an int64 variable that stores the value of the flag. </p>
+<h3 id="FlagSet.Int64Var">func (*FlagSet) <span>Int64Var</span> </h3> <pre data-language="go">func (f *FlagSet) Int64Var(p *int64, name string, value int64, usage string)</pre> <p>Int64Var defines an int64 flag with specified name, default value, and usage string. The argument p points to an int64 variable in which to store the value of the flag. </p>
+<h3 id="FlagSet.IntVar">func (*FlagSet) <span>IntVar</span> </h3> <pre data-language="go">func (f *FlagSet) IntVar(p *int, name string, value int, usage string)</pre> <p>IntVar defines an int flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag. </p>
+<h3 id="FlagSet.Lookup">func (*FlagSet) <span>Lookup</span> </h3> <pre data-language="go">func (f *FlagSet) Lookup(name string) *Flag</pre> <p>Lookup returns the <a href="#Flag">Flag</a> structure of the named flag, returning nil if none exists. </p>
+<h3 id="FlagSet.NArg">func (*FlagSet) <span>NArg</span> </h3> <pre data-language="go">func (f *FlagSet) NArg() int</pre> <p>NArg is the number of arguments remaining after flags have been processed. </p>
+<h3 id="FlagSet.NFlag">func (*FlagSet) <span>NFlag</span> </h3> <pre data-language="go">func (f *FlagSet) NFlag() int</pre> <p>NFlag returns the number of flags that have been set. </p>
+<h3 id="FlagSet.Name">func (*FlagSet) <span>Name</span> <span title="Added in Go 1.10">1.10</span> </h3> <pre data-language="go">func (f *FlagSet) Name() string</pre> <p>Name returns the name of the flag set. </p>
+<h3 id="FlagSet.Output">func (*FlagSet) <span>Output</span> <span title="Added in Go 1.10">1.10</span> </h3> <pre data-language="go">func (f *FlagSet) Output() io.Writer</pre> <p>Output returns the destination for usage and error messages. <span>os.Stderr</span> is returned if output was not set or was set to nil. </p>
+<h3 id="FlagSet.Parse">func (*FlagSet) <span>Parse</span> </h3> <pre data-language="go">func (f *FlagSet) Parse(arguments []string) error</pre> <p>Parse parses flag definitions from the argument list, which should not include the command name. Must be called after all flags in the <a href="#FlagSet">FlagSet</a> are defined and before flags are accessed by the program. The return value will be <a href="#ErrHelp">ErrHelp</a> if -help or -h were set but not defined. </p>
+<h3 id="FlagSet.Parsed">func (*FlagSet) <span>Parsed</span> </h3> <pre data-language="go">func (f *FlagSet) Parsed() bool</pre> <p>Parsed reports whether f.Parse has been called. </p>
+<h3 id="FlagSet.PrintDefaults">func (*FlagSet) <span>PrintDefaults</span> </h3> <pre data-language="go">func (f *FlagSet) PrintDefaults()</pre> <p>PrintDefaults prints, to standard error unless configured otherwise, the default values of all defined command-line flags in the set. See the documentation for the global function PrintDefaults for more information. </p>
+<h3 id="FlagSet.Set">func (*FlagSet) <span>Set</span> </h3> <pre data-language="go">func (f *FlagSet) Set(name, value string) error</pre> <p>Set sets the value of the named flag. </p>
+<h3 id="FlagSet.SetOutput">func (*FlagSet) <span>SetOutput</span> </h3> <pre data-language="go">func (f *FlagSet) SetOutput(output io.Writer)</pre> <p>SetOutput sets the destination for usage and error messages. If output is nil, <span>os.Stderr</span> is used. </p>
+<h3 id="FlagSet.String">func (*FlagSet) <span>String</span> </h3> <pre data-language="go">func (f *FlagSet) String(name string, value string, usage string) *string</pre> <p>String defines a string flag with specified name, default value, and usage string. The return value is the address of a string variable that stores the value of the flag. </p>
+<h3 id="FlagSet.StringVar">func (*FlagSet) <span>StringVar</span> </h3> <pre data-language="go">func (f *FlagSet) StringVar(p *string, name string, value string, usage string)</pre> <p>StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag. </p>
+<h3 id="FlagSet.TextVar">func (*FlagSet) <span>TextVar</span> <span title="Added in Go 1.19">1.19</span> </h3> <pre data-language="go">func (f *FlagSet) TextVar(p encoding.TextUnmarshaler, name string, value encoding.TextMarshaler, usage string)</pre> <p>TextVar defines a flag with a specified name, default value, and usage string. The argument p must be a pointer to a variable that will hold the value of the flag, and p must implement encoding.TextUnmarshaler. If the flag is used, the flag value will be passed to p's UnmarshalText method. The type of the default value must be the same as the type of p. </p>
+<h3 id="FlagSet.Uint">func (*FlagSet) <span>Uint</span> </h3> <pre data-language="go">func (f *FlagSet) Uint(name string, value uint, usage string) *uint</pre> <p>Uint defines a uint flag with specified name, default value, and usage string. The return value is the address of a uint variable that stores the value of the flag. </p>
+<h3 id="FlagSet.Uint64">func (*FlagSet) <span>Uint64</span> </h3> <pre data-language="go">func (f *FlagSet) Uint64(name string, value uint64, usage string) *uint64</pre> <p>Uint64 defines a uint64 flag with specified name, default value, and usage string. The return value is the address of a uint64 variable that stores the value of the flag. </p>
+<h3 id="FlagSet.Uint64Var">func (*FlagSet) <span>Uint64Var</span> </h3> <pre data-language="go">func (f *FlagSet) Uint64Var(p *uint64, name string, value uint64, usage string)</pre> <p>Uint64Var defines a uint64 flag with specified name, default value, and usage string. The argument p points to a uint64 variable in which to store the value of the flag. </p>
+<h3 id="FlagSet.UintVar">func (*FlagSet) <span>UintVar</span> </h3> <pre data-language="go">func (f *FlagSet) UintVar(p *uint, name string, value uint, usage string)</pre> <p>UintVar defines a uint flag with specified name, default value, and usage string. The argument p points to a uint variable in which to store the value of the flag. </p>
+<h3 id="FlagSet.Var">func (*FlagSet) <span>Var</span> </h3> <pre data-language="go">func (f *FlagSet) Var(value Value, name string, usage string)</pre> <p>Var defines a flag with the specified name and usage string. The type and value of the flag are represented by the first argument, of type <a href="#Value">Value</a>, which typically holds a user-defined implementation of <a href="#Value">Value</a>. For instance, the caller could create a flag that turns a comma-separated string into a slice of strings by giving the slice the methods of <a href="#Value">Value</a>; in particular, <a href="#Set">Set</a> would decompose the comma-separated string into the slice. </p>
+<h3 id="FlagSet.Visit">func (*FlagSet) <span>Visit</span> </h3> <pre data-language="go">func (f *FlagSet) Visit(fn func(*Flag))</pre> <p>Visit visits the flags in lexicographical order, calling fn for each. It visits only those flags that have been set. </p>
+<h3 id="FlagSet.VisitAll">func (*FlagSet) <span>VisitAll</span> </h3> <pre data-language="go">func (f *FlagSet) VisitAll(fn func(*Flag))</pre> <p>VisitAll visits the flags in lexicographical order, calling fn for each. It visits all flags, even those not set. </p>
+<h2 id="Getter">type <span>Getter</span> <span title="Added in Go 1.2">1.2</span> </h2> <p>Getter is an interface that allows the contents of a <a href="#Value">Value</a> to be retrieved. It wraps the <a href="#Value">Value</a> interface, rather than being part of it, because it appeared after Go 1 and its compatibility rules. All <a href="#Value">Value</a> types provided by this package satisfy the <a href="#Getter">Getter</a> interface, except the type used by <a href="#Func">Func</a>. </p>
+<pre data-language="go">type Getter interface {
+ Value
+ Get() any
+}</pre> <h2 id="Value">type <span>Value</span> </h2> <p>Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.) </p>
+<p>If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes -name equivalent to -name=true rather than using the next command-line argument. </p>
+<p>Set is called once, in command line order, for each flag present. The flag package may call the <a href="#String">String</a> method with a zero-valued receiver, such as a nil pointer. </p>
+<pre data-language="go">type Value interface {
+ String() string
+ Set(string) error
+}</pre> <h4 id="example_Value"> <span class="text">Example</span>
+</h4> <p>Code:</p> <pre class="code" data-language="go">package flag_test
+
+import (
+ "flag"
+ "fmt"
+ "net/url"
+)
+
+type URLValue struct {
+ URL *url.URL
+}
+
+func (v URLValue) String() string {
+ if v.URL != nil {
+ return v.URL.String()
+ }
+ return ""
+}
+
+func (v URLValue) Set(s string) error {
+ if u, err := url.Parse(s); err != nil {
+ return err
+ } else {
+ *v.URL = *u
+ }
+ return nil
+}
+
+var u = &amp;url.URL{}
+
+func ExampleValue() {
+ fs := flag.NewFlagSet("ExampleValue", flag.ExitOnError)
+ fs.Var(&amp;URLValue{u}, "url", "URL to parse")
+
+ fs.Parse([]string{"-url", "https://golang.org/pkg/flag/"})
+ fmt.Printf(`{scheme: %q, host: %q, path: %q}`, u.Scheme, u.Host, u.Path)
+
+ // Output:
+ // {scheme: "https", host: "golang.org", path: "/pkg/flag/"}
+}
+</pre><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
+ <a href="http://golang.org/pkg/flag/" class="_attribution-link">http://golang.org/pkg/flag/</a>
+ </p>
+</div>