Merge pull request #2 from alecthomas/master
Add ability to disable interspersed option support.
diff --git a/README.md b/README.md
index 714d202..a9506fd 100644
--- a/README.md
+++ b/README.md
@@ -40,7 +40,9 @@
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 @@
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/export_test.go b/export_test.go
index 8ff8e29..8c74635 100644
--- a/export_test.go
+++ b/export_test.go
@@ -4,7 +4,10 @@
package pflag
-import "os"
+import (
+ "io/ioutil"
+ "os"
+)
// Additional routines compiled into the package only during testing.
@@ -12,7 +15,11 @@
// After calling ResetForTesting, parse errors in flag handling will not
// exit the program.
func ResetForTesting(usage func()) {
- commandLine = NewFlagSet(os.Args[0], ContinueOnError)
+ commandLine = &FlagSet{
+ name: os.Args[0],
+ errorHandling: ContinueOnError,
+ output: ioutil.Discard,
+ }
Usage = usage
}
diff --git a/flag.go b/flag.go
index 8f5e8e7..743e011 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
@@ -844,8 +846,9 @@
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)
@@ -881,6 +884,11 @@
commandLine.VarP(value, name, "", usage)
}
+// Like Var, but accepts a shorthand letter that can be used after a single dash.
+func VarP(value Value, name, shorthand, usage string) {
+ commandLine.VarP(value, name, shorthand, usage)
+}
+
// failf prints to standard error a formatted error and usage message and
// returns the error.
func (f *FlagSet) failf(format string, a ...interface{}) error {