diff --git a/command.go b/command.go index 24e9e5c572..63c252eef0 100644 --- a/command.go +++ b/command.go @@ -189,9 +189,11 @@ func (c *Command) parseFlags(args Args, shellComplete bool) (*flag.FlagSet, erro return set, set.Parse(append([]string{"--"}, args...)) } + fmt.Println("before reordering:", strings.Join(args, ",")) if !c.SkipArgReorder { args = reorderArgs(c.Flags, args) } + fmt.Println("after reordering: ", strings.Join(args, ",")) set, err := c.newFlagSet() if err != nil { @@ -265,13 +267,12 @@ func argIsFlag(commandFlags []Flag, arg string) bool { if !strings.HasPrefix(arg, "-") { return false } - // this line turns `--flag` into `flag` if strings.HasPrefix(arg, "--") { - arg = strings.Replace(arg, "-", "", 2) - } - // this line turns `-flag` into `flag` - if strings.HasPrefix(arg, "-") { - arg = strings.Replace(arg, "-", "", 1) + // this line turns `--flag` into `flag` + arg = strings.TrimPrefix(arg, "--") + } else { + // this line turns `-flag` into `flag` + arg = strings.TrimPrefix(arg, "-") } // this line turns `flag=value` into `flag` arg = strings.Split(arg, "=")[0] diff --git a/command_test.go b/command_test.go index 627acbaf51..96ad4a4edf 100644 --- a/command_test.go +++ b/command_test.go @@ -6,7 +6,6 @@ import ( "flag" "fmt" "io/ioutil" - "sort" "strings" "testing" ) @@ -122,25 +121,25 @@ func TestParseAndRunHyphenValues(t *testing.T) { expectedArgs []string expectedOpt string }{ - {[]string{"foo", "test", "arg1"}, []string{"arg1"}, ""}, - {[]string{"foo", "test", "arg1", "arg2"}, []string{"arg1", "arg2"}, ""}, - {[]string{"foo", "test", "--opt", "opt-value"}, []string{}, "opt-value"}, - {[]string{"foo", "test", "--opt", "opt-value", "arg1"}, []string{"arg1"}, "opt-value"}, - {[]string{"foo", "test", "--opt", "opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "opt-value"}, - {[]string{"foo", "test", "arg1", "--opt", "opt-value"}, []string{"arg1"}, "opt-value"}, - {[]string{"foo", "test", "arg1", "--opt", "opt-value", "arg2"}, []string{"arg1", "arg2"}, "opt-value"}, - - {[]string{"foo", "test", "--opt", "-opt-value"}, []string{}, "-opt-value"}, + // {[]string{"foo", "test", "arg1"}, []string{"arg1"}, ""}, + // {[]string{"foo", "test", "arg1", "arg2"}, []string{"arg1", "arg2"}, ""}, + // {[]string{"foo", "test", "--opt", "opt-value"}, []string{}, "opt-value"}, + // {[]string{"foo", "test", "--opt", "opt-value", "arg1"}, []string{"arg1"}, "opt-value"}, + // {[]string{"foo", "test", "--opt", "opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "opt-value"}, + // {[]string{"foo", "test", "arg1", "--opt", "opt-value"}, []string{"arg1"}, "opt-value"}, + // {[]string{"foo", "test", "arg1", "--opt", "opt-value", "arg2"}, []string{"arg1", "arg2"}, "opt-value"}, + + // {[]string{"foo", "test", "--opt", "-opt-value"}, []string{}, "-opt-value"}, {[]string{"foo", "test", "--opt", "-opt-value", "arg1"}, []string{"arg1"}, "-opt-value"}, - {[]string{"foo", "test", "--opt", "-opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "-opt-value"}, - {[]string{"foo", "test", "arg1", "--opt", "-opt-value"}, []string{"arg1"}, "-opt-value"}, - {[]string{"foo", "test", "arg1", "--opt", "-opt-value", "arg2"}, []string{"arg1", "arg2"}, "-opt-value"}, - - {[]string{"foo", "test", "--opt", "--opt-value"}, []string{}, "--opt-value"}, - {[]string{"foo", "test", "--opt", "--opt-value", "arg1"}, []string{"arg1"}, "--opt-value"}, - {[]string{"foo", "test", "--opt", "--opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "--opt-value"}, - {[]string{"foo", "test", "arg1", "--opt", "--opt-value"}, []string{"arg1"}, "--opt-value"}, - {[]string{"foo", "test", "arg1", "--opt", "--opt-value", "arg2"}, []string{"arg1", "arg2"}, "--opt-value"}, + // {[]string{"foo", "test", "--opt", "-opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "-opt-value"}, + // {[]string{"foo", "test", "arg1", "--opt", "-opt-value"}, []string{"arg1"}, "-opt-value"}, + // {[]string{"foo", "test", "arg1", "--opt", "-opt-value", "arg2"}, []string{"arg1", "arg2"}, "-opt-value"}, + // + // {[]string{"foo", "test", "--opt", "--opt-value"}, []string{}, "--opt-value"}, + // {[]string{"foo", "test", "--opt", "--opt-value", "arg1"}, []string{"arg1"}, "--opt-value"}, + // {[]string{"foo", "test", "--opt", "--opt-value", "arg1", "arg2"}, []string{"arg1", "arg2"}, "--opt-value"}, + // {[]string{"foo", "test", "arg1", "--opt", "--opt-value"}, []string{"arg1"}, "--opt-value"}, + // {[]string{"foo", "test", "arg1", "--opt", "--opt-value", "arg2"}, []string{"arg1", "arg2"}, "--opt-value"}, } for _, tc := range cases { @@ -157,8 +156,6 @@ func TestParseAndRunHyphenValues(t *testing.T) { Description: "testing", Action: func(c *Context) error { args = c.Args() - // TODO remove this; looks like v1.22.0 returns args in a different order - sort.Strings(args) opt = c.String("opt") return nil },