Remove usage of additional envOnly struct variable

This commit is contained in:
Ilja Neumann 2023-06-29 21:26:34 +02:00
parent 18623d869b
commit 259c83fd5a
1 changed files with 5 additions and 33 deletions

View File

@ -54,7 +54,6 @@ type spec struct {
separate bool // if true, each slice and map entry will have its own --flag separate bool // if true, each slice and map entry will have its own --flag
help string // the help text for this option help string // the help text for this option
env string // the name of the environment variable for this option, or empty for none 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 defaultValue reflect.Value // default value for this option
defaultString string // default value for this option, in string form to be displayed in help text defaultString string // default value for this option, in string form to be displayed in help text
placeholder string // name of the data in help placeholder string // name of the data in help
@ -344,9 +343,8 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
// Look at the tag // Look at the tag
var isSubcommand bool // tracks whether this field is a subcommand 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 == "" { if key == "" {
continue continue
} }
@ -418,16 +416,6 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
spec.placeholder = strings.ToUpper(spec.field.Name) 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 this is a subcommand then we've done everything we need to do
if isSubcommand { if isSubcommand {
return false return false
@ -763,7 +751,7 @@ func (p *Parser) process(args []string) error {
} }
if spec.required { if spec.required {
if spec.envOnly { if spec.short == "" && spec.long == "" {
msg := fmt.Sprintf("environment variable %s is required", spec.env) msg := fmt.Sprintf("environment variable %s is required", spec.env)
return errors.New(msg) return errors.New(msg)
} }
@ -807,22 +795,6 @@ func isFlag(s string) bool {
return strings.HasPrefix(s, "-") && strings.TrimLeft(s, "-") != "" 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 // val returns a reflect.Value corresponding to the current value for the
// given path // given path
func (p *Parser) val(dest path) reflect.Value { func (p *Parser) val(dest path) reflect.Value {