From 26c2643bdc957594a977bfbd97ea86d99df6c3e7 Mon Sep 17 00:00:00 2001 From: Philippe Martin Date: Mon, 9 Mar 2026 18:23:32 +0100 Subject: [PATCH] fix: set Args for all cobra commands Signed-off-by: Philippe Martin Co-Authored-By: Claude Code (Claude Sonnet 4.5) --- AGENTS.md | 11 +++++++++++ pkg/cmd/list.go | 1 + pkg/cmd/root.go | 1 + pkg/cmd/workspace.go | 1 + pkg/cmd/workspace_list.go | 1 + 5 files changed, 15 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 136385f..fecc121 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -204,12 +204,22 @@ Skills are reusable capabilities that can be discovered and executed by AI agent 1. Create `pkg/cmd/.go` with a `NewCmd()` function that returns `*cobra.Command` 2. In the `NewCmd()` function: - Create and configure the `cobra.Command` + - **IMPORTANT**: Always define the `Args` field to specify argument validation - Set up any flags or subcommands - Return the configured command 3. Register the command in `pkg/cmd/root.go` by adding `rootCmd.AddCommand(NewCmd())` in the `NewRootCmd()` function 4. Create corresponding test file `pkg/cmd/_test.go` 5. In tests, create command instances using `NewRootCmd()` or `NewCmd()` as needed +**Command Argument Validation:** + +All commands **MUST** declare the `Args` field to specify argument validation behavior. Common options: +- `cobra.NoArgs` - Command accepts no arguments (most common for parent commands and no-arg commands) +- `cobra.ExactArgs(n)` - Command requires exactly n arguments +- `cobra.MinimumNArgs(n)` - Command requires at least n arguments +- `cobra.MaximumNArgs(n)` - Command accepts up to n arguments +- `cobra.RangeArgs(min, max)` - Command accepts between min and max arguments + Example: ```go // pkg/cmd/example.go @@ -217,6 +227,7 @@ func NewExampleCmd() *cobra.Command { return &cobra.Command{ Use: "example", Short: "An example command", + Args: cobra.NoArgs, // Always declare Args field Run: func(cmd *cobra.Command, args []string) { // Command logic here }, diff --git a/pkg/cmd/list.go b/pkg/cmd/list.go index 87fc155..ac89459 100644 --- a/pkg/cmd/list.go +++ b/pkg/cmd/list.go @@ -31,6 +31,7 @@ func NewListCmd() *cobra.Command { Use: "list", Short: workspaceListCmd.Short, Long: workspaceListCmd.Long, + Args: workspaceListCmd.Args, PreRunE: workspaceListCmd.PreRunE, RunE: workspaceListCmd.RunE, } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index 2489e88..8d9f923 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -41,6 +41,7 @@ func NewRootCmd() *cobra.Command { rootCmd := &cobra.Command{ Use: "kortex-cli", Short: "Launch and manage AI agent workspaces with custom configurations", + Args: cobra.NoArgs, } // Add subcommands diff --git a/pkg/cmd/workspace.go b/pkg/cmd/workspace.go index 3122375..3ca8c8f 100644 --- a/pkg/cmd/workspace.go +++ b/pkg/cmd/workspace.go @@ -27,6 +27,7 @@ func NewWorkspaceCmd() *cobra.Command { Use: "workspace", Short: "Manage workspaces", Long: "Manage workspaces registered with kortex-cli init", + Args: cobra.NoArgs, } // Add subcommands diff --git a/pkg/cmd/workspace_list.go b/pkg/cmd/workspace_list.go index a5485ad..c10d12d 100644 --- a/pkg/cmd/workspace_list.go +++ b/pkg/cmd/workspace_list.go @@ -132,6 +132,7 @@ func NewWorkspaceListCmd() *cobra.Command { Use: "list", Short: "List all registered workspaces", Long: "List all workspaces registered with kortex-cli init", + Args: cobra.NoArgs, PreRunE: c.preRun, RunE: c.run, }