Skip to content
Snippets Groups Projects
Unverified Commit 59b058aa authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Enable abbreviated commands

parent d850e500
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,6 @@ type Flags map[string]*Flag
// Command contains the definition of a command, it's flags and subcommands.
type Command struct {
Flags Flags
FlagOrder []string
Args []string
MaxArgs int
MinArgs int
......@@ -41,8 +40,23 @@ func (c Commands) ParseAndRun(line string) error {
words := strings.Fields(line)
cmd, ok := c[strings.ToUpper(words[0])]
if !ok {
wordup := strings.ToUpper(words[0])
possibles := []string{}
for word := range c {
if strings.HasPrefix(word, wordup) {
possibles = append(possibles, word)
}
}
switch len(possibles) {
case 0:
fmt.Printf("ERROR: Unknown command '%s'\n", words[0])
return nil
case 1:
cmd = c[possibles[0]]
default:
fmt.Printf("ERROR: Ambiguous command '%s' (matches %s)\n", words[0], possibles)
return nil
}
}
if cmd.Action == nil {
fmt.Printf("ERROR: Command not implemented:\n%s\n", cmd.Description)
......
This diff is collapsed.
......@@ -78,9 +78,24 @@ func ActionHelp(cmd *dclish.Command) error {
wordup := strings.ToUpper(cmd.Args[0])
helptext, ok := helpmap[wordup]
if !ok {
possibles := []string{}
for word := range helpmap {
if strings.HasPrefix(word, wordup) {
possibles = append(possibles, word)
}
}
switch len(possibles) {
case 0:
fmt.Printf("ERROR: Topic not found: '%s'.\n", cmd.Args[0])
return nil
case 1:
helptext = helpmap[possibles[0]]
default:
fmt.Printf("ERROR: Ambiguous topic '%s' (matches %s)\n", cmd.Args[0], possibles)
return nil
}
}
fmt.Printf("%s\n", helptext)
fmt.Printf("%s\n\n", helptext)
return nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment