Skip to content

Commit 5fdbadb

Browse files
committed
👔 up: cflag - update some for Cmd direct run
1 parent 74716d2 commit 5fdbadb

File tree

3 files changed

+86
-27
lines changed

3 files changed

+86
-27
lines changed

cflag/app.go

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,7 @@ func (a *App) addCmd(c *Cmd) {
102102
a.NameWidth = mathutil.MaxInt(a.NameWidth, ln)
103103

104104
// attach handle func
105-
if c.Func != nil {
106-
// fix: init c.CFlags on not exist
107-
if c.CFlags == nil {
108-
c.CFlags = NewEmpty(func(cf *CFlags) {
109-
cf.Desc = c.Desc
110-
cf.FlagSet = flag.NewFlagSet(c.Name, flag.ContinueOnError)
111-
})
112-
}
113-
114-
c.CFlags.Func = func(_ *CFlags) error {
115-
return c.Func(c)
116-
}
117-
}
105+
c.init()
118106

119107
if c.OnAdd != nil {
120108
c.OnAdd(c)
@@ -244,14 +232,19 @@ func (a *App) showHelp() error {
244232
return nil
245233
}
246234

247-
// Cmd struct
235+
//
236+
// region Command in app
237+
// -----------------------------------
238+
239+
// Cmd struct. see CFlags
248240
type Cmd struct {
249241
*CFlags
250242
Name string
251243
Desc string // desc for command, will sync set to CFlags.Desc
252-
// OnAdd hook func. you can add some cli options or arguments.
244+
// OnAdd hook func. fire on add to App
245+
// - you can add some cli options or arguments.
253246
OnAdd func(c *Cmd)
254-
// Func for run command, will call after options parsed.
247+
// Func for run command, will call after options parsed. will sync set to CFlags.Func
255248
Func func(c *Cmd) error
256249
}
257250

@@ -281,6 +274,45 @@ func (c *Cmd) Config(fn func(c *Cmd)) *Cmd {
281274
return c
282275
}
283276

277+
// QuickRun parse OS flags and run command, will auto handle error
278+
func (c *Cmd) QuickRun() { c.MustParse(nil) }
279+
280+
// MustRun parse flags and run command. alias of MustParse()
281+
func (c *Cmd) MustRun(args []string) { c.MustParse(args) }
282+
283+
// MustParse parse flags and run command, will auto handle error
284+
func (c *Cmd) MustParse(args []string) {
285+
if err := c.Parse(args); err != nil {
286+
ccolor.Redln("ERROR:", err)
287+
}
288+
}
289+
290+
// Parse flags and run command func
291+
//
292+
// If args is nil, will parse os.Args
293+
func (c *Cmd) Parse(args []string) error {
294+
// fix: cmd.xxRun not exec Cmd.Func
295+
c.init()
296+
return c.CFlags.Parse(args)
297+
}
298+
299+
func (c *Cmd) init() {
300+
// attach handle func
301+
if c.Func != nil {
302+
// fix: init c.CFlags on not exist
303+
if c.CFlags == nil {
304+
c.CFlags = NewEmpty(func(cf *CFlags) {
305+
cf.Desc = c.Desc
306+
cf.FlagSet = flag.NewFlagSet(c.Name, flag.ContinueOnError)
307+
})
308+
}
309+
310+
c.CFlags.Func = func(_ *CFlags) error {
311+
return c.Func(c)
312+
}
313+
}
314+
}
315+
284316
func (c *Cmd) getDesc() string {
285317
if c.CFlags.Desc != "" {
286318
return c.CFlags.Desc

cflag/app_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"os"
7+
"strconv"
78
"testing"
89

910
"github.com/gookit/goutil/cflag"
@@ -158,3 +159,33 @@ func TestApp_Run_error(t *testing.T) {
158159

159160
assert.ErrMsg(t, app.RunWithArgs([]string{"demo"}), "command run error")
160161
}
162+
163+
func TestCmd_Run(t *testing.T) {
164+
var c1Opts = struct {
165+
age int
166+
name string
167+
}{}
168+
169+
var buf bytes.Buffer
170+
171+
cmd := cflag.NewCmd("demo", "this is a demo command", func(c *cflag.Cmd) error {
172+
buf.WriteString("name=" + c1Opts.name)
173+
buf.WriteString("age=" + strconv.Itoa(c1Opts.age))
174+
buf.WriteString("arg1=")
175+
buf.WriteString(c.Arg("arg1").String())
176+
return nil
177+
})
178+
179+
cmd.IntVar(&c1Opts.age, "age", 0, "this is a int option;;a")
180+
cmd.StringVar(&c1Opts.name, "name", "", "this is a string option and required;true")
181+
cmd.AddArg("arg1", "this is arg1", true, nil)
182+
// show help
183+
cmd.MustParse([]string{"--help"})
184+
185+
// run
186+
cmd.MustRun([]string{"--name", "inhere", "arg1-value"})
187+
ret := buf.String()
188+
assert.StrContains(t, ret, "name=inhere")
189+
assert.StrContains(t, ret, "age=0")
190+
assert.StrContains(t, ret, "arg1=arg1-value")
191+
}

cflag/cflag.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"strings"
2121

2222
"github.com/gookit/goutil"
23-
"github.com/gookit/goutil/cliutil"
2423
"github.com/gookit/goutil/envutil"
2524
"github.com/gookit/goutil/errorx"
2625
"github.com/gookit/goutil/mathutil"
@@ -213,20 +212,16 @@ func (c *CFlags) BindArg(arg *FlagArg) {
213212
*************************************************************/
214213

215214
// QuickRun parse OS flags and run command, will auto handle error
216-
func (c *CFlags) QuickRun() {
217-
c.MustParse(nil)
218-
}
215+
func (c *CFlags) QuickRun() { c.MustParse(nil) }
219216

220217
// MustRun parse flags and run command. alias of MustParse()
221-
func (c *CFlags) MustRun(args []string) {
222-
c.MustParse(args)
223-
}
218+
func (c *CFlags) MustRun(args []string) { c.MustParse(args) }
224219

225220
// MustParse parse flags and run command, will auto handle error
226221
func (c *CFlags) MustParse(args []string) {
227222
err := c.Parse(args)
228223
if err != nil {
229-
cliutil.Redln("ERROR:", err)
224+
ccolor.Redln("ERROR:", err)
230225
}
231226
}
232227

@@ -240,7 +235,7 @@ func (c *CFlags) Parse(args []string) error {
240235

241236
defer func() {
242237
if err := recover(); err != nil {
243-
cliutil.Errorln("ERROR:", err)
238+
ccolor.Errorln("ERROR:", err)
244239
if Debug {
245240
fmt.Println(errorx.Newf("(debug mode)RECOVERD PARSE ERROR: %v", err))
246241
}
@@ -448,14 +443,15 @@ func (c *CFlags) showHelp(err error) {
448443
buf.Printf("<cyan>%s</>\n\n", c.helpDesc())
449444
}
450445

451-
buf.Printf("<comment>Usage:</> %s [--Options...] [...CliArgs]\n", binName)
446+
buf.Printf("<comment>Usage:</> %s [--Options...] [...Arguments]\n", binName)
452447
buf.WriteStr("<comment>Options:</>\n")
453448

454449
// render options help
455450
c.renderOptionsHelp(buf)
451+
buf.WriteStr1Nl(" <green>-h, --help</>" + strings.Repeat(" ", 4) + "Display command help")
456452

457453
if len(c.bindArgs) > 0 {
458-
buf.WriteStr1("\n<comment>CliArgs:</>\n")
454+
buf.WriteStr1("\n<comment>Arguments:</>\n")
459455
for _, arg := range c.bindArgs {
460456
buf.Printf(
461457
" <green>%s</> %s\n",

0 commit comments

Comments
 (0)