aboutsummaryrefslogtreecommitdiff
path: root/src/flag
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2022-02-03 14:12:08 -0500
committerRuss Cox <rsc@golang.org>2022-04-11 16:34:30 +0000
commit19309779ac5e2f5a2fd3cbb34421dafb2855ac21 (patch)
tree67dfd3e5d96250325e383183f95b6f5fe1968514 /src/flag
parent017933163ab6a2b254f0310c61b57db65cded92e (diff)
downloadgo-19309779ac5e2f5a2fd3cbb34421dafb2855ac21.tar.gz
go-19309779ac5e2f5a2fd3cbb34421dafb2855ac21.zip
all: gofmt main repo
[This CL is part of a sequence implementing the proposal #51082. The design doc is at https://go.dev/s/godocfmt-design.] Run the updated gofmt, which reformats doc comments, on the main repository. Vendored files are excluded. For #51082. Change-Id: I7332f099b60f716295fb34719c98c04eb1a85407 Reviewed-on: https://go-review.googlesource.com/c/go/+/384268 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/flag')
-rw-r--r--src/flag/flag.go23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/flag/flag.go b/src/flag/flag.go
index 15bcb6cea9..a0762441a5 100644
--- a/src/flag/flag.go
+++ b/src/flag/flag.go
@@ -5,29 +5,38 @@
/*
Package flag implements command-line flag parsing.
-Usage
+# Usage
Define flags using flag.String(), Bool(), Int(), etc.
This declares an integer flag, -n, stored in the pointer nFlag, with type *int:
+
import "flag"
var nFlag = flag.Int("n", 1234, "help message for flag n")
+
If you like, you can bind the flag to a variable using the Var() functions.
+
var flagvar int
func init() {
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
}
+
Or you can create custom flags that satisfy the Value interface (with
pointer receivers) and couple them to flag parsing by
+
flag.Var(&flagVal, "name", "help message for flagname")
+
For such flags, the default value is just the initial value of the variable.
After all flags are defined, call
+
flag.Parse()
+
to parse the command line into the defined flags.
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.
+
fmt.Println("ip has value ", *ip)
fmt.Println("flagvar has value ", flagvar)
@@ -35,17 +44,20 @@ After parsing, the arguments following the flags are available as the
slice flag.Args() or individually as flag.Arg(i).
The arguments are indexed from 0 through flag.NArg()-1.
-Command line flag syntax
+# Command line flag syntax
The following forms are permitted:
-flag
-flag=x
-flag x // non-boolean flags only
+
One or two minus signs may be used; they are equivalent.
The last form is not permitted for boolean flags because the
meaning of the command
+
cmd -x *
+
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.
@@ -55,7 +67,9 @@ Flag parsing stops just before the first non-flag argument
Integer flags accept 1234, 0664, 0x1234 and may be negative.
Boolean flags may be:
+
1, 0, t, f, T, F, true, false, TRUE, FALSE, True, False
+
Duration flags accept any input valid for time.ParseDuration.
The default set of command-line flags is controlled by
@@ -605,8 +619,10 @@ func (f *FlagSet) PrintDefaults() {
// 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
+//
// -x int
// usage-message-for-x (default 7)
+//
// 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
@@ -616,8 +632,11 @@ func (f *FlagSet) PrintDefaults() {
// 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
+//
// flag.String("I", "", "search `directory` for include files")
+//
// the output will be
+//
// -I directory
// search directory for include files.
//