Skip to content

Commit

Permalink
feat: show usage text when CLI cmd fails because of missing a require…
Browse files Browse the repository at this point in the history
…d argument (#1774)

## Description:
show usage text when CLI cmd fails because of missing a required
argument

## Is this change user facing?
YES

## References (if applicable):
fix #1576 

Message example:

![image](https://github.com/kurtosis-tech/kurtosis/assets/29951623/73c04c29-b386-4fa5-acf5-e21e424a9031)
  • Loading branch information
leoporoli committed Nov 14, 2023
1 parent 0e6af36 commit a7df8cf
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions cli/cli/command_framework/lowlevel/lowlevel_kurtosis_command.go
Expand Up @@ -2,6 +2,7 @@ package lowlevel

import (
"context"
"errors"
"fmt"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
Expand Down Expand Up @@ -48,7 +49,7 @@ type LowlevelKurtosisCommand struct {
PostValidationAndRunFunc func(ctx context.Context)
}

// Gets a Cobra command represnting the LowlevelKurtosisCommand
// Gets a Cobra command representing the LowlevelKurtosisCommand
// This function is intended to be run in an init() (i.e. before the program runs any logic), so it will panic if
// any errors occur
func (kurtosisCmd *LowlevelKurtosisCommand) MustGetCobraCommand() *cobra.Command {
Expand Down Expand Up @@ -316,6 +317,8 @@ func (kurtosisCmd *LowlevelKurtosisCommand) MustGetCobraCommand() *cobra.Command
strings.Join(allArgUsageStrs, " "),
)

expectedArgsFunc := kurtosisCmd.getExpectedArgumentsFunc(lastArgIsGreedy, numberOfRequiredArgs, allArgUsageStrs)

// Suppressing exhaustruct requirement because this struct has ~40 properties
// nolint: exhaustruct
result := &cobra.Command{
Expand All @@ -325,11 +328,7 @@ func (kurtosisCmd *LowlevelKurtosisCommand) MustGetCobraCommand() *cobra.Command
Long: kurtosisCmd.LongDescription,
ValidArgsFunction: getCompletionsFunc,
RunE: cobraRunFunc,
}
if lastArgIsGreedy {
result.Args = cobra.MinimumNArgs(numberOfRequiredArgs)
} else {
result.Args = cobra.RangeArgs(numberOfRequiredArgs, len(kurtosisCmd.Args))
Args: expectedArgsFunc,
}

// Validates that the default values for the declared flags match the declard types, and add them to the Cobra command
Expand Down Expand Up @@ -367,6 +366,31 @@ func (kurtosisCmd *LowlevelKurtosisCommand) MustGetCobraCommand() *cobra.Command
return result
}

func (kurtosisCmd *LowlevelKurtosisCommand) getExpectedArgumentsFunc(lastArgIsGreedy bool, numberOfRequiredArgs int, allArgUsageStrs []string) cobra.PositionalArgs {
var expectedArgsFunc cobra.PositionalArgs

if lastArgIsGreedy {
expectedArgsFunc = cobra.MinimumNArgs(numberOfRequiredArgs)
return expectedArgsFunc
}

expectedArgsFunc = func(cmd *cobra.Command, args []string) error {
min := numberOfRequiredArgs
max := len(kurtosisCmd.Args)

if len(args) < min || len(args) > max {
missingArgsText := fmt.Sprintf("this command accepts between %d and %d arg(s), received %d.", min, max, len(args))
cmdUsageText := fmt.Sprintf("Usage: %s %s", cmd.CommandPath(), strings.Join(allArgUsageStrs, " "))
helpUsageText := fmt.Sprintf("Run '%s --help' for more usage details.", cmd.CommandPath())
errText := strings.Join([]string{missingArgsText, cmdUsageText, helpUsageText}, "\n")
return errors.New(errText)
}
return nil
}

return expectedArgsFunc
}

// ====================================================================================================
// Private Helper Functions
// ====================================================================================================
Expand Down

0 comments on commit a7df8cf

Please sign in to comment.