@@ -912,13 +912,13 @@ func TestCommand_FlagsFromExtPackage(t *testing.T) {
912912
913913func TestCommand_Setup_defaultsReader (t * testing.T ) {
914914 cmd := & Command {}
915- cmd .setupDefaults ([]string {"cli. test" })
915+ cmd .setupDefaults ([]string {"test" })
916916 assert .Equal (t , cmd .Reader , os .Stdin )
917917}
918918
919919func TestCommand_Setup_defaultsWriter (t * testing.T ) {
920920 cmd := & Command {}
921- cmd .setupDefaults ([]string {"cli. test" })
921+ cmd .setupDefaults ([]string {"test" })
922922 assert .Equal (t , cmd .Writer , os .Stdout )
923923}
924924
@@ -1026,7 +1026,7 @@ func TestCommand_VisibleCommands(t *testing.T) {
10261026 },
10271027 }
10281028
1029- cmd .setupDefaults ([]string {"cli. test" })
1029+ cmd .setupDefaults ([]string {"test" })
10301030 expected := []* Command {
10311031 cmd .Commands [0 ],
10321032 }
@@ -1302,14 +1302,14 @@ func TestCommand_ParseSliceFlagsWithMissingValue(t *testing.T) {
13021302
13031303func TestCommand_DefaultStdin (t * testing.T ) {
13041304 cmd := & Command {}
1305- cmd .setupDefaults ([]string {"cli. test" })
1305+ cmd .setupDefaults ([]string {"test" })
13061306
13071307 assert .Equal (t , cmd .Reader , os .Stdin , "Default input reader not set." )
13081308}
13091309
13101310func TestCommand_DefaultStdout (t * testing.T ) {
13111311 cmd := & Command {}
1312- cmd .setupDefaults ([]string {"cli. test" })
1312+ cmd .setupDefaults ([]string {"test" })
13131313
13141314 assert .Equal (t , cmd .Writer , os .Stdout , "Default output writer not set." )
13151315}
@@ -2215,7 +2215,7 @@ func TestCommand_VisibleCategories(t *testing.T) {
22152215 },
22162216 }
22172217
2218- cmd .setupDefaults ([]string {"cli. test" })
2218+ cmd .setupDefaults ([]string {"test" })
22192219 assert .Equal (t , expected , cmd .VisibleCategories ())
22202220
22212221 cmd = & Command {
@@ -2248,7 +2248,7 @@ func TestCommand_VisibleCategories(t *testing.T) {
22482248 },
22492249 }
22502250
2251- cmd .setupDefaults ([]string {"cli. test" })
2251+ cmd .setupDefaults ([]string {"test" })
22522252 assert .Equal (t , expected , cmd .VisibleCategories ())
22532253
22542254 cmd = & Command {
@@ -2273,7 +2273,7 @@ func TestCommand_VisibleCategories(t *testing.T) {
22732273 },
22742274 }
22752275
2276- cmd .setupDefaults ([]string {"cli. test" })
2276+ cmd .setupDefaults ([]string {"test" })
22772277 assert .Empty (t , cmd .VisibleCategories ())
22782278}
22792279
@@ -2554,7 +2554,7 @@ func buildMinimalTestCommand() *Command {
25542554func TestSetupInitializesBothWriters (t * testing.T ) {
25552555 cmd := & Command {}
25562556
2557- cmd .setupDefaults ([]string {"cli. test" })
2557+ cmd .setupDefaults ([]string {"test" })
25582558
25592559 assert .Equal (t , cmd .ErrWriter , os .Stderr , "expected a.ErrWriter to be os.Stderr" )
25602560 assert .Equal (t , cmd .Writer , os .Stdout , "expected a.Writer to be os.Stdout" )
@@ -2566,7 +2566,7 @@ func TestSetupInitializesOnlyNilWriters(t *testing.T) {
25662566 ErrWriter : wr ,
25672567 }
25682568
2569- cmd .setupDefaults ([]string {"cli. test" })
2569+ cmd .setupDefaults ([]string {"test" })
25702570
25712571 assert .Equal (t , cmd .ErrWriter , wr , "expected a.ErrWriter to be a *bytes.Buffer instance" )
25722572 assert .Equal (t , cmd .Writer , os .Stdout , "expected a.Writer to be os.Stdout" )
@@ -4250,6 +4250,255 @@ func TestCommandSliceFlagSeparator(t *testing.T) {
42504250 r .Equal ([]string {"ff" , "dd" , "gg" , "t,u" }, cmd .Value ("foo" ))
42514251}
42524252
4253+ // TestStringFlagTerminator tests the string flag "--flag" with "--" terminator.
4254+ func TestStringFlagTerminator (t * testing.T ) {
4255+ tests := []struct {
4256+ name string
4257+ input []string
4258+ expectFlag string
4259+ expectArgs []string
4260+ expectErr bool
4261+ errorContain string
4262+ }{
4263+ {
4264+ name : "flag and args after terminator" ,
4265+ input : []string {"test" , "--flag" , "x" , "--" , "test" , "a1" , "a2" , "a3" },
4266+ expectFlag : "x" ,
4267+ expectArgs : []string {"test" , "a1" , "a2" , "a3" },
4268+ },
4269+ /* {
4270+ name: "missing flag value due to terminator",
4271+ input: []string{"test", "--flag", "--", "x"},
4272+ expectErr: true,
4273+ errorContain: "flag needs an argument",
4274+ },*/
4275+ {
4276+ name : "terminator with no trailing args" ,
4277+ input : []string {"test" , "--flag" , "x" , "--" },
4278+ expectFlag : "x" ,
4279+ expectArgs : []string {},
4280+ },
4281+ {
4282+ name : "no terminator, only flag" ,
4283+ input : []string {"test" , "--flag" , "x" },
4284+ expectFlag : "x" ,
4285+ expectArgs : []string {},
4286+ },
4287+ {
4288+ name : "flag defined after --" ,
4289+ input : []string {"test" , "--" , "x" , "--flag=value" },
4290+ expectFlag : "" ,
4291+ expectArgs : []string {"x" , "--flag=value" },
4292+ },
4293+ {
4294+ name : "flag and without --" ,
4295+ input : []string {"test" , "--flag" , "value" , "x" },
4296+ expectFlag : "value" ,
4297+ expectArgs : []string {"x" },
4298+ },
4299+ }
4300+
4301+ for _ , tc := range tests {
4302+
4303+ t .Run (tc .name , func (t * testing.T ) {
4304+ var flagVal string
4305+ var argsVal []string
4306+
4307+ // build minimal command with a StringFlag "flag"
4308+ cmd := & Command {
4309+ Name : "test" ,
4310+ Flags : []Flag {
4311+ & StringFlag {
4312+ Name : "flag" ,
4313+ Usage : "a string flag" ,
4314+ Destination : & flagVal ,
4315+ },
4316+ },
4317+ Action : func (ctx context.Context , c * Command ) error {
4318+ argsVal = c .Args ().Slice ()
4319+ return nil
4320+ },
4321+ }
4322+
4323+ err := cmd .Run (context .Background (), tc .input )
4324+ if tc .expectErr {
4325+ assert .Error (t , err )
4326+ if err != nil {
4327+ assert .Contains (t , strings .ToLower (err .Error ()), strings .ToLower (tc .errorContain ))
4328+ }
4329+ } else {
4330+ assert .NoError (t , err )
4331+ assert .Equal (t , tc .expectFlag , flagVal )
4332+ assert .Equal (t , tc .expectArgs , argsVal )
4333+ }
4334+ })
4335+ }
4336+ }
4337+
4338+ // TestBoolFlagTerminator tests the bool flag
4339+ func TestBoolFlagTerminator (t * testing.T ) {
4340+ tests := []struct {
4341+ name string
4342+ input []string
4343+ expectFlag bool
4344+ expectArgs []string
4345+ expectErr bool
4346+ errorContain string
4347+ }{
4348+ /*{
4349+ name: "bool flag with invalid non-bool value",
4350+ input: []string{"test", "--flag", "x", "--", "test", "a1", "a2", "a3"},
4351+ expectErr: true,
4352+ errorContain: "invalid syntax",
4353+ },*/
4354+ {
4355+ name : "bool flag omitted value defaults to true" ,
4356+ input : []string {"test" , "--flag" , "--" , "x" },
4357+ expectFlag : true ,
4358+ expectArgs : []string {"x" },
4359+ },
4360+ {
4361+ name : "bool flag explicitly set to false" ,
4362+ input : []string {"test" , "--flag=false" , "--" , "x" },
4363+ expectFlag : false ,
4364+ expectArgs : []string {"x" },
4365+ },
4366+ {
4367+ name : "bool flag defined after --" ,
4368+ input : []string {"test" , "--" , "x" , "--flag=true" },
4369+ expectFlag : false ,
4370+ expectArgs : []string {"x" , "--flag=true" },
4371+ },
4372+ {
4373+ name : "bool flag and without --" ,
4374+ input : []string {"test" , "--flag=true" , "x" },
4375+ expectFlag : true ,
4376+ expectArgs : []string {"x" },
4377+ },
4378+ }
4379+
4380+ for _ , tc := range tests {
4381+ t .Run (tc .name , func (t * testing.T ) {
4382+ var flagVal bool
4383+ var argsVal []string
4384+
4385+ // build minimal command with a BoolFlag "flag"
4386+ cmd := & Command {
4387+ Name : "test" ,
4388+ Flags : []Flag {
4389+ & BoolFlag {
4390+ Name : "flag" ,
4391+ Usage : "a bool flag" ,
4392+ Destination : & flagVal ,
4393+ },
4394+ },
4395+ Action : func (ctx context.Context , c * Command ) error {
4396+ argsVal = c .Args ().Slice ()
4397+ return nil
4398+ },
4399+ }
4400+
4401+ err := cmd .Run (context .Background (), tc .input )
4402+ if tc .expectErr {
4403+ assert .Error (t , err )
4404+ if err != nil {
4405+ assert .Contains (t , strings .ToLower (err .Error ()), strings .ToLower (tc .errorContain ))
4406+ }
4407+ } else {
4408+ assert .NoError (t , err )
4409+ assert .Equal (t , tc .expectFlag , flagVal )
4410+ assert .Equal (t , tc .expectArgs , argsVal )
4411+ }
4412+ })
4413+ }
4414+ }
4415+
4416+ // TestSliceStringFlagParsing tests the StringSliceFlag
4417+ func TestSliceStringFlagParsing (t * testing.T ) {
4418+ var sliceVal []string
4419+
4420+ cmdNoDelimiter := & Command {
4421+ Name : "test" ,
4422+ Flags : []Flag {
4423+ & StringSliceFlag {
4424+ Name : "flag" ,
4425+ Usage : "a string slice flag without delimiter" ,
4426+ },
4427+ },
4428+ Action : func (ctx context.Context , c * Command ) error {
4429+ sliceVal = c .StringSlice ("flag" )
4430+ return nil
4431+ },
4432+ }
4433+
4434+ /*cmdWithDelimiter := &Command{
4435+ Name: "test",
4436+ Flags: []Flag{
4437+ &StringSliceFlag{
4438+ Name: "flag",
4439+ Usage: "a string slice flag with delimiter",
4440+ Delimiter: ':',
4441+ },
4442+ },
4443+ Action: func(ctx context.Context, c *Command) error {
4444+ sliceVal = c.StringSlice("flag")
4445+ return nil
4446+ },
4447+ }*/
4448+
4449+ tests := []struct {
4450+ name string
4451+ cmd * Command
4452+ input []string
4453+ expectSlice []string
4454+ expectErr bool
4455+ errorContain string
4456+ }{
4457+ {
4458+ name : "single value without delimiter (no split)" ,
4459+ cmd : cmdNoDelimiter ,
4460+ input : []string {"test" , "--flag" , "x" },
4461+ expectSlice : []string {"x" },
4462+ },
4463+ {
4464+ name : "multiple values with comma (default split)" ,
4465+ cmd : cmdNoDelimiter ,
4466+ input : []string {"test" , "--flag" , "x,y" },
4467+ expectSlice : []string {"x" , "y" },
4468+ },
4469+ /*{
4470+ name: "Case 10: with delimiter specified ':'",
4471+ cmd: cmdWithDelimiter,
4472+ input: []string{"test", "--flag", "x:y"},
4473+ expectSlice: []string{"x", "y"},
4474+ },*/
4475+ {
4476+ name : "without delimiter specified, value remains unsplit" ,
4477+ cmd : cmdNoDelimiter ,
4478+ input : []string {"test" , "--flag" , "x:y" },
4479+ expectSlice : []string {"x:y" },
4480+ },
4481+ }
4482+
4483+ for _ , tc := range tests {
4484+ // Reset sliceVal
4485+ sliceVal = nil
4486+
4487+ t .Run (tc .name , func (t * testing.T ) {
4488+ err := tc .cmd .Run (context .Background (), tc .input )
4489+ if tc .expectErr {
4490+ assert .Error (t , err )
4491+ if err != nil {
4492+ assert .Contains (t , strings .ToLower (err .Error ()), strings .ToLower (tc .errorContain ))
4493+ }
4494+ } else {
4495+ assert .NoError (t , err )
4496+ assert .Equal (t , tc .expectSlice , sliceVal )
4497+ }
4498+ })
4499+ }
4500+ }
4501+
42534502func TestJSONExportCommand (t * testing.T ) {
42544503 cmd := buildExtendedTestCommand ()
42554504 cmd .Arguments = []Argument {
0 commit comments