From e6ff482cdf6758885ae9a1bdcd3ea6fb5e620a05 Mon Sep 17 00:00:00 2001 From: Guillaume Bouvignies Date: Wed, 26 Jul 2023 09:35:04 +0200 Subject: [PATCH] fix: CLI args marked as greedy were not greedy (#975) ## Description: Fixes #974 ## Is this change user facing? NO ## References (if applicable): --- .../lowlevel/lowlevel_kurtosis_command.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cli/cli/command_framework/lowlevel/lowlevel_kurtosis_command.go b/cli/cli/command_framework/lowlevel/lowlevel_kurtosis_command.go index d013b4c9ab..c772665bee 100644 --- a/cli/cli/command_framework/lowlevel/lowlevel_kurtosis_command.go +++ b/cli/cli/command_framework/lowlevel/lowlevel_kurtosis_command.go @@ -151,6 +151,8 @@ func (kurtosisCmd *LowlevelKurtosisCommand) MustGetCobraCommand() *cobra.Command // - Any arg after an optional arg (the parser wouldn't know whether you want the optional arg or the one after it) // - Any arg after an arg that consumes N args (since the CLI couldn't know where the greedy arg stops and the required arg begins) terminalArgKey := "" + numberOfRequiredArgs := 0 + lastArgIsGreedy := false for _, argConfig := range kurtosisCmd.Args { key := argConfig.Key if terminalArgKey != "" { @@ -161,8 +163,12 @@ func (kurtosisCmd *LowlevelKurtosisCommand) MustGetCobraCommand() *cobra.Command key, )) } + if !argConfig.IsOptional { + numberOfRequiredArgs += 1 + } if argConfig.IsOptional || argConfig.IsGreedy { terminalArgKey = key + lastArgIsGreedy = argConfig.IsGreedy } } @@ -319,7 +325,11 @@ func (kurtosisCmd *LowlevelKurtosisCommand) MustGetCobraCommand() *cobra.Command Long: kurtosisCmd.LongDescription, ValidArgsFunction: getCompletionsFunc, RunE: cobraRunFunc, - Args: cobra.MaximumNArgs(len(kurtosisCmd.Args)), + } + if lastArgIsGreedy { + result.Args = cobra.MinimumNArgs(numberOfRequiredArgs) + } else { + result.Args = cobra.RangeArgs(numberOfRequiredArgs, len(kurtosisCmd.Args)) } // Validates that the default values for the declared flags match the declard types, and add them to the Cobra command