init
This commit is contained in:
50
myCobra/cobra.go
Normal file
50
myCobra/cobra.go
Normal file
@ -0,0 +1,50 @@
|
||||
package myCobra
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type SimpleCmd struct {
|
||||
Use string
|
||||
Short string
|
||||
Example string
|
||||
PreRun func()
|
||||
RunE func() error
|
||||
cobraModel *cobra.Command
|
||||
}
|
||||
|
||||
func (t *SimpleCmd) GetCobra() *cobra.Command {
|
||||
if t.cobraModel == nil {
|
||||
t.cobraModel = &cobra.Command{
|
||||
Use: t.Use,
|
||||
Short: t.Short,
|
||||
Example: t.Example,
|
||||
SilenceUsage: true,
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
t.PreRun()
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return t.RunE()
|
||||
},
|
||||
}
|
||||
}
|
||||
return t.cobraModel
|
||||
}
|
||||
|
||||
func (t *SimpleCmd) SetArgsFunc(argsFunc func(args []string) error) {
|
||||
t.GetCobra().Args = func(cmd *cobra.Command, args []string) error {
|
||||
return argsFunc(args)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *SimpleCmd) SetStringVar(p *string, name, shorthand string, value string, usage string) {
|
||||
t.GetCobra().PersistentFlags().StringVarP(p, name, shorthand, value, usage)
|
||||
}
|
||||
|
||||
func (t *SimpleCmd) AddCommand(cmd *SimpleCmd) {
|
||||
t.GetCobra().AddCommand(cmd.GetCobra())
|
||||
}
|
||||
|
||||
func (t *SimpleCmd) Execute() error {
|
||||
return t.GetCobra().Execute()
|
||||
}
|
Reference in New Issue
Block a user