Skip to content

Commit

Permalink
Only look for help and version flags in the first argument
Browse files Browse the repository at this point in the history
" mow.cli" used to search for -h, --help, -v, --version, ... shortcuts
in all the arguments.
This causes an issue with `--` (all what follows is an arg), e.g.:

```
app -- -h
```

Would still display the app help instead of treating `-h` as the
argument it is.

Fixes #41
  • Loading branch information
jawher committed Jan 22, 2017
1 parent 0de3d3b commit 10bba4a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (cli *Cli) parse(args []string, entry, inFlow, outFlow *step) error {
}

func (cli *Cli) versionSetAndRequested(args []string) bool {
return cli.version != nil && cli.isArgSet(args, cli.version.option.names)
return cli.version != nil && cli.isFlagSet(args, cli.version.option.names)
}

/*
Expand Down
29 changes: 20 additions & 9 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,44 @@ func TestImplicitSpec(t *testing.T) {
require.True(t, called, "Exec wasn't called")
}

func TestHelpShortcut(t *testing.T) {
func testHelpAndVersionWithOptionsEnd(flag string, t *testing.T) {
t.Logf("Testing help/version with --: flag=%q", flag)
defer suppressOutput()()

exitCalled := false
defer exitShouldBeCalledWith(t, 2, &exitCalled)()
defer exitShouldBeCalledWith(t, 0, &exitCalled)()

app := App("x", "")
app.Spec = "Y"
app.Version("v version", "1.0")
app.Spec = "CMD"

app.String(StringArg{Name: "Y", Value: "", Desc: ""})
cmd := app.String(StringArg{Name: "CMD", Value: "", Desc: ""})

actionCalled := false
app.Action = func() {
actionCalled = true
require.Equal(t, flag, *cmd)
}
app.Run([]string{"x", "y", "-h", "z"})

require.False(t, actionCalled, "action should not have been called")
require.True(t, exitCalled, "exit should have been called")
app.Run([]string{"x", "--", flag})

require.True(t, actionCalled, "action should have been called")
require.False(t, exitCalled, "exit should not have been called")

}

func TestHelpAndVersionWithOptionsEnd(t *testing.T) {
for _, flag := range []string{"-h", "--help", "-v", "--version"} {
testHelpAndVersionWithOptionsEnd(flag, t)
}
}

func TestHelpMessage(t *testing.T) {
var out, err string
defer captureAndRestoreOutput(&out, &err)()

exitCalled := false
defer exitShouldBeCalledWith(t, 2, &exitCalled)()
defer exitShouldBeCalledWith(t, 0, &exitCalled)()

app := App("app", "App Desc")
app.Spec = "[-o] ARG"
Expand Down Expand Up @@ -104,7 +115,7 @@ func TestLongHelpMessage(t *testing.T) {
defer captureAndRestoreOutput(&out, &err)()

exitCalled := false
defer exitShouldBeCalledWith(t, 2, &exitCalled)()
defer exitShouldBeCalledWith(t, 0, &exitCalled)()

app := App("app", "App Desc")
app.LongDesc = "Longer App Desc"
Expand Down
47 changes: 20 additions & 27 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,13 @@ func (c *Cmd) doInit() error {
}

func (c *Cmd) onError(err error) {
if err != nil {
switch c.ErrorHandling {
case flag.ExitOnError:
exiter(2)
case flag.PanicOnError:
panic(err)
}
} else {
if c.ErrorHandling == flag.ExitOnError {
exiter(2)
}
switch c.ErrorHandling {
case flag.ExitOnError:
exiter(2)
case flag.PanicOnError:
panic(err)
}

}

/*
Expand Down Expand Up @@ -401,7 +396,7 @@ func (c *Cmd) formatDescription(desc, envVar string) string {
func (c *Cmd) parse(args []string, entry, inFlow, outFlow *step) error {
if c.helpRequested(args) {
c.PrintLongHelp()
c.onError(nil)
exiter(0)
return nil
}

Expand Down Expand Up @@ -471,26 +466,24 @@ func (c *Cmd) parse(args []string, entry, inFlow, outFlow *step) error {

}

func (c *Cmd) isArgSet(args []string, searchArgs []string) bool {
for _, arg := range args {
for _, sub := range c.commands {
if sub.isAlias(arg) {
return false
}
}
for _, searchArg := range searchArgs {
if arg == searchArg {
return true
}
func (c *Cmd) helpRequested(args []string) bool {
return c.isFlagSet(args, []string{"-h", "--help"})
}

func (c *Cmd) isFlagSet(args []string, searchArgs []string) bool {
if len(args) == 0 {
return false
}

arg := args[0]
for _, searchArg := range searchArgs {
if arg == searchArg {
return true
}
}
return false
}

func (c *Cmd) helpRequested(args []string) bool {
return c.isArgSet(args, []string{"-h", "--help"})
}

func (c *Cmd) getOptsAndArgs(args []string) int {
consumed := 0

Expand Down

0 comments on commit 10bba4a

Please sign in to comment.