Unverified Commit 59b058aa authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Enable abbreviated commands

parent d850e500
Loading
Loading
Loading
Loading
+17 −3
Original line number Diff line number Diff line
@@ -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)
+241 −418

File changed.

Preview size limit exceeded, changes collapsed.

+18 −3
Original line number Diff line number Diff line
@@ -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
}