Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve custom command error message for missing arguments #752

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
aknysh marked this conversation as resolved.
Show resolved Hide resolved
}
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()))
aknysh marked this conversation as resolved.
Show resolved Hide resolved
}

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