Skip to content

Reordering of args to Command if BoolFlag is after args  #103

Closed

Description

@stengaard

if a Command is invoked as cli cmd arg1 -f arg2 where -f is a BoolFlag the result of ctx.Args() in the command function will be [arg2 arg1].

This appears to be related to #36

I have made the test case below (which currrently fails), but I am not really able to find a decent fix.

func TestApp_CommandWithBoolFlagAfterArgs(t *testing.T) {

    var args []string

    app := cli.NewApp()
    command := cli.Command{
        Name: "cmd",
        Flags: []cli.Flag{
            cli.BoolFlag{
                Name:  "f",
                Usage: "foo",
            },
        },
        Action: func(c *cli.Context) {
            args = c.Args()
        },
    }
    app.Commands = []cli.Command{command}
    app.Run([]string{"", "cmd", "1", "2", "-f", "3"})

    expect := []string{"1", "2", "3"}
    if !reflect.DeepEqual(args, expect) {
        t.Errorf("Got %v (type %T) - expected %v (type %T)", args, args, expect, expect)
    }
}

Fails with

    app_test.go:216: Got [3 1 2] (type []string) - expected [1 2 3] (type []string)

Hope you can come up with something good.

Activity

  1. changed the title [-]Reordering of args to Command if flag is after args [/-] [+]Reordering of args to Command if BoolFlag is after args [/+] on Jul 3, 2014
  2. self-assigned this
    on Aug 10, 2014
  3. added
    status/claimed
    on May 7, 2016
  4. jszwedko commented on May 10, 2016

    @jszwedko
    Contributor

    Fixed by #398 (requires use of the v2 branch however as the change was not backwards compatible).

  5. removed
    status/claimed
    on May 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions