aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Bendersky <eliben@users.noreply.github.com>2019-09-04 09:18:39 -0700
committerAndrew Jackura <ajackura@google.com>2019-09-04 09:18:39 -0700
commitdd7622c7b1e7543f248e76a408f75d346252edaa (patch)
tree908642337f30dae99ead3ebff33a9571edb784bd
parent636abe8753b82e6eefa1beca9f46e49b470aa3d7 (diff)
downloadgo-subcommands-dd7622c7b1e7543f248e76a408f75d346252edaa.tar.gz
Only print "<cmd> flags" suggestion when flags were defined (#27)
Fixes #18
-rw-r--r--subcommands.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/subcommands.go b/subcommands.go
index 3997d51..af8463a 100644
--- a/subcommands.go
+++ b/subcommands.go
@@ -172,6 +172,15 @@ func (cdr *Commander) VisitAll(fn func(*flag.Flag)) {
}
}
+// countFlags returns the number of top-level flags defined, even those not set.
+func (cdr *Commander) countTopFlags() int {
+ count := 0
+ cdr.VisitAll(func(*flag.Flag) {
+ count++
+ })
+ return count
+}
+
// Execute should be called once the top-level-flags on a Commander
// have been initialized. It finds the correct subcommand and executes
// it, and returns an ExitStatus with the result. On a usage error, an
@@ -229,7 +238,9 @@ func (cdr *Commander) explain(w io.Writer) {
sort.Strings(cdr.important)
if len(cdr.important) == 0 {
- fmt.Fprintf(w, "\nUse \"%s flags\" for a list of top-level flags\n", cdr.name)
+ if cdr.countTopFlags() > 0 {
+ fmt.Fprintf(w, "\nUse \"%s flags\" for a list of top-level flags\n", cdr.name)
+ }
return
}