replace "name" and "typ" by storing the original StructField

This commit is contained in:
Alex Flint 2021-01-31 19:40:38 -08:00
parent 2a91531140
commit efe5cdf4da
1 changed files with 13 additions and 17 deletions

View File

@ -47,10 +47,9 @@ func (p path) Child(f reflect.StructField) path {
// spec represents a command line option // spec represents a command line option
type spec struct { type spec struct {
dest path dest path
typ reflect.Type field reflect.StructField // name of struct field from this this option was created
name string // canonical name for the option long string // the --long form for this option, or empty if none
long string short string // the -s short form for this option, or empty if none
short string
multiple bool multiple bool
required bool required bool
positional 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 // duplicate the entire path to avoid slice overwrites
subdest := dest.Child(field) subdest := dest.Child(field)
spec := spec{ spec := spec{
dest: subdest, dest: subdest,
long: strings.ToLower(field.Name), field: field,
typ: field.Type, long: strings.ToLower(field.Name),
} }
spec.name = spec.long
help, exists := field.Tag.Lookup("help") help, exists := field.Tag.Lookup("help")
if exists { if exists {
spec.help = help 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)) errs = append(errs, fmt.Sprintf("%s.%s: too many hyphens", t.Name(), field.Name))
case strings.HasPrefix(key, "--"): case strings.HasPrefix(key, "--"):
spec.long = key[2:] spec.long = key[2:]
if spec.long != "" {
spec.name = spec.long
}
case strings.HasPrefix(key, "-"): case strings.HasPrefix(key, "-"):
if len(key) != 2 { if len(key) != 2 {
errs = append(errs, fmt.Sprintf("%s.%s: short arguments must be one character only", 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") placeholder, hasPlaceholder := field.Tag.Lookup("placeholder")
if hasPlaceholder { if hasPlaceholder {
spec.placeholder = placeholder spec.placeholder = placeholder
} else if spec.long != "" {
spec.placeholder = strings.ToUpper(spec.long)
} else { } 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 // 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) { if i+1 == len(args) {
return fmt.Errorf("missing value for %s", arg) 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) return fmt.Errorf("missing value for %s", arg)
} }
value = args[i+1] value = args[i+1]
@ -623,13 +619,13 @@ func (p *Parser) process(args []string) error {
if spec.multiple { if spec.multiple {
err := setSlice(p.val(spec.dest), positionals, true) err := setSlice(p.val(spec.dest), positionals, true)
if err != nil { 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 positionals = nil
} else { } else {
err := scalar.ParseValue(p.val(spec.dest), positionals[0]) err := scalar.ParseValue(p.val(spec.dest), positionals[0])
if err != nil { 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:] positionals = positionals[1:]
} }
@ -644,7 +640,7 @@ func (p *Parser) process(args []string) error {
continue continue
} }
name := spec.name name := strings.ToLower(spec.field.Name)
if spec.long != "" && !spec.positional { if spec.long != "" && !spec.positional {
name = "--" + spec.long name = "--" + spec.long
} }