Skip to content

Commit e689762

Browse files
authored
Add option to specify in usage alias command name (#1482)
* Add option to specify in usage alias command name * add test
1 parent 6dba15f commit e689762

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

plugins/components/conversionlayer.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ func createCommandUsages(cmd Command, convertedStringFlags map[string]StringFlag
133133
}
134134

135135
func getCmdUsageString(cmd Command, namespaces ...string) string {
136-
return strings.Join(append(removeEmptyValues(namespaces), cmd.Name), " ")
136+
name := cmd.Name
137+
if cmd.UsageOptions != nil && cmd.UsageOptions.CommandName != "" {
138+
name = cmd.UsageOptions.CommandName
139+
}
140+
return strings.Join(append(removeEmptyValues(namespaces), name), " ")
137141
}
138142

139143
// Generated usages are based on the command's flags and arguments:

plugins/components/conversionlayer_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ func TestCreateCommandUsages(t *testing.T) {
103103
stringFlags: map[string]StringFlag{optStrFlag.Name: optStrFlag, strFlag.Name: strFlag},
104104
expected: override, // override is not expected to be changed upon using UsageOptions
105105
},
106+
{
107+
name: "with custom command name",
108+
cmd: Command{
109+
Name: cmdName,
110+
Arguments: []Argument{{Name: "first argument"}},
111+
UsageOptions: &UsageOptions{CommandName: "custom-command"},
112+
},
113+
expected: []string{
114+
fmt.Sprintf("%s custom-command <%s>", appNameSpace, "first argument"),
115+
},
116+
},
106117
}
107118

108119
for _, test := range tests {

plugins/components/structure.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type Command struct {
5252
}
5353

5454
type UsageOptions struct {
55+
// Command name to be used in the usage instead of the actual command name.
56+
CommandName string
5557
// Special cases, each of these will be created as command usage option and the value appended as suffix for the command name.
5658
Usage []string
5759
// If true then the given usages will replace the auto generated usage. Otherwise the given usages will be appended to the auto generated usage.

0 commit comments

Comments
 (0)