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