From e2ad4369f5973803a2bff879dec7f8d5d680bcb3 Mon Sep 17 00:00:00 2001 From: Jules Casteran Date: Tue, 28 Mar 2023 15:01:32 +0200 Subject: [PATCH 1/2] fix(shell): arg doc when using positional arguments --- internal/core/shell.go | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/internal/core/shell.go b/internal/core/shell.go index 9b6c12a237..2afe09604f 100644 --- a/internal/core/shell.go +++ b/internal/core/shell.go @@ -74,6 +74,18 @@ func argIsOption(arg string) bool { return strings.Contains(arg, "=") || strings.Contains(arg, ".") } +func argIsPositional(cmd *Command, arg string) bool { + if cmd.Verb != "" && cmd.Verb == arg { + return false + } else if cmd.Resource != "" && cmd.Resource == arg { + return false + } else if cmd.Namespace != "" && cmd.Resource == arg { + return false + } + + return true +} + // removeOptions removes options from a list of argument // ex: scw instance create name=myserver // will be changed to: scw instance server create @@ -122,9 +134,12 @@ func getCommand(meta *meta, args []string, suggest string) *Command { rawCommand = meta.CliConfig.Alias.ResolveAliases(rawCommand) - command, foundCommand := meta.Commands.find(rawCommand...) - if foundCommand { - return command + // Find the closest command in case there is multiple positional arguments + for ; len(rawCommand) > 1; rawCommand = rawCommand[:len(rawCommand)-1] { + command, foundCommand := meta.Commands.find(rawCommand...) + if foundCommand { + return command + } } return nil } @@ -133,23 +148,29 @@ func getCommand(meta *meta, args []string, suggest string) *Command { // it will return command description if it is a command // or option description if suggest is an option of a command func getSuggestDescription(meta *meta, args []string, suggest string) string { - isOption := argIsOption(suggest) - command := getCommand(meta, args, suggest) if command == nil { return "command not found" } - if isOption { + if argIsOption(suggest) { option := command.ArgSpecs.GetByName(optionToArgSpecName(suggest)) if option != nil { return option.Short } - } else { - return command.Short + return "" } - return "" + if argIsPositional(command, suggest) { + option := command.ArgSpecs.GetPositionalArg() + if option != nil { + return option.Short + } + return "" + } + + // Should be a command, juste use command short + return command.Short } // sortOptions sorts options, putting required first then order alphabetically From 232aad88a239be3d39d29bc59e45e153f8e74e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jules=20Cast=C3=A9ran?= Date: Tue, 28 Mar 2023 15:08:56 +0200 Subject: [PATCH 2/2] Update internal/core/shell.go --- internal/core/shell.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/core/shell.go b/internal/core/shell.go index 2afe09604f..21d99bb664 100644 --- a/internal/core/shell.go +++ b/internal/core/shell.go @@ -169,7 +169,7 @@ func getSuggestDescription(meta *meta, args []string, suggest string) string { return "" } - // Should be a command, juste use command short + // Should be a command, just use command short return command.Short }