13497: Use basename($0) as subcommand, if it is one.
authorTom Clegg <tclegg@veritasgenetics.com>
Thu, 7 Jun 2018 13:33:20 +0000 (09:33 -0400)
committerTom Clegg <tclegg@veritasgenetics.com>
Thu, 7 Jun 2018 19:23:32 +0000 (15:23 -0400)
Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tclegg@veritasgenetics.com>

lib/cmd/cmd.go
lib/cmd/cmd_test.go

index 2cc71e68a8e749e76d558b6c98e9e2c26a23f2db..8b8427a7007695ac65b1478fc259775662d71a2d 100644 (file)
@@ -11,6 +11,7 @@ import (
        "fmt"
        "io"
        "io/ioutil"
+       "path/filepath"
        "sort"
        "strings"
 )
@@ -46,12 +47,15 @@ func (m Multi) RunCommand(prog string, args []string, stdin io.Reader, stdout, s
                m.Usage(stderr)
                return 2
        }
-       if cmd, ok := m[args[0]]; !ok {
-               fmt.Fprintf(stderr, "unrecognized command %q\n", args[0])
+       _, basename := filepath.Split(prog)
+       if cmd, ok := m[basename]; ok {
+               return cmd.RunCommand(prog, args, stdin, stdout, stderr)
+       } else if cmd, ok = m[args[0]]; ok {
+               return cmd.RunCommand(prog+" "+args[0], args[1:], stdin, stdout, stderr)
+       } else {
+               fmt.Fprintf(stderr, "%s: unrecognized command %q\n", prog, args[0])
                m.Usage(stderr)
                return 2
-       } else {
-               return cmd.RunCommand(prog+" "+args[0], args[1:], stdin, stdout, stderr)
        }
 }
 
index d8a4861572341046dab556ade76a0cb4f2ffe342..2fc50985f194c8caa2e7ba332ce7d94bfb7189c9 100644 (file)
@@ -42,6 +42,16 @@ func (s *CmdSuite) TestHello(c *check.C) {
        c.Check(stderr.String(), check.Equals, "")
 }
 
+func (s *CmdSuite) TestHelloViaProg(c *check.C) {
+       defer cmdtest.LeakCheck(c)()
+       stdout := bytes.NewBuffer(nil)
+       stderr := bytes.NewBuffer(nil)
+       exited := testCmd.RunCommand("/usr/local/bin/echo", []string{"hello", "world"}, bytes.NewReader(nil), stdout, stderr)
+       c.Check(exited, check.Equals, 0)
+       c.Check(stdout.String(), check.Equals, "hello world\n")
+       c.Check(stderr.String(), check.Equals, "")
+}
+
 func (s *CmdSuite) TestUsage(c *check.C) {
        defer cmdtest.LeakCheck(c)()
        stdout := bytes.NewBuffer(nil)
@@ -49,7 +59,7 @@ func (s *CmdSuite) TestUsage(c *check.C) {
        exited := testCmd.RunCommand("prog", []string{"nosuchcommand", "hi"}, bytes.NewReader(nil), stdout, stderr)
        c.Check(exited, check.Equals, 2)
        c.Check(stdout.String(), check.Equals, "")
-       c.Check(stderr.String(), check.Matches, `(?ms)^unrecognized command "nosuchcommand"\n.*echo.*\n`)
+       c.Check(stderr.String(), check.Matches, `(?ms)^prog: unrecognized command "nosuchcommand"\n.*echo.*\n`)
 }
 
 func (s *CmdSuite) TestSubcommandToFront(c *check.C) {