Inspired by Cobra. Cobra is a library for creating powerful modern CLI applications. "action" can be used to create a more fine-grained behavior of a command.
cmd := &cobra.Command{
Use: "example-cmd",
Short: "A sample command.",
RunE: func(cmd *cobra.Command, args []string) error {
act := &action.Action{
Name: "example-action",
Run: func(act *action.Action) error { return nil },
}
_ = act.AddAction(
newSubAction1(),
newSubAction2(),
)
act.Execute()
},
}
func newSubAction1() *action.Action {
return &action.Action{
Name: "sub-action1",
Executable: func(act *action.Action) bool {
// do something
return true
},
Run: func(act *action.Action) error { return nil },
}
}
func newSubAction2() *action.Action {
return &action.Action{
Name: "sub-action2",
Executable: func(act *action.Action) bool {
// do something
return false
},
Run: func(act *action.Action) error { return nil },
}
}
Executable
: whether is an executable action.action.Execute()
will find the first executable action of the root action and execute it. If no executable action can be found, run the current action.
You can find the docs at go docs.
action
had been being developed with GoLand under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.