summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2022-02-09 18:04:03 -0800
committerGitHub <[email protected]>2022-02-09 18:04:03 -0800
commitf0f44b65d1179ccedb4c56f493f97ec569a6654e (patch)
tree0d202fcd6c6a194e8c7c14bbc3af3ed386938fe6 /usage.go
parentd3706100bf0cfef2b8f6f9119889541c302d72d1 (diff)
parent5fb236a65dd1edf0de556d5bac83a1103b9cf73a (diff)
Merge pull request #175 from alexflint/bracketing-positionalsv1.4.3
Fix bracketing for non-required positionals in usage
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go26
1 files changed, 18 insertions, 8 deletions
diff --git a/usage.go b/usage.go
index 860dc15..e936811 100644
--- a/usage.go
+++ b/usage.go
@@ -124,22 +124,32 @@ func (p *Parser) writeUsageForSubcommand(w io.Writer, cmd *command) {
}
}
- // write the positional component of the usage message
+ // When we parse positionals, we check that:
+ // 1. required positionals come before non-required positionals
+ // 2. there is at most one multiple-value positional
+ // 3. if there is a multiple-value positional then it comes after all other positionals
+ // Here we merely print the usage string, so we do not explicitly re-enforce those rules
+
+ // write the positionals in following form:
+ // REQUIRED1 REQUIRED2
+ // REQUIRED1 REQUIRED2 [OPTIONAL1 [OPTIONAL2]]
+ // REQUIRED1 REQUIRED2 REPEATED [REPEATED ...]
+ // REQUIRED1 REQUIRED2 [REPEATEDOPTIONAL [REPEATEDOPTIONAL ...]]
+ // REQUIRED1 REQUIRED2 [OPTIONAL1 [REPEATEDOPTIONAL [REPEATEDOPTIONAL ...]]]
+ var closeBrackets int
for _, spec := range positionals {
- // prefix with a space
fmt.Fprint(w, " ")
+ if !spec.required {
+ fmt.Fprint(w, "[")
+ closeBrackets += 1
+ }
if spec.cardinality == multiple {
- if !spec.required {
- fmt.Fprint(w, "[")
- }
fmt.Fprintf(w, "%s [%s ...]", spec.placeholder, spec.placeholder)
- if !spec.required {
- fmt.Fprint(w, "]")
- }
} else {
fmt.Fprint(w, spec.placeholder)
}
}
+ fmt.Fprint(w, strings.Repeat("]", closeBrackets))
// if the program supports subcommands, give a hint to the user about their existence
if len(cmd.subcommands) > 0 {