Skip to content

Latest commit

 

History

History
67 lines (54 loc) · 1.8 KB

File metadata and controls

67 lines (54 loc) · 1.8 KB

It seems you want to apply the cobra.MaximumNArgs(1) validation within the Run function of a Cobra command and handle the error if the validation fails. Here's how you can achieve that:

package main

import (
	"fmt"
	"os"

	"github.com/spf13/cobra"
)

func main() {
	rootCmd := &cobra.Command{
		Use:   "mycli",
		Short: "My CLI application",
		Run: func(cmd *cobra.Command, args []string) {
			if err := cobra.MaximumNArgs(1)(cmd, args); err != nil {
				fmt.Println("Please enter valid data")
				os.Exit(1)
			}
			// Your command logic here
			fmt.Println("Valid data received:", args)
		},
	}

	if err := rootCmd.Execute(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

Explanation:

  • Validation Inside Run Function:

    • The Run function of the command directly includes the argument validation logic.
    • cobra.MaximumNArgs(1)(cmd, args) is used to apply the validation for at most 1 argument.
    • If the validation fails (i.e., if there are more than 1 argument), it prints an error message and exits the program.
  • Handling Errors:

    • The rootCmd.Execute() call returns an error if there's any issue during command execution. This error is checked, and if it's not nil, it's printed, and the program exits with a non-zero status code.
  • Command Logic:

    • Below the validation check, you can place your actual command logic that executes when the validation passes. In this example, I've added a simple print statement to indicate that valid data was received.

Usage:

You can run this CLI application and observe the behavior:

  • Valid usage with 1 or fewer arguments:

    ./mycli arg1

    Output:

    Valid data received: [arg1]
    
  • Invalid usage with more than 1 argument:

    ./mycli arg1 arg2

    Output:

    Please enter valid data