Skip to content

Commit

Permalink
👔 up(app): update the command register logic, set IndentLongOpt=true
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 25, 2023
1 parent f00baba commit 69d1c14
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
8 changes: 8 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func NewApp(fns ...func(app *App)) *App {

app.fs = gflag.New(app.Name).WithConfigFn(func(opt *gflag.Config) {
opt.WithoutType = true
opt.IndentLongOpt = true
opt.Alignment = gflag.AlignLeft
})

Expand Down Expand Up @@ -211,9 +212,16 @@ func (app *App) AddCommand(c *Command) {
c.app = app
// inherit some from application
c.Ctx = app.Ctx
// init for cmd flags parser
c.Flags.Init(c.Name)

// inherit flag parser config
fsCfg := app.fs.ParserCfg()
c.Flags.WithConfigFn(gflag.WithIndentLongOpt(fsCfg.IndentLongOpt))

// do add command
app.addCommand(app.Name, c)

app.fireWithCmd(events.OnAppCmdAdded, c, nil)
}

Expand Down
17 changes: 13 additions & 4 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ func (ctx *Context) WorkDir() string {
return ctx.workDir
}

// ChWorkDir work dir path
func (ctx *Context) ChWorkDir(dir string) {
if len(dir) > 0 {
ctx.workDir = dir
}
}

// ArgLine os.Args to string, but no binName.
func (ctx *Context) ArgLine() string {
return ctx.argLine
Expand Down Expand Up @@ -222,6 +229,11 @@ func (b *base) BinDir() string { return b.Ctx.BinDir() }
// WorkDir get work dirname
func (b *base) WorkDir() string { return b.Ctx.workDir }

// ChWorkDir work dir path
func (b *base) ChWorkDir(dir string) {
b.Ctx.ChWorkDir(dir)
}

// ResetData from ctx
func (b *base) ResetData() {
if b.Ctx != nil {
Expand Down Expand Up @@ -274,7 +286,7 @@ func (b *base) IsCommand(name string) bool {

// add Command to the group
func (b *base) addCommand(pName string, c *Command) {
// init command
// ensure init command
c.initialize()

cName := c.Name
Expand Down Expand Up @@ -307,9 +319,6 @@ func (b *base) addCommand(pName string, c *Command) {
// add aliases for the command
Logf(VerbCrazy, "register command '%s'(parent: %s), aliases: %v", cName, pName, c.Aliases)
b.cmdAliases.AddAliases(c.Name, c.Aliases)

// inherit global flags from application
// append
b.commands[cName] = c
}

Expand Down
20 changes: 10 additions & 10 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,20 @@ func (c *Command) AddCommand(sub *Command) {
// init command
sub.app = c.app
sub.parent = c
// inherit standalone value
sub.standalone = c.standalone
// inherit something from parent
// inherit something from parent command
sub.Ctx = c.Ctx
sub.standalone = c.standalone

// initialize command
c.initialize()

// extend path names from parent
sub.pathNames = c.pathNames[0:]

// do add
// do add and init sub command
c.base.addCommand(c.Name, sub)
// update some parser config
sub.WithConfigFn(gflag.WithIndentLongOpt(c.ParserCfg().IndentLongOpt))
}

// Match sub command by input names
Expand Down Expand Up @@ -258,20 +259,19 @@ func (c *Command) initialize() {
c.initCommandBase(cName)
c.Fire(events.OnCmdInitBefore, nil)

// load common subs
// init for cmd flags parser
c.Flags.Init(cName)

// load common sub commands
if len(c.Subs) > 0 {
for _, sub := range c.Subs {
c.AddCommand(sub)
}
}

// init for cmd flags parser
c.Flags.Init(cName)

// format description
if len(c.Desc) > 0 {
c.Desc = strutil.UpperFirst(c.Desc)
// contains help var "{$cmd}". replace on here is for 'app help'
if strings.Contains(c.Desc, "{$cmd}") {
c.Desc = strings.Replace(c.Desc, "{$cmd}", c.Name, -1)
}
Expand Down Expand Up @@ -703,7 +703,7 @@ func (c *Command) App() *App {
return c.app
}

// ID get command ID string. return like: git:branch:create
// ID get command ID string. return like "git:branch:create"
func (c *Command) ID() string {
return strings.Join(c.pathNames, CommandSep)
}
Expand Down
2 changes: 1 addition & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ var CmdHelpTemplate = `{{.Desc}}
<comment>Help:</>
{{.Cmd.Help}}{{end}}`

// ShowHelp show command help info
// ShowHelp show command help information
func (c *Command) ShowHelp() (err error) {
Debugf("render the command '%s' help information", c.Name)

Expand Down

0 comments on commit 69d1c14

Please sign in to comment.