Skip to content

Commit

Permalink
up: update some app and cmd method name, update some type name.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 30, 2022
1 parent 9feede0 commit 3479377
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 71 deletions.
4 changes: 2 additions & 2 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ func (app *App) bindAppOpts() {
// }

// binding global options
app.opts.bindingFlags(fs)
app.opts.bindingOpts(fs)
// add more ...
// This is an internal option
fs.BoolVar(&gOpts.inCompletion, &gflag.FlagMeta{
fs.BoolVar(&gOpts.inCompletion, &gflag.CliOpt{
Name: "in-completion",
Desc: "generate completion scripts for bash/zsh",
// hidden it
Expand Down
23 changes: 9 additions & 14 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const maxFunc = 64
// HandlersChain middleware handlers chain definition
type HandlersChain []RunnerFunc

// Last returns the last handler in the chain. ie. the last handler is the main own.
// Last returns the last handler in the chain. tip: the last handler is the main own.
func (c HandlersChain) Last() RunnerFunc {
length := len(c)
if length > 0 {
Expand All @@ -49,9 +49,7 @@ type Command struct {
// internal use
base

// --- provide option and argument parse and binding.

// Flags (options+arguments) for the command
// Flags cli (options+arguments) parse and manage for the command
gflag.Flags

// Name is the command name.
Expand Down Expand Up @@ -111,7 +109,7 @@ type Command struct {
// command is standalone running.
standalone bool
// global option binding on standalone. deny error on repeat run.
goptBounded bool
gOptBounded bool
}

// NewCommand create a new command instance.
Expand All @@ -126,7 +124,6 @@ func NewCommand(name, desc string, setFn ...func(c *Command)) *Command {
c := &Command{
Name: name,
Desc: desc,
// Flags: *NewFlags(name, desc),
}

// has config func
Expand Down Expand Up @@ -269,8 +266,8 @@ func (c *Command) initialize() {
c.Arguments.SetName(cName)
// c.Arguments.SetValidateNum(gOpts.strictMode)

// init for cmd Flags
c.Flags.InitFlagSet(cName)
// init for cmd flags
c.InitFlagSet(cName)

// format description
if len(c.Desc) > 0 {
Expand Down Expand Up @@ -391,11 +388,10 @@ func (c *Command) Run(args []string) (err error) {
}

// binding global options
if !c.goptBounded {
if !c.gOptBounded {
Debugf("cmd: %s - binding global options on standalone mode", c.Name)
// bindingCommonGOpts(&c.Flags)
gOpts.bindingFlags(&c.Flags)
c.goptBounded = true
gOpts.bindingOpts(&c.Flags)
c.gOptBounded = true
}

// dispatch and parse flags and execute command
Expand Down Expand Up @@ -512,7 +508,7 @@ func (c *Command) parseOptions(args []string) (ss []string, err error) {
}

// remaining args
return c.Flags.RawArgs(), nil
return c.RawArgs(), nil
}

// prepare: before execute the command
Expand Down Expand Up @@ -662,7 +658,6 @@ func (c *Command) Copy() *Command {
// reset some fields
nc.Func = nil
nc.Hooks.ResetHooks() // TODO bug, will clear c.Hooks
// nc.Flags = flag.FlagSet{}

return &nc
}
Expand Down
2 changes: 1 addition & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func TestCommand_Run_emptyArgs(t *testing.T) {

is.NoErr(err)
is.Eq("name=test", bf.String())
is.Eq("int desc", c0.FlagMeta("int").Desc)
is.Eq("int desc", c0.Opt("int").Desc)
is.NotEmpty(c0.Args())
is.Eq("arg0", c0.Arg("arg0").Name)
}
Expand Down
16 changes: 8 additions & 8 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ func GCtx() *Context {
return gCtx
}

// Flags alias of the gflag.Flags
type Flags = gflag.Flags
// Flags alias of the gflag.Parser
type Flags = gflag.Parser

// FlagMeta alias of the gflag.Option
type FlagMeta = gflag.Option
// FlagMeta alias of the gflag.CliOpt
type FlagMeta = gflag.CliOpt

// FlagsConfig alias of the gflag.FlagsConfig
type FlagsConfig = gflag.FlagsConfig
// FlagsConfig alias of the gflag.Config
type FlagsConfig = gflag.Config

// NewFlags create new gflag.Flags
func NewFlags(nameWithDesc ...string) *gflag.Flags {
Expand Down Expand Up @@ -141,9 +141,9 @@ func (g *GlobalOpts) SetDisable() {
g.Disable = true
}

func (g *GlobalOpts) bindingFlags(fs *Flags) {
func (g *GlobalOpts) bindingOpts(fs *gflag.Parser) {
fs.BoolOpt(&g.ShowHelp, "help", "h", false, "Display the help information")
fs.AfterParse = func(_ *Flags) error {
fs.AfterParse = func(_ *gflag.Parser) error {
// return ErrHelp on ShowHelp=true
if g.ShowHelp {
return flag.ErrHelp
Expand Down
87 changes: 42 additions & 45 deletions gflag/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (ops *CliOpts) InitFlagSet(name string) {

// Bool binding a bool option flag, return pointer
func (ops *CliOpts) Bool(name, shorts string, defVal bool, desc string) *bool {
opt := newFlagOpt(name, desc, defVal, shorts)
opt := newCliOpt(name, desc, defVal, shorts)
name = ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
Expand All @@ -75,7 +75,7 @@ func (ops *CliOpts) BoolVar(ptr *bool, opt *CliOpt) { ops.boolOpt(ptr, opt) }

// BoolOpt binding a bool option
func (ops *CliOpts) BoolOpt(ptr *bool, name, shorts string, defVal bool, desc string) {
ops.boolOpt(ptr, newFlagOpt(name, desc, defVal, shorts))
ops.boolOpt(ptr, newCliOpt(name, desc, defVal, shorts))
}

// binding option and shorts
Expand All @@ -95,7 +95,7 @@ func (ops *CliOpts) Float64Var(ptr *float64, opt *CliOpt) { ops.float64Opt(ptr,

// Float64Opt binding a float64 option
func (ops *CliOpts) Float64Opt(p *float64, name, shorts string, defVal float64, desc string) {
ops.float64Opt(p, newFlagOpt(name, desc, defVal, shorts))
ops.float64Opt(p, newCliOpt(name, desc, defVal, shorts))
}

func (ops *CliOpts) float64Opt(p *float64, opt *CliOpt) {
Expand All @@ -111,7 +111,7 @@ func (ops *CliOpts) float64Opt(p *float64, opt *CliOpt) {

// Str binding an string option flag, return pointer
func (ops *CliOpts) Str(name, shorts string, defValue, desc string) *string {
opt := newFlagOpt(name, desc, defValue, shorts)
opt := newCliOpt(name, desc, defValue, shorts)
name = ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
Expand All @@ -126,7 +126,7 @@ func (ops *CliOpts) StrVar(p *string, opt *CliOpt) { ops.strOpt(p, opt) }

// StrOpt binding an string option
func (ops *CliOpts) StrOpt(p *string, name, shorts, defValue, desc string) {
ops.strOpt(p, newFlagOpt(name, desc, defValue, shorts))
ops.strOpt(p, newCliOpt(name, desc, defValue, shorts))
}

// binding option and shorts
Expand All @@ -143,36 +143,36 @@ func (ops *CliOpts) strOpt(p *string, opt *CliOpt) {

// Int binding an int option flag, return pointer
func (ops *CliOpts) Int(name, shorts string, defValue int, desc string) *int {
opt := newFlagOpt(name, desc, defValue, shorts)
opt := newCliOpt(name, desc, defValue, shorts)
name = ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
p := ops.fSet.Int(name, defValue, opt.Desc)
ptr := ops.fSet.Int(name, defValue, opt.Desc)
opt.flag = ops.fSet.Lookup(name)

return p
return ptr
}

// IntVar binding an int option flag
func (ops *CliOpts) IntVar(p *int, opt *CliOpt) { ops.intOpt(p, opt) }
func (ops *CliOpts) IntVar(ptr *int, opt *CliOpt) { ops.intOpt(ptr, opt) }

// IntOpt binding an int option
func (ops *CliOpts) IntOpt(p *int, name, shorts string, defValue int, desc string) {
ops.intOpt(p, newFlagOpt(name, desc, defValue, shorts))
func (ops *CliOpts) IntOpt(ptr *int, name, shorts string, defValue int, desc string) {
ops.intOpt(ptr, newCliOpt(name, desc, defValue, shorts))
}

func (ops *CliOpts) intOpt(p *int, opt *CliOpt) {
func (ops *CliOpts) intOpt(ptr *int, opt *CliOpt) {
defValue := opt.DValue().Int()
name := ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
ops.fSet.IntVar(p, name, defValue, opt.Desc)
ops.fSet.IntVar(ptr, name, defValue, opt.Desc)
opt.flag = ops.fSet.Lookup(name)
}

// Int64 binding an int64 option flag, return pointer
func (ops *CliOpts) Int64(name, shorts string, defValue int64, desc string) *int64 {
opt := newFlagOpt(name, desc, defValue, shorts)
opt := newCliOpt(name, desc, defValue, shorts)
name = ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
Expand All @@ -183,101 +183,101 @@ func (ops *CliOpts) Int64(name, shorts string, defValue int64, desc string) *int
}

// Int64Var binding an int64 option flag
func (ops *CliOpts) Int64Var(p *int64, opt *CliOpt) { ops.int64Opt(p, opt) }
func (ops *CliOpts) Int64Var(ptr *int64, opt *CliOpt) { ops.int64Opt(ptr, opt) }

// Int64Opt binding an int64 option
func (ops *CliOpts) Int64Opt(p *int64, name, shorts string, defValue int64, desc string) {
ops.int64Opt(p, newFlagOpt(name, desc, defValue, shorts))
func (ops *CliOpts) Int64Opt(ptr *int64, name, shorts string, defValue int64, desc string) {
ops.int64Opt(ptr, newCliOpt(name, desc, defValue, shorts))
}

func (ops *CliOpts) int64Opt(p *int64, opt *CliOpt) {
func (ops *CliOpts) int64Opt(ptr *int64, opt *CliOpt) {
defVal := opt.DValue().Int64()
name := ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
ops.fSet.Int64Var(p, name, defVal, opt.Desc)
ops.fSet.Int64Var(ptr, name, defVal, opt.Desc)
opt.flag = ops.fSet.Lookup(name)
}

// --- uintX option

// Uint binding an int option flag, return pointer
func (ops *CliOpts) Uint(name, shorts string, defVal uint, desc string) *uint {
opt := newFlagOpt(name, desc, defVal, shorts)
opt := newCliOpt(name, desc, defVal, shorts)
name = ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
p := ops.fSet.Uint(name, defVal, opt.Desc)
ptr := ops.fSet.Uint(name, defVal, opt.Desc)
opt.flag = ops.fSet.Lookup(name)

return p
return ptr
}

// UintVar binding an uint option flag
func (ops *CliOpts) UintVar(p *uint, opt *CliOpt) { ops.uintOpt(p, opt) }
func (ops *CliOpts) UintVar(ptr *uint, opt *CliOpt) { ops.uintOpt(ptr, opt) }

// UintOpt binding an uint option
func (ops *CliOpts) UintOpt(p *uint, name, shorts string, defValue uint, desc string) {
ops.uintOpt(p, newFlagOpt(name, desc, defValue, shorts))
func (ops *CliOpts) UintOpt(ptr *uint, name, shorts string, defValue uint, desc string) {
ops.uintOpt(ptr, newCliOpt(name, desc, defValue, shorts))
}

func (ops *CliOpts) uintOpt(p *uint, opt *CliOpt) {
func (ops *CliOpts) uintOpt(ptr *uint, opt *CliOpt) {
defVal := opt.DValue().Int()
name := ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
ops.fSet.UintVar(p, name, uint(defVal), opt.Desc)
ops.fSet.UintVar(ptr, name, uint(defVal), opt.Desc)
opt.flag = ops.fSet.Lookup(name)
}

// Uint64 binding an int option flag, return pointer
func (ops *CliOpts) Uint64(name, shorts string, defVal uint64, desc string) *uint64 {
opt := newFlagOpt(name, desc, defVal, shorts)
opt := newCliOpt(name, desc, defVal, shorts)
name = ops.checkFlagInfo(opt)

p := ops.fSet.Uint64(name, defVal, opt.Desc)
ptr := ops.fSet.Uint64(name, defVal, opt.Desc)
opt.flag = ops.fSet.Lookup(name)

return p
return ptr
}

// Uint64Var binding an uint option flag
func (ops *CliOpts) Uint64Var(p *uint64, opt *CliOpt) { ops.uint64Opt(p, opt) }
func (ops *CliOpts) Uint64Var(ptr *uint64, opt *CliOpt) { ops.uint64Opt(ptr, opt) }

// Uint64Opt binding an uint64 option
func (ops *CliOpts) Uint64Opt(p *uint64, name, shorts string, defVal uint64, desc string) {
ops.uint64Opt(p, newFlagOpt(name, desc, defVal, shorts))
func (ops *CliOpts) Uint64Opt(ptr *uint64, name, shorts string, defVal uint64, desc string) {
ops.uint64Opt(ptr, newCliOpt(name, desc, defVal, shorts))
}

// binding option and shorts
func (ops *CliOpts) uint64Opt(p *uint64, opt *CliOpt) {
func (ops *CliOpts) uint64Opt(ptr *uint64, opt *CliOpt) {
defVal := opt.DValue().Int64()
name := ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
ops.fSet.Uint64Var(p, name, uint64(defVal), opt.Desc)
ops.fSet.Uint64Var(ptr, name, uint64(defVal), opt.Desc)
opt.flag = ops.fSet.Lookup(name)
}

// Var binding an custom var option flag
func (ops *CliOpts) Var(p flag.Value, opt *CliOpt) { ops.varOpt(p, opt) }
func (ops *CliOpts) Var(ptr flag.Value, opt *CliOpt) { ops.varOpt(ptr, opt) }

// VarOpt binding a custom var option
//
// Usage:
//
// var names gcli.Strings
// cmd.VarOpt(&names, "tables", "t", "description ...")
func (ops *CliOpts) VarOpt(p flag.Value, name, shorts, desc string) {
ops.varOpt(p, newFlagOpt(name, desc, nil, shorts))
func (ops *CliOpts) VarOpt(v flag.Value, name, shorts, desc string) {
ops.varOpt(v, newCliOpt(name, desc, nil, shorts))
}

// binding option and shorts
func (ops *CliOpts) varOpt(p flag.Value, opt *CliOpt) {
func (ops *CliOpts) varOpt(v flag.Value, opt *CliOpt) {
name := ops.checkFlagInfo(opt)

// binding option to flag.FlagSet
ops.fSet.Var(p, name, opt.Desc)
ops.fSet.Var(v, name, opt.Desc)
opt.flag = ops.fSet.Lookup(name)
}

Expand Down Expand Up @@ -400,9 +400,6 @@ func (ops *CliOpts) Opts() map[string]*CliOpt { return ops.opts }
* flag options metadata
***********************************************************************/

// FlagMeta alias of flag CliOpt
type FlagMeta = CliOpt

// CliOpt define for a flag option
type CliOpt struct {
// go flag value
Expand Down Expand Up @@ -430,8 +427,8 @@ type CliOpt struct {
Question string
}

// newFlagOpt quick create an CliOpt
func newFlagOpt(name, desc string, defVal any, shortcut string) *CliOpt {
// newCliOpt quick create an CliOpt
func newCliOpt(name, desc string, defVal any, shortcut string) *CliOpt {
return &CliOpt{
Name: name,
Desc: desc,
Expand Down
2 changes: 1 addition & 1 deletion gflag/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (p *Parser) FromStruct(ptr any) error {
optName = strutil.SnakeCase(name, "-")
}

opt := newFlagOpt(optName, mp["desc"], mp["default"], mp["shorts"])
opt := newCliOpt(optName, mp["desc"], mp["default"], mp["shorts"])
if must, has := mp["required"]; has {
opt.Required = strutil.MustBool(must)
}
Expand Down

0 comments on commit 3479377

Please sign in to comment.