From 1a755607fdb7065a524cd94f8365d8cd2d825fcf Mon Sep 17 00:00:00 2001 From: danmx Date: Thu, 18 Aug 2022 10:28:13 +0200 Subject: [PATCH] feat(command): adding args to use line Signed-off-by: danmx --- command.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/command.go b/command.go index 675bb1340a..dad492f9f5 100644 --- a/command.go +++ b/command.go @@ -216,6 +216,10 @@ type Command struct { // line of a command when printing help or generating docs DisableFlagsInUseLine bool + // DisableArgsInUseLine will disable the addition of [args] to the usage + // line of a command when printing help or generating docs + DisableArgsInUseLine bool + // DisableSuggestions disables the suggestions based on Levenshtein distance // that go along with 'unknown command' messages. DisableSuggestions bool @@ -1271,6 +1275,9 @@ func (c *Command) UseLine() string { if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") { useline += " [flags]" } + if c.HasAvailableArgs() && !strings.Contains(useline, "[args]") && !c.DisableArgsInUseLine { + useline += " [args]" + } return useline } @@ -1613,6 +1620,11 @@ func (c *Command) HasAvailableInheritedFlags() bool { return c.InheritedFlags().HasAvailableFlags() } +// HasAvailableArgs checks if the command has non-nil Args. +func (c *Command) HasAvailableArgs() bool { + return c.Args != nil +} + // Flag climbs up the command tree looking for matching flag. func (c *Command) Flag(name string) (flag *flag.Flag) { flag = c.Flags().Lookup(name)