spf13 / cobra-cli

Cobra CLI tool to generate applications and commands

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to call a command from another command with flags

santoshlite opened this issue · comments

Hey, I am trying to call a command with a specific flag from another command but I've been struggling to make it work. Here is what I am doing:

var startCmd = &cobra.Command{
	Use:   "start",
	Short: "The central command for the CLI.",
	Long: `start can execute any of the other commands in the CLI.`,
	Run: func(cmd *cobra.Command, args []string) {
		getCmd.SetArgs([]string{"--data", "price"})
		getCmd.Run(cmd, os.Args[1:])
               // getCmdRun(cmd, []string{"--data", "price"}) -> tried this as well but failed too
	},
}

func init() {
	rootCmd.AddCommand(startCmd)
	startCmd.AddCommand(getCmd)
}

Each time it raises a warning from the getCmd command saying my flag is not recognized. How do we do that?