summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIlja Neumann <[email protected]>2023-06-29 21:26:34 +0200
committerIlja Neumann <[email protected]>2023-06-29 21:26:34 +0200
commit259c83fd5aeb44fbb67a183cf09b4ca16d9b30e2 (patch)
tree2116619d5cabf6e1c43c698f502da42dad1e4df6
parent18623d869bfb7aa7b87eda2c8dc7d1bf6149f316 (diff)
Remove usage of additional envOnly struct variable
-rw-r--r--parse.go38
1 files changed, 5 insertions, 33 deletions
diff --git a/parse.go b/parse.go
index a16485b..63cfab3 100644
--- a/parse.go
+++ b/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 {