Skip to content

Commit

Permalink
improve custom command error message for missing arguments (#752)
Browse files Browse the repository at this point in the history
* update custom command error message for missing arguments

Signed-off-by: Pulak Kanti Bhowmick <[email protected]>

* use string builder

Signed-off-by: Pulak Kanti Bhowmick <[email protected]>

* change format as per review

Signed-off-by: Pulak Kanti Bhowmick <[email protected]>

---------

Signed-off-by: Pulak Kanti Bhowmick <[email protected]>
Co-authored-by: Andriy Knysh <[email protected]>
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2024
1 parent 315e694 commit 1f135ad
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -164,11 +165,19 @@ func preCustomCommand(
parentCommand *cobra.Command,
commandConfig *schema.Command,
) {
var err error

var sb strings.Builder
if len(args) != len(commandConfig.Arguments) {
err = fmt.Errorf("invalid number of arguments, %d argument(s) required", len(commandConfig.Arguments))
u.LogErrorAndExit(schema.CliConfiguration{}, err)
sb.WriteString(fmt.Sprintf("Command requires %d argument(s):\n", len(commandConfig.Arguments)))
for i, arg := range commandConfig.Arguments {
if arg.Name == "" {
u.LogErrorAndExit(schema.CliConfiguration{}, errors.New("invalid argument configuration: empty argument name"))
}
sb.WriteString(fmt.Sprintf(" %d. %s\n", i+1, arg.Name))
}
if len(args) > 0 {
sb.WriteString(fmt.Sprintf("\nReceived %d argument(s): %s", len(args), strings.Join(args, ", ")))
}
u.LogErrorAndExit(schema.CliConfiguration{}, errors.New(sb.String()))
}

// no "steps" means a sub command should be specified
Expand Down

0 comments on commit 1f135ad

Please sign in to comment.