Skip to content

Commit

Permalink
feat(command): adding args to use line
Browse files Browse the repository at this point in the history
Signed-off-by: danmx <[email protected]>
  • Loading branch information
danmx committed Sep 24, 2023
1 parent 0c72800 commit 64cff20
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 4 deletions.
17 changes: 13 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,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
Expand Down Expand Up @@ -1413,12 +1417,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
}

Expand Down Expand Up @@ -1762,6 +1766,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)
Expand Down
100 changes: 100 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 64cff20

Please sign in to comment.