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

use default value for required arg if not provided #763

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 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
16 changes: 12 additions & 4 deletions cmd/cmd_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,27 @@ func preCustomCommand(
parentCommand *cobra.Command,
commandConfig *schema.Command,
) {
var sb strings.Builder
if len(args) != len(commandConfig.Arguments) {
var sb strings.Builder
missingArgCount := 0
sb.WriteString(fmt.Sprintf("Command requires %d argument(s):\n", len(commandConfig.Arguments)))
for i, arg := range commandConfig.Arguments {
for _, arg := range commandConfig.Arguments {
if !arg.Required || arg.Default != "" {
continue
}
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))
missingArgCount += 1
sb.WriteString(fmt.Sprintf(" %d. %s\n", missingArgCount, 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()))

if missingArgCount > 0 {
u.LogErrorAndExit(schema.CliConfiguration{}, errors.New(sb.String()))
}
}

// no "steps" means a sub command should be specified
Expand Down
2 changes: 2 additions & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ type Command struct {
type CommandArgument struct {
Name string `yaml:"name" json:"name" mapstructure:"name"`
Description string `yaml:"description" json:"description" mapstructure:"description"`
Required bool `yaml:"required" json:"required" mapstructure:"required"`
Default any `yaml:"default" json:"default" mapstructure: "default"`
}

type CommandFlag struct {
Expand Down