Preserve double dash values during flag alias parsing
This commit is contained in:
@@ -56,7 +56,7 @@ func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []comm
|
||||
index++
|
||||
continue
|
||||
}
|
||||
if !flag.Boolean && index+1 < len(argv) && argv[index+1] != "--" {
|
||||
if !flag.Boolean && index+1 < len(argv) {
|
||||
interpreted = append(interpreted, "--"+flag.Name+"="+argv[index+1])
|
||||
index += 2
|
||||
continue
|
||||
@@ -101,7 +101,7 @@ func interpretFlagTokens(argv []string, flags []commandmeta.Flag, aliases []comm
|
||||
interpreted = append(interpreted, "--"+flag.Name+"="+value)
|
||||
break
|
||||
}
|
||||
if index+1 < len(argv) && argv[index+1] != "--" {
|
||||
if index+1 < len(argv) {
|
||||
interpreted = append(interpreted, "--"+flag.Name+"="+argv[index+1])
|
||||
index += 2
|
||||
consumedNext = true
|
||||
|
||||
@@ -92,6 +92,29 @@ func TestParseFlagAliasesPreserveValuesAndTerminators(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseConsumesDoubleDashAsAFlagValue(t *testing.T) {
|
||||
for _, spelling := range []string{"--color", "--colour", "-c", "-C"} {
|
||||
t.Run(spelling, func(t *testing.T) {
|
||||
result := Parse(flagGrammarTree(), []string{"run", spelling, "--", "--taggy=one", "--colour=red", "--", "--taggy=operand"}, flagGrammarOptions())
|
||||
if result.Failure != nil {
|
||||
t.Fatalf("parse failed: %s", result.Failure.Message)
|
||||
}
|
||||
if !reflect.DeepEqual(result.FlagValues[0], []string{"--", "red"}) || !reflect.DeepEqual(result.FlagValues[4], []string{"one"}) {
|
||||
t.Errorf("flag values = %#v", result.FlagValues)
|
||||
}
|
||||
if !reflect.DeepEqual(result.FlagSpellings[0], []string{spelling, "--colour"}) || !reflect.DeepEqual(result.Operands, []string{"--taggy=operand"}) {
|
||||
t.Errorf("spellings = %#v, operands = %#v", result.FlagSpellings, result.Operands)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, args := range [][]string{{"run", "-v", "--", "--colour"}, {"run", "--color", "blue", "--", "--colour"}} {
|
||||
result := Parse(flagGrammarTree(), args, flagGrammarOptions())
|
||||
if result.Failure != nil || !reflect.DeepEqual(result.Operands, []string{"--colour"}) {
|
||||
t.Errorf("Parse(%q) = %#v", args, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseHelpAliasAndUnknownShortMembers(t *testing.T) {
|
||||
result := Parse(flagGrammarTree(), []string{"run", "--hlep"}, flagGrammarOptions())
|
||||
if result.Failure != nil || !result.HelpRequested {
|
||||
|
||||
Reference in New Issue
Block a user