Commit 995f938a authored by Rob Pike's avatar Rob Pike

more nuanced handling of usage message to allow user control.

also print argv(0) in default message

R=gri
DELTA=37  (21 added, 9 deleted, 7 changed)
OCL=15540
CL=15554
parent bf04eefd
...@@ -287,15 +287,6 @@ type Flags struct { ...@@ -287,15 +287,6 @@ type Flags struct {
flag_list *Flag; // BUG: remove when we can iterate over maps flag_list *Flag; // BUG: remove when we can iterate over maps
} }
func (flags *Flags) Usage() {
// BUG: use map iteration when available
print("Usage: \n");
for f := flags.flag_list; f != nil; f = f.next {
print(" -", f.name, "=", f.value.Str(), ": ", f.usage, "\n");
}
sys.exit(1);
}
// --Customer's value getters // --Customer's value getters
func (f *Flag) BVal() bool { func (f *Flag) BVal() bool {
if !f.value.IsBool() { if !f.value.IsBool() {
...@@ -328,6 +319,27 @@ func New() *Flags { ...@@ -328,6 +319,27 @@ func New() *Flags {
var flags *Flags = New(); var flags *Flags = New();
export func PrintDefaults() {
// BUG: use map iteration when available
for f := flags.flag_list; f != nil; f = f.next {
print(" -", f.name, "=", f.value.Str(), ": ", f.usage, "\n");
}
}
export func Usage() {
if sys.argc() > 0 {
print("Usage of ", sys.argv(0), ": \n");
} else {
print("Usage: \n");
}
PrintDefaults();
sys.exit(1);
}
export func NFlag() int {
return len(flags.actual)
}
export func Arg(i int) string { export func Arg(i int) string {
i += flags.first_arg; i += flags.first_arg;
if i < 0 || i >= sys.argc() { if i < 0 || i >= sys.argc() {
...@@ -391,7 +403,7 @@ func (f *Flags) ParseOne(index int) (ok bool, next int) ...@@ -391,7 +403,7 @@ func (f *Flags) ParseOne(index int) (ok bool, next int)
name := s[num_minuses : len(s)]; name := s[num_minuses : len(s)];
if len(name) == 0 || name[0] == '-' || name[0]=='=' { if len(name) == 0 || name[0] == '-' || name[0]=='=' {
print("bad flag syntax: ", s, "\n"); print("bad flag syntax: ", s, "\n");
f.Usage(); Usage();
} }
// it's a flag. does it have an argument? // it's a flag. does it have an argument?
...@@ -408,13 +420,13 @@ func (f *Flags) ParseOne(index int) (ok bool, next int) ...@@ -408,13 +420,13 @@ func (f *Flags) ParseOne(index int) (ok bool, next int)
flag, alreadythere := flags.actual[name]; flag, alreadythere := flags.actual[name];
if alreadythere { if alreadythere {
print("flag specified twice: -", name, "\n"); print("flag specified twice: -", name, "\n");
f.Usage(); Usage();
} }
m := flags.formal; m := flags.formal;
flag, alreadythere = m[name]; // BUG flag, alreadythere = m[name]; // BUG
if !alreadythere { if !alreadythere {
print("flag provided but not defined: -", name, "\n"); print("flag provided but not defined: -", name, "\n");
f.Usage(); Usage();
} }
if !has_value && index < sys.argc()-1 && flag.value.ValidValue(sys.argv(index+1)) { if !has_value && index < sys.argc()-1 && flag.value.ValidValue(sys.argv(index+1)) {
// value is the next arg // value is the next arg
...@@ -428,7 +440,7 @@ func (f *Flags) ParseOne(index int) (ok bool, next int) ...@@ -428,7 +440,7 @@ func (f *Flags) ParseOne(index int) (ok bool, next int)
k, ok := atob(value); k, ok := atob(value);
if !ok { if !ok {
print("invalid boolean value ", value, " for flag: -", name, "\n"); print("invalid boolean value ", value, " for flag: -", name, "\n");
f.Usage(); Usage();
} }
flag.value.AsBool().Set(k) flag.value.AsBool().Set(k)
} else { } else {
...@@ -437,18 +449,18 @@ func (f *Flags) ParseOne(index int) (ok bool, next int) ...@@ -437,18 +449,18 @@ func (f *Flags) ParseOne(index int) (ok bool, next int)
case flag.value.IsInt(): case flag.value.IsInt():
if !has_value { if !has_value {
print("flag needs an argument: -", name, "\n"); print("flag needs an argument: -", name, "\n");
f.Usage(); Usage();
} }
k, ok := atoi(value); k, ok := atoi(value);
if !ok { if !ok {
print("invalid integer value ", value, " for flag: -", name, "\n"); print("invalid integer value ", value, " for flag: -", name, "\n");
f.Usage(); Usage();
} }
flag.value.AsInt().Set(k) flag.value.AsInt().Set(k)
case flag.value.IsString(): case flag.value.IsString():
if !has_value { if !has_value {
print("flag needs an argument: -", name, "\n"); print("flag needs an argument: -", name, "\n");
f.Usage(); Usage();
} }
flag.value.AsString().Set(value) flag.value.AsString().Set(value)
} }
......
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