Skip to content

Commit

Permalink
Ensure flags suffix is added to useline
Browse files Browse the repository at this point in the history
If a mapped command is runnable and has flags,
make sure its useline includes the ' [flags]' suffix

Signed-off-by: Vui Lam <vui.lam@broadcom.com>
  • Loading branch information
vuil committed May 1, 2024
1 parent d181bcb commit fd24b5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions plugin/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ func hierarchyFromMappedCommandPath(mappedCommandPath string, cmd *cobra.Command
}

func useLineEx(cmd *cobra.Command, ic *InvocationContext) string {
useline := getInvocationStringForUseLine(cmd, ic)

if cmd.DisableFlagsInUseLine {
return useline
}

if cmd.HasAvailableFlags() && !strings.Contains(useline, "[flags]") {
useline += " [flags]"
}

return useline
}

func getInvocationStringForUseLine(cmd *cobra.Command, ic *InvocationContext) string {
// by checking sourceCommandPath we limit the use of the InvocationContext
// to only command-level (not plugin level) mapping
if ic == nil || ic.sourceCommandPath == "" {
Expand Down
6 changes: 3 additions & 3 deletions plugin/usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func TestGlobalTestFetchCommandHelpTextWithInvocationContext(t *testing.T) {
expected := `Fetch the plugin tests
Usage:
tanzu fetch
tanzu fetch [flags]
Examples:
sample example usage of the fetch command
Expand Down Expand Up @@ -576,7 +576,7 @@ func TestCommandMappedCommandWithInvocationContext(t *testing.T) {
expected := `Push the plugin tests
Usage:
tanzu pu SOMESTUFF
tanzu pu SOMESTUFF [flags]
tanzu pu [command]
Expand Down Expand Up @@ -643,7 +643,7 @@ func TestCommandMappedCommandSubCommandWithInvocationContext(t *testing.T) {
expected := `Push more
Usage:
tanzu pu more
tanzu pu more [flags]
Aliases:
more, mo
Expand Down

0 comments on commit fd24b5e

Please sign in to comment.