1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
5 // package cmd helps define reusable functions that can be exposed as
6 // [subcommands of] command line programs.
21 type Handler interface {
22 RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int
25 type HandlerFunc func(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int
27 func (f HandlerFunc) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
28 return f(prog, args, stdin, stdout, stderr)
33 func (v Version) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
34 prog = regexp.MustCompile(` -*version$`).ReplaceAllLiteralString(prog, "")
35 fmt.Fprintf(stdout, "%s %s (%s)\n", prog, v, runtime.Version())
39 // Multi is a Handler that looks up its first argument in a map, and
40 // invokes the resulting Handler with the remaining args.
44 // os.Exit(Multi(map[string]Handler{
45 // "foobar": HandlerFunc(func(prog string, args []string) int {
46 // fmt.Println(args[0])
49 // })("/usr/bin/multi", []string{"foobar", "baz"}))
51 // ...prints "baz" and exits 2.
52 type Multi map[string]Handler
54 func (m Multi) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
55 _, basename := filepath.Split(prog)
56 basename = strings.TrimPrefix(basename, "arvados-")
57 basename = strings.TrimPrefix(basename, "crunch-")
58 if cmd, ok := m[basename]; ok {
59 return cmd.RunCommand(prog, args, stdin, stdout, stderr)
60 } else if len(args) < 1 {
61 fmt.Fprintf(stderr, "usage: %s command [args]\n", prog)
64 } else if cmd, ok = m[args[0]]; ok {
65 return cmd.RunCommand(prog+" "+args[0], args[1:], stdin, stdout, stderr)
67 fmt.Fprintf(stderr, "%s: unrecognized command %q\n", prog, args[0])
73 func (m Multi) Usage(stderr io.Writer) {
74 fmt.Fprintf(stderr, "\nAvailable commands:\n")
75 m.listSubcommands(stderr, "")
78 func (m Multi) listSubcommands(out io.Writer, prefix string) {
79 var subcommands []string
81 if strings.HasPrefix(sc, "-") {
82 // Some subcommands have alternate versions
83 // like "--version" for compatibility. Don't
84 // clutter the subcommand summary with those.
87 subcommands = append(subcommands, sc)
89 sort.Strings(subcommands)
90 for _, sc := range subcommands {
91 switch cmd := m[sc].(type) {
93 cmd.listSubcommands(out, prefix+sc+" ")
95 fmt.Fprintf(out, " %s%s\n", prefix, sc)
100 type FlagSet interface {
101 Init(string, flag.ErrorHandling)
104 Parse([]string) error
109 // SubcommandToFront silently parses args using flagset, and returns a
110 // copy of args with the first non-flag argument moved to the
111 // front. If parsing fails or consumes all of args, args is returned
114 // SubcommandToFront invokes methods on flagset that have side
115 // effects, including Parse. In typical usage, flagset will not used
116 // for anything else after being passed to SubcommandToFront.
117 func SubcommandToFront(args []string, flagset FlagSet) []string {
118 flagset.Init("", flag.ContinueOnError)
119 flagset.SetOutput(ioutil.Discard)
120 if err := flagset.Parse(args); err != nil || flagset.NArg() == 0 {
121 // No subcommand found.
124 // Move subcommand to the front.
125 flagargs := len(args) - flagset.NArg()
126 newargs := make([]string, len(args))
127 newargs[0] = args[flagargs]
128 copy(newargs[1:flagargs+1], args[:flagargs])
129 copy(newargs[flagargs+1:], args[flagargs+1:])