From f2d156e8ff5be17a0b2304f47b6f5b59f8771e2c Mon Sep 17 00:00:00 2001 From: danmx Date: Thu, 27 Aug 2020 09:40:03 +0200 Subject: [PATCH] feat(command): adding args to use line Signed-off-by: danmx --- command.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/command.go b/command.go index 27d39d54e0..fd01fbf102 100644 --- a/command.go +++ b/command.go @@ -144,6 +144,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 @@ -1233,12 +1237,12 @@ func (c *Command) UseLine() string { } else { useline = c.Use } - if c.DisableFlagsInUseLine { - return useline - } - if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") { + if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") && !c.DisableFlagsInUseLine { useline += " [flags]" } + if c.HasAvailableArgs() && !strings.Contains(useline, "[args]") && !c.DisableArgsInUseLine { + useline += " [args]" + } return useline } @@ -1581,6 +1585,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)