Skip to content

cli.Command: Argument order is changed depending on the flag set #355

Closed

Description

@brenol

Hello!

I'm not sure if this is the expected behavior, however the following code:

package main

import (
    "fmt"
    "os"

    "github.com/codegangsta/cli"
)

func main() {
    app := cli.NewApp()

    app.Commands = []cli.Command{
        cli.Command{
            Name:  "update",
            Usage: "update a report",
            Flags: []cli.Flag{
                cli.StringFlag{
                    Name:  "filename",
                    Usage: "filename",
                },
            },
            Action: func(c *cli.Context) {
                fmt.Println(c.Args())
            },
        },
    }

    app.Run(os.Args)
}

Runs differently depending on the position of the arguments passed:

$ ./main update Arg1 Arg2 Arg3 --filename test.json
[Arg1 Arg2 Arg3]

$ ./main update Arg1 --filename test.json Arg2 Arg3
[Arg2 Arg3 Arg1]

Expected behavior for me would be that when running the second command, the following should output:

$ ./main update Arg1 --filename test.json Arg2 Arg3
[Arg1 Arg2 Arg3]

It seems that the fact that the flag is in the middle is breaking the Args() order.

Thanks

Activity

  1. jszwedko commented on Apr 24, 2016

    @jszwedko
    Contributor

    @brenol definitely a bug, thank you for the report!

  2. added
    kind/bug
    on Apr 24, 2016
  3. jszwedko commented on Apr 24, 2016

    @jszwedko
    Contributor

    This appears to be due to the argument reordering that is done to try to trick the stdlib flag parser to handling commands that have arguments specified before flags (the stdlib flag library stops flag parsing as soon as it encounters the first argument). At first blush, I think supporting this behavior would require much more complicated preprocessing of flags, but I'm open to reviewing a PR addressing this. I'm almost more inclined to remove the re-ordering feature as it appears to only work in very specific circumstances (if you specify all of your arguments before you specify flags).

    https://github.com/codegangsta/cli/blob/master/command.go#L104-L116

  4. added this to the v2.0.0 milestone on May 3, 2016
  5. jszwedko commented on May 3, 2016

    @jszwedko
    Contributor

    I think this supersedes #103 too -- my opinion is to remove the reordering.

  6. danslimmon commented on May 7, 2016

    @danslimmon

    Removing the ordering seems like a fine answer. I looked at this for a while today, and there's really no way to fix it without, like you said @jszwedko, a whole bunch of complicated flag preprocessing.

  7. added
    status/claimed
    on May 7, 2016
  8. 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). Let me know what you think!

  9. 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

kind/bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions