summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go30
1 files changed, 13 insertions, 17 deletions
diff --git a/parse.go b/parse.go
index 5308d70..22729e6 100644
--- a/parse.go
+++ b/parse.go
@@ -47,10 +47,9 @@ func (p path) Child(f reflect.StructField) path {
// spec represents a command line option
type spec struct {
dest path
- typ reflect.Type
- name string // canonical name for the option
- long string
- short string
+ field reflect.StructField // name of struct field from this this option was created
+ long string // the --long form for this option, or empty if none
+ short string // the -s short form for this option, or empty if none
multiple bool
required bool
positional bool
@@ -276,13 +275,11 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
// duplicate the entire path to avoid slice overwrites
subdest := dest.Child(field)
spec := spec{
- dest: subdest,
- long: strings.ToLower(field.Name),
- typ: field.Type,
+ dest: subdest,
+ field: field,
+ long: strings.ToLower(field.Name),
}
- spec.name = spec.long
-
help, exists := field.Tag.Lookup("help")
if exists {
spec.help = help
@@ -311,9 +308,6 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
errs = append(errs, fmt.Sprintf("%s.%s: too many hyphens", t.Name(), field.Name))
case strings.HasPrefix(key, "--"):
spec.long = key[2:]
- if spec.long != "" {
- spec.name = spec.long
- }
case strings.HasPrefix(key, "-"):
if len(key) != 2 {
errs = append(errs, fmt.Sprintf("%s.%s: short arguments must be one character only",
@@ -369,8 +363,10 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) {
placeholder, hasPlaceholder := field.Tag.Lookup("placeholder")
if hasPlaceholder {
spec.placeholder = placeholder
+ } else if spec.long != "" {
+ spec.placeholder = strings.ToUpper(spec.long)
} else {
- spec.placeholder = strings.ToUpper(spec.name)
+ spec.placeholder = strings.ToUpper(spec.field.Name)
}
// Check whether this field is supported. It's good to do this here rather than
@@ -598,7 +594,7 @@ func (p *Parser) process(args []string) error {
if i+1 == len(args) {
return fmt.Errorf("missing value for %s", arg)
}
- if !nextIsNumeric(spec.typ, args[i+1]) && isFlag(args[i+1]) {
+ if !nextIsNumeric(spec.field.Type, args[i+1]) && isFlag(args[i+1]) {
return fmt.Errorf("missing value for %s", arg)
}
value = args[i+1]
@@ -623,13 +619,13 @@ func (p *Parser) process(args []string) error {
if spec.multiple {
err := setSlice(p.val(spec.dest), positionals, true)
if err != nil {
- return fmt.Errorf("error processing %s: %v", spec.name, err)
+ return fmt.Errorf("error processing %s: %v", spec.field.Name, err)
}
positionals = nil
} else {
err := scalar.ParseValue(p.val(spec.dest), positionals[0])
if err != nil {
- return fmt.Errorf("error processing %s: %v", spec.name, err)
+ return fmt.Errorf("error processing %s: %v", spec.field.Name, err)
}
positionals = positionals[1:]
}
@@ -644,7 +640,7 @@ func (p *Parser) process(args []string) error {
continue
}
- name := spec.name
+ name := strings.ToLower(spec.field.Name)
if spec.long != "" && !spec.positional {
name = "--" + spec.long
}