Skip to content

Commit

Permalink
✨ feat: cflag - add command support by struct cflag.Cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 21, 2024
1 parent a74c379 commit 21b0dcd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
10 changes: 10 additions & 0 deletions cflag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,16 @@ func main() {
return nil
}

// add cmd by struct
app.Add(&cflag.Cmd{
Name: "demo2",
Desc: "this is demo2 command",
Func: func(c *cflag.Cmd) error {
dump.P("hi, on demo2 command")
return nil
},
})

// Multiple commands can be added
app.Add(c1)

Expand Down
10 changes: 10 additions & 0 deletions cflag/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ func main() {
return nil
}

// add cmd by struct
app.Add(&cflag.Cmd{
Name: "demo2",
Desc: "this is demo2 command",
Func: func(c *cflag.Cmd) error {
dump.P("hi, on demo2 command")
return nil
},
})

// 可以添加多个命令
app.Add(c1)

Expand Down
24 changes: 21 additions & 3 deletions cflag/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ func NewApp(fns ...func(app *App)) *App {
return app
}

// Add command(s) to app
// Add command(s) to app.
//
// NOTE: command object should create use NewCmd()
func (a *App) Add(cmds ...*Cmd) {
for _, cmd := range cmds {
a.addCmd(cmd)
Expand All @@ -72,6 +74,14 @@ func (a *App) addCmd(c *Cmd) {

// attach handle func
if c.Func != nil {
// fix: init c.CFlags on not exist
if c.CFlags == nil {
c.CFlags = NewEmpty(func(cf *CFlags) {
cf.Desc = c.Desc
cf.FlagSet = flag.NewFlagSet(c.Name, flag.ContinueOnError)
})
}

c.CFlags.Func = func(_ *CFlags) error {
return c.Func(c)
}
Expand Down Expand Up @@ -145,7 +155,7 @@ func (a *App) showHelp() error {
for _, name := range a.names {
c := a.cmds[name]
name := strutil.PadRight(name, " ", a.NameWidth)
buf.Printf(" <green>%s</> %s\n", name, strutil.UpperFirst(c.Desc))
buf.Printf(" <green>%s</> %s\n", name, strutil.UpperFirst(c.getDesc()))
}

name := strutil.PadRight("help", " ", a.NameWidth)
Expand All @@ -168,8 +178,9 @@ func (a *App) showHelp() error {
type Cmd struct {
*CFlags
Name string
Func func(c *Cmd) error
Desc string // desc for command, will sync to CFlags.Desc
OnAdd func(c *Cmd)
Func func(c *Cmd) error
}

// NewCmd instance
Expand All @@ -192,3 +203,10 @@ func (c *Cmd) Config(fn func(c *Cmd)) *Cmd {
}
return c
}

func (c *Cmd) getDesc() string {
if c.CFlags.Desc != "" {
return c.CFlags.Desc
}
return c.Desc
}
10 changes: 10 additions & 0 deletions cflag/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func TestApp_Run(t *testing.T) {
app.Add(c1)
})

// add cmd by struct
app.Add(&cflag.Cmd{
Name: "demo2",
Desc: "this is demo2 command",
Func: func(c *cflag.Cmd) error {
dump.P("hi, on demo2 command")
return nil
},
})

// show help1
osArgs := os.Args
os.Args = []string{"./myapp"}
Expand Down

0 comments on commit 21b0dcd

Please sign in to comment.