Skip to content

Commit

Permalink
Remove usage of additional envOnly struct variable
Browse files Browse the repository at this point in the history
  • Loading branch information
IljaN committed Jun 29, 2023
1 parent 18623d8 commit 259c83f
Showing 1 changed file with 5 additions and 33 deletions.
38 changes: 5 additions & 33 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 259c83f

Please sign in to comment.