aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ogier <[email protected]>2012-12-19 04:23:25 -0500
committerAlex Ogier <[email protected]>2012-12-19 04:23:25 -0500
commitbc8eb13c45bf419dbf31b974e9931e5ab049cada (patch)
tree4c2731f0f02205a55bed85df370ef4bd30af522e
parent929490184252ab7e27a45d03738303e99df438f7 (diff)
Merge changes from Go's standard flag library.
Up to date through e7cd0a82d669. Relevant changesets: https://code.google.com/p/go/source/detail?r=059ba0d0dc6a27d115c619ff8bcd646335eed67f https://code.google.com/p/go/source/detail?r=64a249c65d510184268160bf595cf3db478a296e https://code.google.com/p/go/source/detail?r=6ca4825bc86760d43392469c0af266bb858fc77b
-rw-r--r--README.md10
-rw-r--r--flag.go17
2 files changed, 16 insertions, 11 deletions
diff --git a/README.md b/README.md
index 714d202..a9506fd 100644
--- a/README.md
+++ b/README.md
@@ -40,7 +40,9 @@ Most code never instantiates this struct directly, and instead uses
functions such as String(), BoolVar(), and Var(), and is therefore
unaffected.
-Define flags using flag.String(), Bool(), Int(), etc. Example:
+Define flags using flag.String(), Bool(), Int(), etc.
+
+This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
``` go
var ip *int = flag.Int("flagname", 1234, "help message for flagname")
@@ -76,13 +78,13 @@ 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.
``` go
-fmt.Println("ip has value ", *ip);
-fmt.Println("flagvar has value ", flagvar);
+fmt.Println("ip has value ", *ip)
+fmt.Println("flagvar has value ", flagvar)
```
After parsing, the arguments after the flag are available as the
slice flag.Args() or individually as flag.Arg(i).
-The arguments are indexed from 0 up to flag.NArg().
+The arguments are indexed from 0 through flag.NArg()-1.
The pflag package also defines some new functions that are not in flag,
that give one-letter shorthands for flags. You can use these by appending
diff --git a/flag.go b/flag.go
index 06edf20..a698e27 100644
--- a/flag.go
+++ b/flag.go
@@ -24,8 +24,10 @@
functions such as String(), BoolVar(), and Var(), and is therefore
unaffected.
- Define flags using flag.String(), Bool(), Int(), etc. Example:
- var ip *int = flag.Int("flagname", 1234, "help message for flagname")
+ Define flags using flag.String(), Bool(), Int(), etc.
+
+ This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
+ var ip = flag.Int("flagname", 1234, "help message for flagname")
If you like, you can bind the flag to a variable using the Var() functions.
var flagvar int
func init() {
@@ -42,12 +44,12 @@
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);
+ fmt.Println("ip has value ", *ip)
+ fmt.Println("flagvar has value ", flagvar)
After parsing, the arguments after the flag are available as the
slice flag.Args() or individually as flag.Arg(i).
- The arguments are indexed from 0 up to flag.NArg().
+ The arguments are indexed from 0 through flag.NArg()-1.
The pflag package also defines some new functions that are not in flag,
that give one-letter shorthands for flags. You can use these by appending
@@ -843,8 +845,9 @@ func (f *FlagSet) VarP(value Value, name, shorthand, usage string) {
flag := &Flag{name, shorthand, usage, value, value.String()}
_, alreadythere := f.formal[name]
if alreadythere {
- fmt.Fprintf(f.out(), "%s flag redefined: %s\n", f.name, name)
- panic("flag redefinition") // Happens only if flags are declared with identical names
+ msg := fmt.Sprintf("%s flag redefined: %s", f.name, name)
+ fmt.Fprintln(f.out(), msg)
+ panic(msg) // Happens only if flags are declared with identical names
}
if f.formal == nil {
f.formal = make(map[string]*Flag)