Remove usage of additional envOnly struct variable
This commit is contained in:
parent
18623d869b
commit
259c83fd5a
38
parse.go
38
parse.go
|
@ -54,10 +54,9 @@ type spec struct {
|
|||
separate bool // if true, each slice and map entry will have its own --flag
|
||||
help string // the help text for this option
|
||||
env string // the name of the environment variable for this option, or empty for none
|
||||
envOnly bool
|
||||
defaultValue reflect.Value // default value for this option
|
||||
defaultString string // default value for this option, in string form to be displayed in help text
|
||||
placeholder string // name of the data in help
|
||||
defaultValue reflect.Value // default value for this option
|
||||
defaultString string // default value for this option, in string form to be displayed in help text
|
||||
placeholder string // name of the data in help
|
||||
}
|
||||
|
||||
// command represents a named subcommand, or the top-level command
|
||||
|
@ -344,9 +343,8 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
|
|||
|
||||
// Look at the tag
|
||||
var isSubcommand bool // tracks whether this field is a subcommand
|
||||
kvPairs := strings.Split(tag, ",")
|
||||
|
||||
for _, key := range kvPairs {
|
||||
for _, key := range strings.Split(tag, ",") {
|
||||
if key == "" {
|
||||
continue
|
||||
}
|
||||
|
@ -418,16 +416,6 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
|
|||
spec.placeholder = strings.ToUpper(spec.field.Name)
|
||||
}
|
||||
|
||||
noFormSpecs := emptyLongAndShort(kvPairs)
|
||||
|
||||
if spec.env == "" && noFormSpecs {
|
||||
errs = append(errs, fmt.Sprintf("%s.%s: short arguments must be one character only",
|
||||
t.Name(), field.Name))
|
||||
return false
|
||||
} else if spec.env != "" && noFormSpecs {
|
||||
spec.envOnly = true
|
||||
}
|
||||
|
||||
// if this is a subcommand then we've done everything we need to do
|
||||
if isSubcommand {
|
||||
return false
|
||||
|
@ -763,7 +751,7 @@ func (p *Parser) process(args []string) error {
|
|||
}
|
||||
|
||||
if spec.required {
|
||||
if spec.envOnly {
|
||||
if spec.short == "" && spec.long == "" {
|
||||
msg := fmt.Sprintf("environment variable %s is required", spec.env)
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
@ -807,22 +795,6 @@ func isFlag(s string) bool {
|
|||
return strings.HasPrefix(s, "-") && strings.TrimLeft(s, "-") != ""
|
||||
}
|
||||
|
||||
func emptyLongAndShort(kv []string) bool {
|
||||
var noShort, noLong bool
|
||||
for _, key := range kv {
|
||||
if key == "-" {
|
||||
noShort = true
|
||||
}
|
||||
|
||||
if key == "--" {
|
||||
noLong = true
|
||||
}
|
||||
}
|
||||
|
||||
return noShort && noLong
|
||||
|
||||
}
|
||||
|
||||
// val returns a reflect.Value corresponding to the current value for the
|
||||
// given path
|
||||
func (p *Parser) val(dest path) reflect.Value {
|
||||
|
|
Loading…
Reference in New Issue