1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
5 // package cmd defines a RunFunc type, representing a process that can
6 // be invoked from a command line.
14 // A RunFunc runs a command with the given args, and returns an exit
16 type RunFunc func(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int
18 // Multi returns a command that looks up its first argument in m, and
19 // runs the resulting RunFunc with the remaining args.
23 // os.Exit(Multi(map[string]RunFunc{
24 // "foobar": func(prog string, args []string) int {
25 // fmt.Println(args[0])
28 // })("/usr/bin/multi", []string{"foobar", "baz"}))
30 // ...prints "baz" and exits 2.
31 func Multi(m map[string]RunFunc) RunFunc {
32 return func(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
34 fmt.Fprintf(stderr, "usage: %s command [args]", prog)
37 if cmd, ok := m[args[0]]; !ok {
38 fmt.Fprintf(stderr, "unrecognized command %q", args[0])
41 return cmd(prog+" "+args[0], args[1:], stdin, stdout, stderr)