Skip to content

Commit 5b3eb1c

Browse files
authored
Merge pull request #1941 from dearchap/issue_1930
Fix:(issue_1930) Fix for invalid bool counts
2 parents 37a4347 + 0faa177 commit 5b3eb1c

File tree

4 files changed

+72
-68
lines changed

4 files changed

+72
-68
lines changed

command.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,6 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
800800
return cmd.Args(), err
801801
}
802802

803-
tracef("normalizing flags (cmd=%[1]q)", cmd.Name)
804-
805-
if err := normalizeFlags(cmd.Flags, cmd.flagSet); err != nil {
806-
return cmd.Args(), err
807-
}
808-
809803
tracef("done parsing flags (cmd=%[1]q)", cmd.Name)
810804

811805
return cmd.Args(), nil

command_test.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,17 +3204,60 @@ func TestCommand_Bool(t *testing.T) {
32043204
}
32053205

32063206
func TestCommand_Value(t *testing.T) {
3207-
set := flag.NewFlagSet("test", 0)
3208-
set.Int("myflag", 12, "doc")
3209-
parentSet := flag.NewFlagSet("test", 0)
3210-
parentSet.Int("top-flag", 13, "doc")
3211-
pCmd := &Command{flagSet: parentSet}
3212-
cmd := &Command{flagSet: set, parent: pCmd}
3207+
subCmd := &Command{
3208+
Name: "test",
3209+
Flags: []Flag{
3210+
&IntFlag{
3211+
Name: "myflag",
3212+
Usage: "doc",
3213+
Aliases: []string{"m", "mf"},
3214+
},
3215+
},
3216+
Action: func(ctx context.Context, c *Command) error {
3217+
return nil
3218+
},
3219+
}
32133220

3214-
r := require.New(t)
3215-
r.Equal(12, cmd.Value("myflag"))
3216-
r.Equal(13, cmd.Value("top-flag"))
3217-
r.Nil(cmd.Value("unknown-flag"))
3221+
cmd := &Command{
3222+
Flags: []Flag{
3223+
&IntFlag{
3224+
Name: "top-flag",
3225+
Usage: "doc",
3226+
Aliases: []string{"t", "tf"},
3227+
},
3228+
},
3229+
Commands: []*Command{
3230+
subCmd,
3231+
},
3232+
}
3233+
t.Run("flag name", func(t *testing.T) {
3234+
r := require.New(t)
3235+
err := cmd.Run(buildTestContext(t), []string{"main", "--top-flag", "13", "test", "--myflag", "14"})
3236+
3237+
r.NoError(err)
3238+
r.Equal(int64(13), cmd.Value("top-flag"))
3239+
r.Equal(int64(13), cmd.Value("t"))
3240+
r.Equal(int64(13), cmd.Value("tf"))
3241+
3242+
r.Equal(int64(14), subCmd.Value("myflag"))
3243+
r.Equal(int64(14), subCmd.Value("m"))
3244+
r.Equal(int64(14), subCmd.Value("mf"))
3245+
})
3246+
3247+
t.Run("flag aliases", func(t *testing.T) {
3248+
r := require.New(t)
3249+
err := cmd.Run(buildTestContext(t), []string{"main", "-tf", "15", "test", "-m", "16"})
3250+
3251+
r.NoError(err)
3252+
r.Equal(int64(15), cmd.Value("top-flag"))
3253+
r.Equal(int64(15), cmd.Value("t"))
3254+
r.Equal(int64(15), cmd.Value("tf"))
3255+
3256+
r.Equal(int64(16), subCmd.Value("myflag"))
3257+
r.Equal(int64(16), subCmd.Value("m"))
3258+
r.Equal(int64(16), subCmd.Value("mf"))
3259+
r.Nil(cmd.Value("unknown-flag"))
3260+
})
32183261
}
32193262

32203263
func TestCommand_Value_InvalidFlagAccessHandler(t *testing.T) {

flag.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cli
22

33
import (
44
"context"
5-
"errors"
65
"flag"
76
"fmt"
87
"io"
@@ -198,48 +197,6 @@ func newFlagSet(name string, flags []Flag) (*flag.FlagSet, error) {
198197
return set, nil
199198
}
200199

201-
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
202-
switch ff.Value.(type) {
203-
case Serializer:
204-
_ = set.Set(name, ff.Value.(Serializer).Serialize())
205-
default:
206-
_ = set.Set(name, ff.Value.String())
207-
}
208-
}
209-
210-
func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
211-
visited := make(map[string]bool)
212-
set.Visit(func(f *flag.Flag) {
213-
visited[f.Name] = true
214-
})
215-
for _, f := range flags {
216-
parts := f.Names()
217-
if len(parts) == 1 {
218-
continue
219-
}
220-
var ff *flag.Flag
221-
for _, name := range parts {
222-
name = strings.Trim(name, " ")
223-
if visited[name] {
224-
if ff != nil {
225-
return errors.New("Cannot use two forms of the same flag: " + name + " " + ff.Name)
226-
}
227-
ff = set.Lookup(name)
228-
}
229-
}
230-
if ff == nil {
231-
continue
232-
}
233-
for _, name := range parts {
234-
name = strings.Trim(name, " ")
235-
if !visited[name] {
236-
copyFlag(name, ff, set)
237-
}
238-
}
239-
}
240-
return nil
241-
}
242-
243200
func visibleFlags(fl []Flag) []Flag {
244201
var visible []Flag
245202
for _, f := range fl {

flag_test.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,38 @@ func TestBoolFlagCountFromCommand(t *testing.T) {
8383
expectedCount int
8484
}{
8585
{
86-
input: []string{"-tf", "-w", "-huh"},
86+
input: []string{"main", "-tf", "-w", "-huh"},
8787
expectedVal: true,
8888
expectedCount: 3,
8989
},
9090
{
91-
input: []string{},
91+
input: []string{"main", "-huh"},
92+
expectedVal: true,
93+
expectedCount: 1,
94+
},
95+
{
96+
input: []string{"main"},
9297
expectedVal: false,
9398
expectedCount: 0,
9499
},
95100
}
96101

102+
bf := &BoolFlag{Name: "tf", Aliases: []string{"w", "huh"}}
97103
for _, bct := range boolCountTests {
98-
set := flag.NewFlagSet("test", 0)
99-
cmd := &Command{flagSet: set}
100-
tf := &BoolFlag{Name: "tf", Aliases: []string{"w", "huh"}}
104+
cmd := &Command{
105+
Flags: []Flag{
106+
bf,
107+
},
108+
}
101109
r := require.New(t)
102110

103-
r.NoError(tf.Apply(set))
104-
r.NoError(set.Parse(bct.input))
111+
r.NoError(cmd.Run(buildTestContext(t), bct.input))
105112

106-
r.Equal(bct.expectedVal, tf.Get(cmd))
107-
r.Equal(bct.expectedCount, cmd.Count("tf"))
113+
r.Equal(bct.expectedVal, cmd.Value(bf.Name))
114+
r.Equal(bct.expectedCount, cmd.Count(bf.Name))
115+
for _, alias := range bf.Aliases {
116+
r.Equal(bct.expectedCount, cmd.Count(alias))
117+
}
108118
}
109119
}
110120

0 commit comments

Comments
 (0)