diff options
| author | Alex Flint <[email protected]> | 2021-04-16 21:03:14 -0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-04-16 21:03:14 -0700 |
| commit | f4eb7f3a585abd65b0568428b2b9fde8cebffb6a (patch) | |
| tree | 8710075d4a458bdb31880d7a68f143c3b7a53108 /usage.go | |
| parent | 113aef7114af6135372272d6bb8ac4ceaf3a7a79 (diff) | |
| parent | 172800ff9a2765185520c06dcf969fde9f5eb5e3 (diff) | |
Merge pull request #137 from alexflint/optional-long
Optional long names
Diffstat (limited to 'usage.go')
| -rw-r--r-- | usage.go | 56 |
1 files changed, 41 insertions, 15 deletions
@@ -36,12 +36,15 @@ func (p *Parser) WriteUsage(w io.Writer) { // writeUsageForCommand writes usage information for the given subcommand func (p *Parser) writeUsageForCommand(w io.Writer, cmd *command) { - var positionals, options []*spec + var positionals, longOptions, shortOptions []*spec for _, spec := range cmd.specs { - if spec.positional { + switch { + case spec.positional: positionals = append(positionals, spec) - } else { - options = append(options, spec) + case spec.long != "": + longOptions = append(longOptions, spec) + case spec.short != "": + shortOptions = append(shortOptions, spec) } } @@ -64,7 +67,19 @@ func (p *Parser) writeUsageForCommand(w io.Writer, cmd *command) { } // write the option component of the usage message - for _, spec := range options { + for _, spec := range shortOptions { + // prefix with a space + fmt.Fprint(w, " ") + if !spec.required { + fmt.Fprint(w, "[") + } + fmt.Fprint(w, synopsis(spec, "-"+spec.short)) + if !spec.required { + fmt.Fprint(w, "]") + } + } + + for _, spec := range longOptions { // prefix with a space fmt.Fprint(w, " ") if !spec.required { @@ -144,12 +159,15 @@ func (p *Parser) WriteHelp(w io.Writer) { // writeHelp writes the usage string for the given subcommand func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) { - var positionals, options []*spec + var positionals, longOptions, shortOptions []*spec for _, spec := range cmd.specs { - if spec.positional { + switch { + case spec.positional: positionals = append(positionals, spec) - } else { - options = append(options, spec) + case spec.long != "": + longOptions = append(longOptions, spec) + case spec.short != "": + shortOptions = append(shortOptions, spec) } } @@ -166,10 +184,13 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) { } } - // write the list of options - if len(options) > 0 || cmd.parent == nil { + // write the list of options with the short-only ones first to match the usage string + if len(shortOptions)+len(longOptions) > 0 || cmd.parent == nil { fmt.Fprint(w, "\nOptions:\n") - for _, spec := range options { + for _, spec := range shortOptions { + p.printOption(w, spec) + } + for _, spec := range longOptions { p.printOption(w, spec) } } @@ -215,11 +236,16 @@ func (p *Parser) writeHelpForCommand(w io.Writer, cmd *command) { } func (p *Parser) printOption(w io.Writer, spec *spec) { - left := synopsis(spec, "--"+spec.long) + ways := make([]string, 0, 2) + if spec.long != "" { + ways = append(ways, synopsis(spec, "--"+spec.long)) + } if spec.short != "" { - left += ", " + synopsis(spec, "-"+spec.short) + ways = append(ways, synopsis(spec, "-"+spec.short)) + } + if len(ways) > 0 { + printTwoCols(w, strings.Join(ways, ", "), spec.help, spec.defaultVal, spec.env) } - printTwoCols(w, left, spec.help, spec.defaultVal, spec.env) } func synopsis(spec *spec, form string) string { |
