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

Execute several commands at once. #482

Closed
naliferov opened this issue Jul 2, 2017 · 3 comments
Closed

Execute several commands at once. #482

naliferov opened this issue Jul 2, 2017 · 3 comments

Comments

@naliferov
Copy link

naliferov commented Jul 2, 2017

For example i have command called "Init". What i should to write in this command, if i want when i start this command "Init" it will trigger to execute some number of subcommands?

@n10v
Copy link
Collaborator

n10v commented Jul 2, 2017

What's the problem to just call the Execute func of needed subcommands?

package main

import (
	"github.com/spf13/cobra"
)

var c1 = &cobra.Command{Use: "c1", Run: runC1}
var c2 = &cobra.Command{Use: "c2", Run: runC2}

func runC1(cmd *cobra.Command, args []string) {
	// do something
}

func runC2(cmd *cobra.Command, args []string) {
	// do something
	c1.Execute(cmd, args)
	// do somethind
}

func init() {
	c1.AddCommand(c2)
}

func main() {}

@naliferov
Copy link
Author

naliferov commented Jul 3, 2017

Thanks. This is exactly what i need. Maybe you can add it in examples of Readme.
But maybe code can it be more simple like that?

	Use:   "init",
	Short: "",
	Long: "",
	Run: func(cmd *cobra.Command, args []string) {
		cmd1.Run(cmd, args)
		cmd2.Run(cmd, args)
		cmd3.Run(cmd, args)
	},

cmd1, cmd2, cmd3 is commands and each of theme in separate file.

@n10v
Copy link
Collaborator

n10v commented Jul 3, 2017

Maybe you can add it in examples of Readme.

No, I think it's obvious solution

But maybe code can it be more simple like that?

But Run and Execute are different things actually. It depends on what you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants