adding in boolean alias support

This commit is contained in:
James He 2022-08-11 15:49:12 -05:00 committed by Dan Buch
parent 909232502b
commit e0db267492
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 26 additions and 7 deletions

View File

@ -121,13 +121,19 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC
// ApplyInputSourceValue applies a Bool value to the flagSet if required
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.BoolFlag.Name) {
value, err := isc.Bool(f.BoolFlag.Name)
if err != nil {
return err
}
for _, name := range f.Names() {
_ = f.set.Set(name, strconv.FormatBool(value))
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
for _, name := range f.BoolFlag.Names() {
if isc.isSet(name) {
value, err := isc.Bool(name)
if err != nil {
return err
}
if value {
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatBool(value))
}
}
}
}
}
return nil

View File

@ -178,6 +178,19 @@ func TestBoolApplyInputSourceMethodSet(t *testing.T) {
refute(t, true, c.Bool("test"))
}
func TestBoolApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test", Aliases: []string{"test_alias"}}),
FlagName: "test_alias",
MapValue: true,
}
c := runTest(t, tis)
expect(t, true, c.Bool("test_alias"))
c = runRacyTest(t, tis)
refute(t, true, c.Bool("test_alias"))
}
func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),