diff options
| author | Andrew Morozko <[email protected]> | 2020-12-20 02:54:03 +0300 |
|---|---|---|
| committer | Andrew Morozko <[email protected]> | 2020-12-20 02:54:03 +0300 |
| commit | faebd3e0f24333a5777102db081a844fdaf7b07b (patch) | |
| tree | 3fc7fc1b29a7c592d16d82d38ff82a991c203b0c /parse.go | |
| parent | b91c03d2c6c9e6429f3947f6b6e83321a56aa0f2 (diff) | |
Optional long arguments
Diffstat (limited to 'parse.go')
| -rw-r--r-- | parse.go | 16 |
1 files changed, 11 insertions, 5 deletions
@@ -48,6 +48,7 @@ func (p path) Child(f reflect.StructField) path { type spec struct { dest path typ reflect.Type + name string // canonical name for the option long string short string multiple bool @@ -280,6 +281,8 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { typ: field.Type, } + spec.name = spec.long + help, exists := field.Tag.Lookup("help") if exists { spec.help = help @@ -308,6 +311,9 @@ 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", @@ -364,7 +370,7 @@ func cmdFromStruct(name string, dest path, t reflect.Type) (*command, error) { if hasPlaceholder { spec.placeholder = placeholder } else { - spec.placeholder = strings.ToUpper(spec.long) + spec.placeholder = strings.ToUpper(spec.name) } // Check whether this field is supported. It's good to do this here rather than @@ -617,13 +623,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.long, err) + return fmt.Errorf("error processing %s: %v", spec.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.long, err) + return fmt.Errorf("error processing %s: %v", spec.name, err) } positionals = positionals[1:] } @@ -638,8 +644,8 @@ func (p *Parser) process(args []string) error { continue } - name := spec.long - if !spec.positional { + name := spec.name + if spec.long != "" && !spec.positional { name = "--" + spec.long } |
