Fix crash with "go test -v -cpu=1,1 ./cmd/camput/"

Don't double-register the help flag. Only affects tests run twice, like above.

Change-Id: Ia235f2e2aa4a70e3b5a5c6fe21d8add90069664f
This commit is contained in:
Brad Fitzpatrick 2014-02-21 10:59:34 -08:00
parent 1333551de1
commit 85e2e5e301
2 changed files with 23 additions and 3 deletions

View File

@ -89,6 +89,23 @@ func TestUsageOnNoargs(t *testing.T) {
}
}
// TestCommandUsage tests that we output a command-specific usage message and return
// with a non-zero exit status.
func TestCommandUsage(t *testing.T) {
var e env
out, err, code := e.Run("attr")
if code != 1 {
t.Errorf("exit code = %d; want 1", code)
}
if len(out) != 0 {
t.Errorf("wanted nothing on stdout; got:\n%s", out)
}
sub := "Attr takes 3 args: <permanode> <attr> <value>"
if !bytes.Contains(err, []byte(sub)) {
t.Errorf("stderr doesn't contain substring %q. Got:\n%s", sub, err)
}
}
func TestUploadingChangingDirectory(t *testing.T) {
// TODO(bradfitz):
// $ mkdir /tmp/somedir

View File

@ -61,6 +61,7 @@ var (
// mode name to actual subcommand mapping
modeCommand = make(map[string]CommandRunner)
modeFlags = make(map[string]*flag.FlagSet)
wantHelp = make(map[string]*bool)
// Indirections for replacement by tests
Stderr io.Writer = os.Stderr
@ -100,6 +101,10 @@ func RegisterCommand(mode string, makeCmd func(Flags *flag.FlagSet) CommandRunne
}
flags := flag.NewFlagSet(mode+" options", flag.ContinueOnError)
flags.Usage = func() {}
var cmdHelp bool
flags.BoolVar(&cmdHelp, "help", false, "Help for this mode.")
wantHelp[mode] = &cmdHelp
modeFlags[mode] = flags
modeCommand[mode] = makeCmd(flags)
}
@ -249,13 +254,11 @@ func Main() {
cmdFlags := modeFlags[mode]
cmdFlags.SetOutput(Stderr)
var cmdHelp bool
cmdFlags.BoolVar(&cmdHelp, "help", false, "Help for this mode.")
err := cmdFlags.Parse(args[1:])
if err != nil {
err = ErrUsage
} else {
if cmdHelp {
if *wantHelp[mode] {
help(mode)
return
}