add environment variable support 🎉
This commit is contained in:
parent
f21b22dd90
commit
38f9e1622d
4
app.go
4
app.go
@ -228,7 +228,7 @@ func (a *App) Run(arguments []string) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
cerr := checkRequiredFlags(a.Flags, set)
|
||||
cerr := checkRequiredFlags(a.Flags, context)
|
||||
if cerr != nil {
|
||||
ShowAppHelp(context)
|
||||
return cerr
|
||||
@ -358,7 +358,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||
}
|
||||
}
|
||||
|
||||
cerr := checkRequiredFlags(a.Flags, set)
|
||||
cerr := checkRequiredFlags(a.Flags, context)
|
||||
if cerr != nil {
|
||||
ShowSubcommandHelp(context)
|
||||
return cerr
|
||||
|
@ -135,7 +135,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
cerr := checkRequiredFlags(c.Flags, set)
|
||||
cerr := checkRequiredFlags(c.Flags, context)
|
||||
if cerr != nil {
|
||||
ShowCommandHelp(context, c.Name)
|
||||
return cerr
|
||||
|
@ -309,17 +309,12 @@ func (e *errRequiredFlags) getMissingFlags() []string {
|
||||
return e.missingFlags
|
||||
}
|
||||
|
||||
func checkRequiredFlags(flags []Flag, set *flag.FlagSet) requiredFlagsErr {
|
||||
visited := make(map[string]bool)
|
||||
set.Visit(func(f *flag.Flag) {
|
||||
visited[f.Name] = true
|
||||
})
|
||||
|
||||
func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr {
|
||||
var missingFlags []string
|
||||
for _, f := range flags {
|
||||
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
|
||||
key := strings.Split(f.GetName(), ",")[0]
|
||||
if !visited[key] {
|
||||
if !context.IsSet(key) {
|
||||
missingFlags = append(missingFlags, key)
|
||||
}
|
||||
}
|
||||
|
@ -495,9 +495,11 @@ func TestCheckRequiredFlags(t *testing.T) {
|
||||
flags.Apply(set)
|
||||
}
|
||||
set.Parse(test.parseInput)
|
||||
ctx := &Context{}
|
||||
context := NewContext(ctx.App, set, ctx)
|
||||
|
||||
// logic under test
|
||||
err := checkRequiredFlags(test.flags, set)
|
||||
err := checkRequiredFlags(test.flags, context)
|
||||
|
||||
// assertions
|
||||
if test.expectedAnError && err == nil {
|
||||
|
Loading…
Reference in New Issue
Block a user