Skip to content

Commit e5de968

Browse files
authored
fix(shell): arg doc when using positional arguments (#2964)
1 parent c0b1403 commit e5de968

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

internal/core/shell.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ func argIsOption(arg string) bool {
7474
return strings.Contains(arg, "=") || strings.Contains(arg, ".")
7575
}
7676

77+
func argIsPositional(cmd *Command, arg string) bool {
78+
if cmd.Verb != "" && cmd.Verb == arg {
79+
return false
80+
} else if cmd.Resource != "" && cmd.Resource == arg {
81+
return false
82+
} else if cmd.Namespace != "" && cmd.Resource == arg {
83+
return false
84+
}
85+
86+
return true
87+
}
88+
7789
// removeOptions removes options from a list of argument
7890
// ex: scw instance create name=myserver
7991
// will be changed to: scw instance server create
@@ -122,9 +134,12 @@ func getCommand(meta *meta, args []string, suggest string) *Command {
122134

123135
rawCommand = meta.CliConfig.Alias.ResolveAliases(rawCommand)
124136

125-
command, foundCommand := meta.Commands.find(rawCommand...)
126-
if foundCommand {
127-
return command
137+
// Find the closest command in case there is multiple positional arguments
138+
for ; len(rawCommand) > 1; rawCommand = rawCommand[:len(rawCommand)-1] {
139+
command, foundCommand := meta.Commands.find(rawCommand...)
140+
if foundCommand {
141+
return command
142+
}
128143
}
129144
return nil
130145
}
@@ -133,23 +148,29 @@ func getCommand(meta *meta, args []string, suggest string) *Command {
133148
// it will return command description if it is a command
134149
// or option description if suggest is an option of a command
135150
func getSuggestDescription(meta *meta, args []string, suggest string) string {
136-
isOption := argIsOption(suggest)
137-
138151
command := getCommand(meta, args, suggest)
139152
if command == nil {
140153
return "command not found"
141154
}
142155

143-
if isOption {
156+
if argIsOption(suggest) {
144157
option := command.ArgSpecs.GetByName(optionToArgSpecName(suggest))
145158
if option != nil {
146159
return option.Short
147160
}
148-
} else {
149-
return command.Short
161+
return ""
150162
}
151163

152-
return ""
164+
if argIsPositional(command, suggest) {
165+
option := command.ArgSpecs.GetPositionalArg()
166+
if option != nil {
167+
return option.Short
168+
}
169+
return ""
170+
}
171+
172+
// Should be a command, just use command short
173+
return command.Short
153174
}
154175

155176
// sortOptions sorts options, putting required first then order alphabetically

0 commit comments

Comments
 (0)