Commit 2a36f9df authored by Robert Hencke's avatar Robert Hencke Committed by Andrew Gerrand

flag: replace fail with failf

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/4254061
parent 4c052844
...@@ -260,7 +260,9 @@ var Usage = func() { ...@@ -260,7 +260,9 @@ var Usage = func() {
var panicOnError = false var panicOnError = false
func fail() { // failf prints to standard output a formatted error and Usage, and then exits the program.
func failf(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
Usage() Usage()
if panicOnError { if panicOnError {
panic("flag parse error") panic("flag parse error")
...@@ -415,8 +417,7 @@ func (f *allFlags) parseOne() (ok bool) { ...@@ -415,8 +417,7 @@ func (f *allFlags) parseOne() (ok bool) {
} }
name := s[num_minuses:] name := s[num_minuses:]
if len(name) == 0 || name[0] == '-' || name[0] == '=' { if len(name) == 0 || name[0] == '-' || name[0] == '=' {
fmt.Fprintln(os.Stderr, "bad flag syntax:", s) failf("bad flag syntax: %s\n", s)
fail()
} }
// it's a flag. does it have an argument? // it's a flag. does it have an argument?
...@@ -434,14 +435,12 @@ func (f *allFlags) parseOne() (ok bool) { ...@@ -434,14 +435,12 @@ func (f *allFlags) parseOne() (ok bool) {
m := flags.formal m := flags.formal
flag, alreadythere := m[name] // BUG flag, alreadythere := m[name] // BUG
if !alreadythere { if !alreadythere {
fmt.Fprintf(os.Stderr, "flag provided but not defined: -%s\n", name) failf("flag provided but not defined: -%s\n", name)
fail()
} }
if fv, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg if fv, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
if has_value { if has_value {
if !fv.Set(value) { if !fv.Set(value) {
fmt.Fprintf(os.Stderr, "invalid boolean value %q for flag: -%s\n", value, name) failf("invalid boolean value %q for flag: -%s\n", value, name)
fail()
} }
} else { } else {
fv.Set("true") fv.Set("true")
...@@ -454,13 +453,11 @@ func (f *allFlags) parseOne() (ok bool) { ...@@ -454,13 +453,11 @@ func (f *allFlags) parseOne() (ok bool) {
value, f.args = f.args[0], f.args[1:] value, f.args = f.args[0], f.args[1:]
} }
if !has_value { if !has_value {
fmt.Fprintf(os.Stderr, "flag needs an argument: -%s\n", name) failf("flag needs an argument: -%s\n", name)
fail()
} }
ok = flag.Value.Set(value) ok = flag.Value.Set(value)
if !ok { if !ok {
fmt.Fprintf(os.Stderr, "invalid value %q for flag: -%s\n", value, name) failf("invalid value %q for flag: -%s\n", value, name)
fail()
} }
} }
flags.actual[name] = flag flags.actual[name] = flag
......
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