Skip to content

Commit

Permalink
DEBUG: reduce test, and print debug for reordered args
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed May 8, 2020
1 parent cfcbe47 commit b76f239
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
13 changes: 7 additions & 6 deletions command.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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]
Expand Down
39 changes: 18 additions & 21 deletions command_test.go
Expand Up @@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io/ioutil"
"sort"
"strings"
"testing"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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
},
Expand Down

0 comments on commit b76f239

Please sign in to comment.