Skip to content

Commit

Permalink
Allow passing value starting with - to custom option
Browse files Browse the repository at this point in the history
Signed-off-by: Difan Zhao <dzhao@pivotal.io>
  • Loading branch information
XenoPhex authored and Difan Zhao committed Apr 4, 2017
1 parent 460c7bb commit 9ddd969
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (p *Parser) parseOption(s *parseState, name string, option *Option, canarg
} else {
arg = s.pop()

if argumentIsOption(arg) && !(option.isSignedNumber() && len(arg) > 1 && arg[0] == '-' && arg[1] >= '0' && arg[1] <= '9') {
if argumentIsOption(arg) && option.isUnmarshaler() == nil && !(option.isSignedNumber() && len(arg) > 1 && arg[0] == '-' && arg[1] >= '0' && arg[1] <= '9') {
return newErrorf(ErrExpectedArgument, "expected argument for flag `%s', but got option `%s'", option, arg)
} else if p.Options&PassDoubleDash != 0 && arg == "--" {
return newErrorf(ErrExpectedArgument, "expected argument for flag `%s', but got double dash `--'", option)
Expand Down
38 changes: 28 additions & 10 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ func TestEnvDefaults(t *testing.T) {
}
}

type CustomFlag struct {
Value string
}

func (c *CustomFlag) UnmarshalFlag(s string) error {
c.Value = s
return nil
}

func TestOptionAsArgument(t *testing.T) {
var tests = []struct {
args []string
Expand Down Expand Up @@ -399,30 +408,39 @@ func TestOptionAsArgument(t *testing.T) {
rest: []string{"-", "-"},
},
{
// Accept arguments which start with '-' if the next character is a digit, for number options only
// Accept arguments which start with '-' if the next character is a digit
args: []string{"--int-slice", "-3"},
},
{
// Accept arguments which start with '-' if the next character is a digit, for number options only
// Accept arguments which start with '-' if the next character is a digit
args: []string{"--int16", "-3"},
},
{
// Accept arguments which start with '-' if the next character is a digit, for number options only
// Accept arguments which start with '-' if the next character is a digit
args: []string{"--float32", "-3.2"},
},
{
// Accept arguments which start with '-' if the next character is a digit, for number options only
// Accept arguments which start with '-' if the next character is a digit
args: []string{"--float32ptr", "-3.2"},
},
{
// Accept arguments which start with '-' if the next character is not a digit, for custom flags
args: []string{"--custom-flag", "-foo"},
},
{
// Accept arguments which start with '-' if the next character is a digit, for custom flags
args: []string{"--custom-flag", "-1"},
},
}

var opts struct {
StringSlice []string `long:"string-slice"`
IntSlice []int `long:"int-slice"`
Int16 int16 `long:"int16"`
Float32 float32 `long:"float32"`
Float32Ptr *float32 `long:"float32ptr"`
OtherOption bool `long:"other-option" short:"o"`
StringSlice []string `long:"string-slice"`
IntSlice []int `long:"int-slice"`
Int16 int16 `long:"int16"`
Float32 float32 `long:"float32"`
Float32Ptr *float32 `long:"float32ptr"`
OtherOption bool `long:"other-option" short:"o"`
Custom CustomFlag `long:"custom-flag" short:"c"`
}

for _, test := range tests {
Expand Down

0 comments on commit 9ddd969

Please # to comment.