feat(command): adding args to use line

Signed-off-by: danmx <daniel.iziourov@elastic.co>
This commit is contained in:
danmx 2022-08-18 10:28:13 +02:00 committed by danmx
parent 5c962a221e
commit 63a0032301
No known key found for this signature in database
GPG key ID: CEEF376EF34B5317
2 changed files with 113 additions and 4 deletions

View file

@ -242,6 +242,10 @@ type Command struct {
// line of a command when printing help or generating docs // line of a command when printing help or generating docs
DisableFlagsInUseLine bool 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 // DisableSuggestions disables the suggestions based on Levenshtein distance
// that go along with 'unknown command' messages. // that go along with 'unknown command' messages.
DisableSuggestions bool DisableSuggestions bool
@ -1415,12 +1419,12 @@ func (c *Command) UseLine() string {
} else { } else {
useline = c.Use useline = c.Use
} }
if c.DisableFlagsInUseLine { if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") && !c.DisableFlagsInUseLine {
return useline
}
if c.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {
useline += " [flags]" useline += " [flags]"
} }
if c.HasAvailableArgs() && !strings.Contains(useline, "[args]") && !c.DisableArgsInUseLine {
useline += " [args]"
}
return useline return useline
} }
@ -1764,6 +1768,11 @@ func (c *Command) HasAvailableInheritedFlags() bool {
return c.InheritedFlags().HasAvailableFlags() 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. // Flag climbs up the command tree looking for matching flag.
func (c *Command) Flag(name string) (flag *flag.Flag) { func (c *Command) Flag(name string) (flag *flag.Flag) {
flag = c.Flags().Lookup(name) flag = c.Flags().Lookup(name)

View file

@ -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)
}
}
}