@@ -238,7 +238,12 @@ func TestFlagsFromEnv(t *testing.T) {
238238 errContains : `could not parse "foobar" as []float64 value from environment ` +
239239 `variable "SECONDS" for flag seconds:` ,
240240 },
241-
241+ {
242+ name : "Generic" ,
243+ input : "foo,bar" ,
244+ output : & Parser {"foo" , "bar" },
245+ fl : & GenericFlag {Name : "names" , Value : & Parser {}, Sources : EnvVars ("NAMES" )},
246+ },
242247 {
243248 name : "IntSliceFlag valid" ,
244249 input : "1,2" ,
@@ -461,6 +466,16 @@ func TestFlagStringifying(t *testing.T) {
461466 fl : & FloatSliceFlag {Name : "pepperonis" , DefaultText : "shaved" },
462467 expected : "--pepperonis value [ --pepperonis value ]\t (default: shaved)" ,
463468 },
469+ {
470+ name : "generic-flag" ,
471+ fl : & GenericFlag {Name : "yogurt" },
472+ expected : "--yogurt value\t " ,
473+ },
474+ {
475+ name : "generic-flag-with-default-text" ,
476+ fl : & GenericFlag {Name : "ricotta" , DefaultText : "plops" },
477+ expected : "--ricotta value\t (default: plops)" ,
478+ },
464479 {
465480 name : "int-flag" ,
466481 fl : & IntFlag {Name : "grubs" },
@@ -1558,6 +1573,84 @@ func TestFloat64SliceFlagApply_ParentCommand(t *testing.T) {
15581573 }).Run (buildTestContext (t ), []string {"run" , "child" })
15591574}
15601575
1576+ var genericFlagTests = []struct {
1577+ name string
1578+ value Value
1579+ expected string
1580+ }{
1581+ {"toads" , & Parser {"abc" , "def" }, "--toads value\t test flag (default: abc,def)" },
1582+ {"t" , & Parser {"abc" , "def" }, "-t value\t test flag (default: abc,def)" },
1583+ }
1584+
1585+ func TestGenericFlagHelpOutput (t * testing.T ) {
1586+ for _ , test := range genericFlagTests {
1587+ fl := & GenericFlag {Name : test .name , Value : test .value , Usage : "test flag" }
1588+ // create a temporary flag set to apply
1589+ tfs := flag .NewFlagSet ("test" , 0 )
1590+ assert .NoError (t , fl .Apply (tfs ))
1591+ assert .Equal (t , test .expected , fl .String ())
1592+ }
1593+ }
1594+
1595+ func TestGenericFlagWithEnvVarHelpOutput (t * testing.T ) {
1596+ defer resetEnv (os .Environ ())
1597+ os .Clearenv ()
1598+ _ = os .Setenv ("APP_ZAP" , "3" )
1599+
1600+ for _ , test := range genericFlagTests {
1601+ fl := & GenericFlag {Name : test .name , Sources : EnvVars ("APP_ZAP" )}
1602+ output := fl .String ()
1603+
1604+ expectedSuffix := withEnvHint ([]string {"APP_ZAP" }, "" )
1605+ if ! strings .HasSuffix (output , expectedSuffix ) {
1606+ t .Errorf ("%s does not end with" + expectedSuffix , output )
1607+ }
1608+ }
1609+ }
1610+
1611+ func TestGenericFlagApply_SetsAllNames (t * testing.T ) {
1612+ fl := GenericFlag {Name : "orbs" , Aliases : []string {"O" , "obrs" }, Value : & Parser {}}
1613+ set := flag .NewFlagSet ("test" , 0 )
1614+ assert .NoError (t , fl .Apply (set ))
1615+ assert .NoError (t , set .Parse ([]string {"--orbs" , "eleventy,3" , "-O" , "4,bloop" , "--obrs" , "19,s" }))
1616+ }
1617+
1618+ func TestGenericFlagValueFromCommand (t * testing.T ) {
1619+ cmd := & Command {
1620+ Name : "foo" ,
1621+ Flags : []Flag {
1622+ & GenericFlag {Name : "myflag" , Value : & Parser {}},
1623+ },
1624+ }
1625+
1626+ assert .NoError (t , cmd .Run (buildTestContext (t ), []string {"foo" , "--myflag" , "abc,def" }))
1627+ assert .Equal (t , & Parser {"abc" , "def" }, cmd .Generic ("myflag" ))
1628+ }
1629+
1630+ func TestParseGenericFromEnv (t * testing.T ) {
1631+ t .Setenv ("APP_SERVE" , "20,30" )
1632+ cmd := & Command {
1633+ Flags : []Flag {
1634+ & GenericFlag {
1635+ Name : "serve" ,
1636+ Aliases : []string {"s" },
1637+ Value : & Parser {},
1638+ Sources : EnvVars ("APP_SERVE" ),
1639+ },
1640+ },
1641+ Action : func (ctx context.Context , cmd * Command ) error {
1642+ if ! reflect .DeepEqual (cmd .Generic ("serve" ), & Parser {"20" , "30" }) {
1643+ t .Errorf ("main name not set from env" )
1644+ }
1645+ if ! reflect .DeepEqual (cmd .Generic ("s" ), & Parser {"20" , "30" }) {
1646+ t .Errorf ("short name not set from env" )
1647+ }
1648+ return nil
1649+ },
1650+ }
1651+ assert .NoError (t , cmd .Run (buildTestContext (t ), []string {"run" }))
1652+ }
1653+
15611654func TestParseMultiString (t * testing.T ) {
15621655 _ = (& Command {
15631656 Flags : []Flag {
@@ -2756,6 +2849,16 @@ func TestFlagDefaultValueWithEnv(t *testing.T) {
27562849 "ssflag" : "some-other-env_value=" ,
27572850 },
27582851 },
2852+ // TODO
2853+ /*{
2854+ name: "generic",
2855+ flag: &GenericFlag{Name: "flag", Value: &Parser{"11", "12"}, Sources: EnvVars("gflag")},
2856+ toParse: []string{"--flag", "15,16"},
2857+ expect: `--flag value (default: 11,12)` + withEnvHint([]string{"gflag"}, ""),
2858+ environ: map[string]string{
2859+ "gflag": "13,14",
2860+ },
2861+ },*/
27592862 }
27602863 for _ , v := range cases {
27612864 for key , val := range v .environ {
@@ -3133,3 +3236,34 @@ func TestDocGetValue(t *testing.T) {
31333236 assert .Equal (t , "" , (& BoolFlag {Name : "foo" , Value : false }).GetValue ())
31343237 assert .Equal (t , "bar" , (& StringFlag {Name : "foo" , Value : "bar" }).GetValue ())
31353238}
3239+
3240+ func TestGenericFlag_SatisfiesFlagInterface (t * testing.T ) {
3241+ var f Flag = & GenericFlag {}
3242+
3243+ _ = f .IsSet ()
3244+ _ = f .Names ()
3245+ }
3246+
3247+ func TestGenericFlag_SatisfiesFmtStringerInterface (t * testing.T ) {
3248+ var f fmt.Stringer = & GenericFlag {}
3249+
3250+ _ = f .String ()
3251+ }
3252+
3253+ func TestGenericFlag_SatisfiesRequiredFlagInterface (t * testing.T ) {
3254+ var f RequiredFlag = & GenericFlag {}
3255+
3256+ _ = f .IsRequired ()
3257+ }
3258+
3259+ func TestGenericFlag_SatisfiesVisibleFlagInterface (t * testing.T ) {
3260+ var f VisibleFlag = & GenericFlag {}
3261+
3262+ _ = f .IsVisible ()
3263+ }
3264+
3265+ func TestGenericFlag_SatisfiesDocFlagInterface (t * testing.T ) {
3266+ var f DocGenerationFlag = & GenericFlag {}
3267+
3268+ _ = f .GetUsage ()
3269+ }
0 commit comments