9 "git.curoverse.com/arvados.git/sdk/go/config"
12 // A Command is a subcommand that can be invoked by Dispatch.
13 type Command interface {
14 DefaultConfigFile() string
15 ParseFlags([]string) error
19 // Dispatch parses flags from args, chooses an entry in cmds using the
20 // next argument after the parsed flags, loads the command's
21 // configuration file if it exists, passes any additional flags to the
22 // command's ParseFlags method, and -- if all of those steps complete
23 // without errors -- runs the command.
24 func Dispatch(cmds map[string]Command, prog string, args []string) error {
25 fs := flag.NewFlagSet(prog, flag.ContinueOnError)
32 cmd, ok := cmds[subcmd]
34 if subcmd != "" && subcmd != "help" {
35 return fmt.Errorf("unrecognized subcommand %q", subcmd)
39 subcmds = append(subcmds, s)
41 sort.Sort(sort.StringSlice(subcmds))
42 return fmt.Errorf("available subcommands: %q", subcmds)
45 err = config.LoadFile(cmd, cmd.DefaultConfigFile())
46 if err != nil && !os.IsNotExist(err) {
54 err = cmd.ParseFlags(args)