Skip to content

Commit

Permalink
fix: --long= should not consume the next argument
Browse files Browse the repository at this point in the history
  • Loading branch information
telemachus committed Jan 17, 2025
1 parent 4e42878 commit a86c7b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
28 changes: 11 additions & 17 deletions flag_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,8 @@ func (fs *FlagSet) parseShortFlag(arg string, args []string) ([]string, error) {
}

func (fs *FlagSet) parseLongFlag(arg string, args []string) ([]string, error) {
var (
name string
value string
)

if equals := strings.IndexRune(arg, '='); equals > 0 {
arg, value = arg[:equals], arg[equals+1:]
}

name = strings.TrimPrefix(arg, "--")
name, value, eqFound := strings.Cut(arg, "=")
name = strings.TrimPrefix(name, "--")

f := fs.findLongFlag(name)
if f == nil {
Expand All @@ -264,22 +256,24 @@ func (fs *FlagSet) parseLongFlag(arg string, args []string) ([]string, error) {
}
}

if value == "" {
if eqFound && f.isBoolFlag && value == "" {
value = "true" // `--debug=` amounts to `--debug=true`
}

if value == "" && !eqFound {
switch {
case f.isBoolFlag:
value = "true" // `-b` or `--foo` default to true
value = "true" // `--foo` defaults to true
if len(args) > 0 {
if _, err := strconv.ParseBool(args[0]); err == nil {
value = args[0] // `-b true` or `--foo false` should also work
value = args[0] // `--foo false` should also work
args = args[1:]
}
}
case !f.isBoolFlag && len(args) > 0:
case len(args) > 0:
value, args = args[0], args[1:]
case !f.isBoolFlag && len(args) <= 0:
return nil, fmt.Errorf("missing value")
default:
panic("unreachable")
return nil, fmt.Errorf("missing value")
}
}

Expand Down
1 change: 1 addition & 0 deletions flag_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestFlagSet_Bool(t *testing.T) {
{args: []string{"--help"}, wantX: false, wantY: true, wantErr: ff.ErrHelp},
{args: []string{"--xflag", "-h"}, wantX: true, wantY: true, wantErr: ff.ErrHelp},
{args: []string{"-y", "--help"}, wantX: false, wantY: false, wantErr: ff.ErrHelp},
{args: []string{"--xflag=", "--help"}, wantX: true, wantY: false, wantErr: ff.ErrHelp},
} {
t.Run(strings.Join(test.args, " "), func(t *testing.T) {
fs := ff.NewFlagSet(t.Name())
Expand Down
6 changes: 6 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ func TestParse_FlagSet(t *testing.T) {
Args: []string{`--str`, `foo`, `--help`, `-b`},
Want: fftest.Vars{S: "foo", B: false, WantParseErrorIs: ff.ErrHelp},
},
{
Name: "--str= -a",
Constructors: []fftest.Constructor{fftest.CoreConstructor},
Args: []string{`--str=`, `-a`},
Want: fftest.Vars{S: "", A: true},
},
{
Name: "-s foo -f 1.23",
Constructors: []fftest.Constructor{fftest.CoreConstructor},
Expand Down

0 comments on commit a86c7b7

Please sign in to comment.