@@ -1328,6 +1328,100 @@ func TestCommand_SingleRuneUnicodeFlag(t *testing.T) {
13281328 assert .Equal (t , []string {"operand" }, args )
13291329}
13301330
1331+ func TestCommand_DigitFlag (t * testing.T ) {
1332+ tests := []struct {
1333+ name string
1334+ shortOptions bool
1335+ args []string
1336+ expectedFour bool
1337+ expectedSix bool
1338+ expectedLevel string
1339+ expectedArgs []string
1340+ }{
1341+ {
1342+ name : "single digit flag" ,
1343+ args : []string {"app" , "-4" , "operand" },
1344+ expectedFour : true ,
1345+ expectedArgs : []string {"operand" },
1346+ },
1347+ {
1348+ name : "digit flags followed by another flag" ,
1349+ args : []string {"app" , "-4" , "-6" , "-level" , "high" , "operand" },
1350+ expectedFour : true ,
1351+ expectedSix : true ,
1352+ expectedLevel : "high" ,
1353+ expectedArgs : []string {"operand" },
1354+ },
1355+ {
1356+ name : "flag name starting with a digit" ,
1357+ args : []string {"app" , "-2fa=on" , "operand" },
1358+ expectedLevel : "on" ,
1359+ expectedArgs : []string {"operand" },
1360+ },
1361+ {
1362+ name : "digit flags in a short option group" ,
1363+ shortOptions : true ,
1364+ args : []string {"app" , "-46" , "operand" },
1365+ expectedFour : true ,
1366+ expectedSix : true ,
1367+ expectedArgs : []string {"operand" },
1368+ },
1369+ {
1370+ name : "undefined digit flag is still a positional argument" ,
1371+ args : []string {"app" , "-5" , "-4" },
1372+ expectedArgs : []string {"-5" , "-4" },
1373+ },
1374+ }
1375+
1376+ for _ , test := range tests {
1377+ t .Run (test .name , func (t * testing.T ) {
1378+ var four , six bool
1379+ var level string
1380+ var args []string
1381+ cmd := & Command {
1382+ Name : "app" ,
1383+ UseShortOptionHandling : test .shortOptions ,
1384+ Flags : []Flag {
1385+ & BoolFlag {Name : "4" },
1386+ & BoolFlag {Name : "6" },
1387+ & StringFlag {Name : "level" , Aliases : []string {"2fa" }},
1388+ },
1389+ Action : func (_ context.Context , cmd * Command ) error {
1390+ four = cmd .Bool ("4" )
1391+ six = cmd .Bool ("6" )
1392+ level = cmd .String ("level" )
1393+ args = cmd .Args ().Slice ()
1394+ return nil
1395+ },
1396+ }
1397+
1398+ require .NoError (t , cmd .Run (buildTestContext (t ), test .args ))
1399+ assert .Equal (t , test .expectedFour , four )
1400+ assert .Equal (t , test .expectedSix , six )
1401+ assert .Equal (t , test .expectedLevel , level )
1402+ assert .Equal (t , test .expectedArgs , args )
1403+ })
1404+ }
1405+ }
1406+
1407+ func TestCommand_NegativeNumberArg_InvalidFlagAccessHandler (t * testing.T ) {
1408+ for _ , shortOptions := range []bool {false , true } {
1409+ t .Run (fmt .Sprintf ("shortOptions=%v" , shortOptions ), func (t * testing.T ) {
1410+ var invalid []string
1411+ cmd := & Command {
1412+ UseShortOptionHandling : shortOptions ,
1413+ InvalidFlagAccessHandler : func (_ context.Context , _ * Command , name string ) {
1414+ invalid = append (invalid , name )
1415+ },
1416+ Action : func (context.Context , * Command ) error { return nil },
1417+ }
1418+
1419+ require .NoError (t , cmd .Run (buildTestContext (t ), []string {"app" , "-5" }))
1420+ assert .Empty (t , invalid )
1421+ })
1422+ }
1423+ }
1424+
13311425func TestCommand_EqualsFlagValueValidation (t * testing.T ) {
13321426 for _ , kind := range []string {"int" , "bool" } {
13331427 for _ , whitespace := range []string {"" , " " , "\t " } {
0 commit comments