From 63a003230193541174782fc348fee37960c3f99d 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 | 17 ++++++-- command_test.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 4 deletions(-) diff --git a/command.go b/command.go index 909d8585..df850b7d 100644 --- a/command.go +++ b/command.go @@ -242,6 +242,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 @@ -1415,12 +1419,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 } @@ -1764,6 +1768,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) diff --git a/command_test.go b/command_test.go index 4afb7f7b..445e26a6 100644 --- a/command_test.go +++ b/command_test.go @@ -2768,3 +2768,103 @@ func TestUnknownFlagShouldReturnSameErrorRegardlessOfArgPosition(t *testing.T) { }) } } + +func TestUseLine(t *testing.T) { + var testFlagSet pflag.FlagSet + testFlagSet.AddFlag(&pflag.Flag{ + Name: "flag_0", + }) + testArgs := func(cmd *Command, args []string) error { return nil } + testCases := []struct { + Cmd *Command + UseLine string + }{ + { + &Command{ + DisableFlagsInUseLine: true, + DisableArgsInUseLine: true, + parent: &Command{ + Use: "parent_use_0", + }, + flags: &testFlagSet, + Args: testArgs, + Use: "use_0", + }, + "parent_use_0 use_0", + }, + { + &Command{ + DisableFlagsInUseLine: false, + DisableArgsInUseLine: false, + parent: &Command{ + Use: "parent_use_1", + }, + Use: "use_1", + }, + "parent_use_1 use_1", + }, + { + &Command{ + DisableFlagsInUseLine: false, + DisableArgsInUseLine: false, + Use: "use_2", + }, + "use_2", + }, + { + &Command{ + DisableFlagsInUseLine: false, + DisableArgsInUseLine: true, + parent: &Command{ + Use: "parent_use_3", + }, + flags: &testFlagSet, + Args: testArgs, + Use: "use_3", + }, + "parent_use_3 use_3 [flags]", + }, + { + &Command{ + DisableFlagsInUseLine: false, + DisableArgsInUseLine: false, + flags: &testFlagSet, + Args: testArgs, + Use: "use_4", + }, + "use_4 [flags] [args]", + }, + { + &Command{ + DisableFlagsInUseLine: false, + DisableArgsInUseLine: false, + Args: testArgs, + Use: "use_5", + }, + "use_5 [args]", + }, + { + &Command{ + DisableFlagsInUseLine: false, + DisableArgsInUseLine: false, + flags: &testFlagSet, + Use: "[flags] use_6", + }, + "[flags] use_6", + }, + { + &Command{ + DisableFlagsInUseLine: false, + DisableArgsInUseLine: false, + Args: testArgs, + Use: "[args] use_7", + }, + "[args] use_7", + }, + } + for i, tc := range testCases { + if tc.Cmd.UseLine() != tc.UseLine { + t.Errorf("test case no. %d mismatch.\nResult: %s\nExpect: %s", i, tc.Cmd.UseLine(), tc.UseLine) + } + } +}