Skip to content

Commit

Permalink
up: update some for gflag parse and app init logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 30, 2022
1 parent 3479377 commit 3482598
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
13 changes: 6 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type App struct {
// AppConfig

fs *Flags
// app flag options
// cli input options for app
opts *GlobalOpts

// Name app name
Expand Down Expand Up @@ -103,21 +103,21 @@ func NewApp(fns ...func(app *App)) *App {
Desc: "This is my console application",
}

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

Logf(VerbCrazy, "create a new cli application, and create base ")

// set a default version
app.Version = "1.0.0"
app.Context = gCtx

// init base
app.base = newBase()
app.opts = newGlobalOpts()

// set a default version
app.Version = "1.0.0"
app.Context = gCtx

for _, fn := range fns {
fn(app)
}
Expand Down Expand Up @@ -224,7 +224,6 @@ func (app *App) AddCommand(c *Command) {

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

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

Expand Down
2 changes: 1 addition & 1 deletion base.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func newBase() base {
cmdAliases: structs.NewAliases(aliasNameCheck),
// ExitOnEnd: false,
tplVars: make(map[string]any),
Context: NewCtx(),
// Context: NewCtx(),
}
}

Expand Down
9 changes: 4 additions & 5 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ func NewCommand(name, desc string, setFn ...func(c *Command)) *Command {
Desc: desc,
}

// init set name
c.Flags.SetName(name)

// has config func
if len(setFn) > 0 {
c.Config = setFn[0]
}

// set name
c.Arguments.SetName(name)
return c
}

Expand Down Expand Up @@ -263,8 +263,7 @@ func (c *Command) initialize() {
}

// init for cmd Arguments
c.Arguments.SetName(cName)
// c.Arguments.SetValidateNum(gOpts.strictMode)
c.Flags.SetName(cName)

// init for cmd flags
c.InitFlagSet(cName)
Expand Down
2 changes: 1 addition & 1 deletion gflag/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

// Arguments definition
type Arguments struct {
// Inherited from Command
// name inherited from gcli.Command
name string
// args definition for a command.
//
Expand Down
16 changes: 13 additions & 3 deletions gflag/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ import (

// CliOpts cli options manage TODO
type CliOpts struct {
// name inherited from gcli.Command
name string

// the options flag set TODO remove flag.FlagSet, custom implement parse
fSet *flag.FlagSet
// all option names of the command. {name: length} // TODO delete, move len to opts.
// all cli option names.
// format: {name: length} // TODO delete, move len to opts.
names map[string]int
// metadata for all options
opts map[string]*CliOpt // TODO support option category
Expand All @@ -31,7 +35,7 @@ type CliOpts struct {
// eg: "-V, --version" length is 13
optMaxLen int
// exist short names. useful for render help
existShort bool
hasShort bool
}

// InitFlagSet create and init flag.FlagSet
Expand All @@ -44,13 +48,19 @@ func (ops *CliOpts) InitFlagSet(name string) {
// ops.cfg = newDefaultFlagConfig()
// }

ops.name = name
ops.fSet = flag.NewFlagSet(name, flag.ContinueOnError)
// disable output internal error message on parse flags
ops.fSet.SetOutput(io.Discard)
// nothing to do ... render usage on after parsed
ops.fSet.Usage = func() {}
}

// SetName for Arguments
func (ops *CliOpts) SetName(name string) {
ops.name = name
}

/***********************************************************************
* Options:
* - binding option var
Expand Down Expand Up @@ -319,7 +329,7 @@ func (ops *CliOpts) checkShortNames(name string, shorts []string) {
return
}

ops.existShort = true
ops.hasShort = true
if ops.shorts == nil {
ops.shorts = map[string]string{}
}
Expand Down
8 changes: 8 additions & 0 deletions gflag/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Parser struct {
// --- cli arguments ---
Arguments

name string
// Desc message
Desc string
// AfterParse options hook
Expand Down Expand Up @@ -73,6 +74,13 @@ func New(nameWithDesc ...string) *Parser {
return p
}

// SetName for parser
func (p *Parser) SetName(name string) {
p.name = name
p.CliOpts.SetName(name)
p.Arguments.SetName(name)
}

// SetConfig for the object.
func (p *Parser) SetConfig(opt *Config) { p.cfg = opt }

Expand Down
3 changes: 2 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/gookit/color"
"github.com/gookit/gcli/v3/helper"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
)

Expand Down Expand Up @@ -58,7 +59,7 @@ Use "<cyan>{$binName} COMMAND -h</>" for more information about a command

// display app help and list all commands. showCommandList()
func (app *App) showApplicationHelp() bool {
Debugf("render application help and commands list")
Debugf("render application help and commands list, help.pairs=%s", maputil.ToString2(app.Vars))

// cmdHelpTemplate = color.ReplaceTag(cmdHelpTemplate)
// render help text template
Expand Down

0 comments on commit 3482598

Please sign in to comment.