Skip to content

Commit 1ff0d5e

Browse files
committed
feat: add descriptive error messages for missing positional arguments
Replace Cobra's generic "accepts N arg(s), received 0" errors with contextual messages like "missing incident_id. Usage: flashduty incident update <id>", helping CLI users understand what's needed.
1 parent 6e74a87 commit 1ff0d5e

3 files changed

Lines changed: 30 additions & 8 deletions

File tree

internal/cli/args.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
// requireArgs returns a positional argument validator that produces descriptive
11+
// error messages naming the missing arguments, e.g.:
12+
//
13+
// "missing incident_id. Usage: flashduty incident update <id>"
14+
func requireArgs(argNames ...string) cobra.PositionalArgs {
15+
return func(cmd *cobra.Command, args []string) error {
16+
if len(args) < len(argNames) {
17+
missing := argNames[len(args):]
18+
return fmt.Errorf("missing %s. Usage: %s", strings.Join(missing, ", "), cmd.UseLine())
19+
}
20+
return nil
21+
}
22+
}

internal/cli/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newConfigSetCmd() *cobra.Command {
4444
Use: "set <key> <value>",
4545
Short: "Set a configuration value",
4646
Long: "Supported keys: app_key, base_url",
47-
Args: cobra.ExactArgs(2),
47+
Args: requireArgs("key", "value"),
4848
RunE: func(cmd *cobra.Command, args []string) error {
4949
key, value := args[0], args[1]
5050

internal/cli/incident.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func newIncidentGetCmd() *cobra.Command {
108108
return &cobra.Command{
109109
Use: "get <id> [<id2> ...]",
110110
Short: "Get incident details",
111-
Args: cobra.MinimumNArgs(1),
111+
Args: requireArgs("incident_id"),
112112
RunE: func(cmd *cobra.Command, args []string) error {
113113
client, err := newClient()
114114
if err != nil {
@@ -252,7 +252,7 @@ func newIncidentUpdateCmd() *cobra.Command {
252252
cmd := &cobra.Command{
253253
Use: "update <id>",
254254
Short: "Update an incident",
255-
Args: cobra.ExactArgs(1),
255+
Args: requireArgs("incident_id"),
256256
RunE: func(cmd *cobra.Command, args []string) error {
257257
client, err := newClient()
258258
if err != nil {
@@ -302,7 +302,7 @@ func newIncidentAckCmd() *cobra.Command {
302302
return &cobra.Command{
303303
Use: "ack <id> [<id2> ...]",
304304
Short: "Acknowledge incidents",
305-
Args: cobra.MinimumNArgs(1),
305+
Args: requireArgs("incident_id"),
306306
RunE: func(cmd *cobra.Command, args []string) error {
307307
client, err := newClient()
308308
if err != nil {
@@ -321,7 +321,7 @@ func newIncidentCloseCmd() *cobra.Command {
321321
return &cobra.Command{
322322
Use: "close <id> [<id2> ...]",
323323
Short: "Close incidents",
324-
Args: cobra.MinimumNArgs(1),
324+
Args: requireArgs("incident_id"),
325325
RunE: func(cmd *cobra.Command, args []string) error {
326326
client, err := newClient()
327327
if err != nil {
@@ -340,7 +340,7 @@ func newIncidentTimelineCmd() *cobra.Command {
340340
return &cobra.Command{
341341
Use: "timeline <id>",
342342
Short: "View incident timeline",
343-
Args: cobra.ExactArgs(1),
343+
Args: requireArgs("incident_id"),
344344
RunE: func(cmd *cobra.Command, args []string) error {
345345
client, err := newClient()
346346
if err != nil {
@@ -381,7 +381,7 @@ func newIncidentAlertsCmd() *cobra.Command {
381381
cmd := &cobra.Command{
382382
Use: "alerts <id>",
383383
Short: "View incident alerts",
384-
Args: cobra.ExactArgs(1),
384+
Args: requireArgs("incident_id"),
385385
RunE: func(cmd *cobra.Command, args []string) error {
386386
client, err := newClient()
387387
if err != nil {
@@ -427,7 +427,7 @@ func newIncidentSimilarCmd() *cobra.Command {
427427
cmd := &cobra.Command{
428428
Use: "similar <id>",
429429
Short: "Find similar incidents",
430-
Args: cobra.ExactArgs(1),
430+
Args: requireArgs("incident_id"),
431431
RunE: func(cmd *cobra.Command, args []string) error {
432432
client, err := newClient()
433433
if err != nil {

0 commit comments

Comments
 (0)