List multiple matched commands.

* When multiple commands match, list them all.
* Change example to an existing command.
This commit is contained in:
klauspost 2015-09-11 15:01:35 +02:00 committed by Nick Craig-Wood
parent 807d4a3c00
commit faee50b238

View file

@ -237,8 +237,8 @@ Subcommands:
fmt.Fprintf(os.Stderr, "Options:\n") fmt.Fprintf(os.Stderr, "Options:\n")
pflag.PrintDefaults() pflag.PrintDefaults()
fmt.Fprintf(os.Stderr, ` fmt.Fprintf(os.Stderr, `
It is only necessary to use a unique prefix of the subcommand, eg 'up' It is only necessary to use a unique prefix of the subcommand, eg 'mo'
for 'upload'. for 'move'.
`) `)
} }
@ -283,6 +283,7 @@ func ParseCommand() (*Command, []string) {
args = args[1:] args = args[1:]
// Find the command doing a prefix match // Find the command doing a prefix match
var found = make([]*Command, 0, 1)
var command *Command var command *Command
for i := range Commands { for i := range Commands {
trialCommand := &Commands[i] trialCommand := &Commands[i]
@ -291,16 +292,24 @@ func ParseCommand() (*Command, []string) {
command = trialCommand command = trialCommand
break break
} else if strings.HasPrefix(trialCommand.Name, cmd) { } else if strings.HasPrefix(trialCommand.Name, cmd) {
if command != nil { found = append(found, trialCommand)
fs.Stats.Error()
log.Fatalf("Not unique - matches multiple commands %q", cmd)
}
command = trialCommand
} }
} }
if command == nil { if command == nil {
fs.Stats.Error() switch len(found) {
log.Fatalf("Unknown command %q", cmd) case 0:
fs.Stats.Error()
log.Fatalf("Unknown command %q", cmd)
case 1:
command = found[0]
default:
fs.Stats.Error()
var names []string
for _, cmd := range found {
names = append(names, `"`+cmd.Name+`"`)
}
log.Fatalf("Not unique - matches multiple commands: %s", strings.Join(names, ", "))
}
} }
if command.Run == nil { if command.Run == nil {
syntaxError() syntaxError()