1
0
mirror of https://github.com/golang/go synced 2024-11-19 22:04:44 -07:00

flag: replace fail with failf

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/4254061
This commit is contained in:
Robert Hencke 2011-03-08 15:41:36 +11:00 committed by Andrew Gerrand
parent 4c052844dd
commit 2a36f9df1c

View File

@ -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) {
} }
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) {
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) {
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