Commit 689c808c authored by Rob Pike's avatar Rob Pike

clean up flags package a bit.

fix a bug in Usage message - would print current value instead of default.

R=rsc
DELTA=53  (7 added, 4 deleted, 42 changed)
OCL=24323
CL=24323
parent 360f0aac
...@@ -14,8 +14,9 @@ package flag ...@@ -14,8 +14,9 @@ package flag
* If you like, you can bind the flag to a variable using the Var() functions. * If you like, you can bind the flag to a variable using the Var() functions.
* var flagvar int * var flagvar int
* func init() { * func init() {
* flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname") * flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
* } * }
*
* 2) After all flags are defined, call * 2) After all flags are defined, call
* flag.Parse() * flag.Parse()
* to parse the command line into the defined flags. * to parse the command line into the defined flags.
...@@ -219,9 +220,10 @@ type _Value interface { ...@@ -219,9 +220,10 @@ type _Value interface {
// -- Flag structure (internal) // -- Flag structure (internal)
type Flag struct { type Flag struct {
name string; name string; // name as it appears on command line
usage string; usage string; // help message
value _Value; value _Value; // value as set
defvalue string; // default value (as text); for usage message
} }
type allFlags struct { type allFlags struct {
...@@ -234,7 +236,7 @@ var flags *allFlags = &allFlags{make(map[string] *Flag), make(map[string] *Flag) ...@@ -234,7 +236,7 @@ var flags *allFlags = &allFlags{make(map[string] *Flag), make(map[string] *Flag)
func PrintDefaults() { func PrintDefaults() {
for k, f := range flags.formal { for k, f := range flags.formal {
print(" -", f.name, "=", f.value.str(), ": ", f.usage, "\n"); print(" -", f.name, "=", f.defvalue, ": ", f.usage, "\n");
} }
} }
...@@ -269,75 +271,76 @@ func add(name string, value _Value, usage string) { ...@@ -269,75 +271,76 @@ func add(name string, value _Value, usage string) {
f.name = name; f.name = name;
f.usage = usage; f.usage = usage;
f.value = value; f.value = value;
f.defvalue = value.str(); // Remember the default value as a string; it won't change.
dummy, alreadythere := flags.formal[name]; dummy, alreadythere := flags.formal[name];
if alreadythere { if alreadythere {
print("flag redefined: ", name, "\n"); print("flag redefined: ", name, "\n");
panic("flag redefinition"); panic("flag redefinition"); // Happens only if flags are declared with identical names
} }
flags.formal[name] = f; flags.formal[name] = f;
} }
func BoolVar(p *bool, name string, value bool, usage string) {
add(name, newBoolValue(value, p), usage);
}
func Bool(name string, value bool, usage string) *bool { func Bool(name string, value bool, usage string) *bool {
p := new(bool); p := new(bool);
add(name, newBoolValue(value, p), usage); BoolVar(p, name, value, usage);
return p; return p;
} }
func BoolVar(p *bool, name string, value bool, usage string) { func IntVar(p *int, name string, value int, usage string) {
add(name, newBoolValue(value, p), usage); add(name, newIntValue(value, p), usage);
} }
func Int(name string, value int, usage string) *int { func Int(name string, value int, usage string) *int {
p := new(int); p := new(int);
add(name, newIntValue(value, p), usage); IntVar(p, name, value, usage);
return p; return p;
} }
func IntVar(p *int, name string, value int, usage string) { func Int64Var(p *int64, name string, value int64, usage string) {
add(name, newIntValue(value, p), usage); add(name, newInt64Value(value, p), usage);
} }
func Int64(name string, value int64, usage string) *int64 { func Int64(name string, value int64, usage string) *int64 {
p := new(int64); p := new(int64);
add(name, newInt64Value(value, p), usage); Int64Var(p, name, value, usage);
return p; return p;
} }
func Int64Var(p *int64, name string, value int64, usage string) { func UintVar(p *uint, name string, value uint, usage string) {
add(name, newInt64Value(value, p), usage); add(name, newUintValue(value, p), usage);
} }
func Uint(name string, value uint, usage string) *uint { func Uint(name string, value uint, usage string) *uint {
p := new(uint); p := new(uint);
add(name, newUintValue(value, p), usage); UintVar(p, name, value, usage);
return p; return p;
} }
func UintVar(p *uint, name string, value uint, usage string) { func Uint64Var(p *uint64, name string, value uint64, usage string) {
add(name, newUintValue(value, p), usage); add(name, newUint64Value(value, p), usage);
} }
func Uint64(name string, value uint64, usage string) *uint64 { func Uint64(name string, value uint64, usage string) *uint64 {
p := new(uint64); p := new(uint64);
add(name, newUint64Value(value, p), usage); Uint64Var(p, name, value, usage);
return p; return p;
} }
func Uint64Var(p *uint64, name string, value uint64, usage string) { func StringVar(p *string, name, value string, usage string) {
add(name, newUint64Value(value, p), usage); add(name, newStringValue(value, p), usage);
} }
func String(name, value string, usage string) *string { func String(name, value string, usage string) *string {
p := new(string); p := new(string);
add(name, newStringValue(value, p), usage); StringVar(p, name, value, usage);
return p; return p;
} }
func StringVar(p *string, name, value string, usage string) { func (f *allFlags) parseOne(index int) (ok bool, next int)
add(name, newStringValue(value, p), usage);
}
func (f *allFlags) ParseOne(index int) (ok bool, next int)
{ {
s := sys.Args[index]; s := sys.Args[index];
f.first_arg = index; // until proven otherwise f.first_arg = index; // until proven otherwise
...@@ -434,7 +437,7 @@ func (f *allFlags) ParseOne(index int) (ok bool, next int) ...@@ -434,7 +437,7 @@ func (f *allFlags) ParseOne(index int) (ok bool, next int)
func Parse() { func Parse() {
for i := 1; i < len(sys.Args); { for i := 1; i < len(sys.Args); {
ok, next := flags.ParseOne(i); ok, next := flags.parseOne(i);
if next > 0 { if next > 0 {
flags.first_arg = next; flags.first_arg = next;
i = next; i = next;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment