backwards compatible RequiredFlag implementation
This commit is contained in:
parent
922d231891
commit
6a2ae78373
10
context.go
10
context.go
@ -301,10 +301,12 @@ func checkRequiredFlags(flags []Flag, set *flag.FlagSet) error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
for _, f := range flags {
|
for _, f := range flags {
|
||||||
if f.IsRequired() {
|
if rf, ok := f.(RequiredFlag); ok {
|
||||||
key := strings.Split(f.GetName(), ",")[0]
|
if rf.IsRequired() {
|
||||||
if !visited[key] {
|
key := strings.Split(f.GetName(), ",")[0]
|
||||||
return fmt.Errorf("Required flag %s not set", f.GetName())
|
if !visited[key] {
|
||||||
|
return fmt.Errorf("Required flag %s not set", f.GetName())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
9
flag.go
9
flag.go
@ -72,10 +72,17 @@ type Flag interface {
|
|||||||
fmt.Stringer
|
fmt.Stringer
|
||||||
// Apply Flag settings to the given flag set
|
// Apply Flag settings to the given flag set
|
||||||
Apply(*flag.FlagSet)
|
Apply(*flag.FlagSet)
|
||||||
IsRequired() bool
|
|
||||||
GetName() string
|
GetName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RequiredFlag is an interface that allows us to return mark flags as required
|
||||||
|
// it allows flags defined in this library to be marked as required in a backwards compatible fashion
|
||||||
|
type RequiredFlag interface {
|
||||||
|
Flag
|
||||||
|
|
||||||
|
IsRequired() bool
|
||||||
|
}
|
||||||
|
|
||||||
// errorableFlag is an interface that allows us to return errors during apply
|
// errorableFlag is an interface that allows us to return errors during apply
|
||||||
// it allows flags defined in this library to return errors in a fashion backwards compatible
|
// it allows flags defined in this library to return errors in a fashion backwards compatible
|
||||||
// TODO remove in v2 and modify the existing Flag interface to return errors
|
// TODO remove in v2 and modify the existing Flag interface to return errors
|
||||||
|
Loading…
Reference in New Issue
Block a user