Implement required flags
This commit is contained in:
committed by
Some DATDevs
parent
9908e96513
commit
7e05320026
18
context.go
18
context.go
@@ -3,6 +3,7 @@ package cli
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -337,3 +338,20 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkRequiredFlags(flags []Flag, set *flag.FlagSet) error {
|
||||
visited := make(map[string]bool)
|
||||
set.Visit(func(f *flag.Flag) {
|
||||
visited[f.Name] = true
|
||||
})
|
||||
|
||||
for _, f := range flags {
|
||||
if f.IsRequired() {
|
||||
key := strings.Split(f.getName(), ",")[0]
|
||||
if !visited[key] {
|
||||
return fmt.Errorf("Required flag %s not set", f.getName())
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user