Skip to content

Commit

Permalink
✨ feat(opt): support bind default value from flag ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 14, 2023
1 parent a3fda82 commit 0b038f5
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion gflag/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ func (ops *CliOpts) strOpt(p *string, opt *CliOpt) {
defVal := opt.DValue().String()
name := ops.checkFlagInfo(opt)

// use *p as default value
if defVal == "" && *p != "" {
defVal = *p
}

// binding option to flag.FlagSet
ops.fSet.StringVar(p, opt.Name, defVal, opt.Desc)
opt.flag = ops.fSet.Lookup(name)
Expand Down Expand Up @@ -211,6 +216,11 @@ func (ops *CliOpts) intOpt(ptr *int, opt *CliOpt) {
defVal := opt.DValue().Int()
name := ops.checkFlagInfo(opt)

// use *p as default value
if defVal == 0 && *ptr != 0 {
defVal = *ptr
}

// binding option to flag.FlagSet
ops.fSet.IntVar(ptr, name, defVal, opt.Desc)
opt.flag = ops.fSet.Lookup(name)
Expand All @@ -224,7 +234,6 @@ func (ops *CliOpts) Int64(name, shorts string, defVal int64, desc string) *int64
// binding option to flag.FlagSet
p := ops.fSet.Int64(name, defVal, opt.Desc)
opt.flag = ops.fSet.Lookup(name)

return p
}

Expand All @@ -240,6 +249,11 @@ func (ops *CliOpts) int64Opt(ptr *int64, opt *CliOpt) {
defVal := opt.DValue().Int64()
name := ops.checkFlagInfo(opt)

// use *p as default value
if defVal == 0 && *ptr != 0 {
defVal = *ptr
}

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

0 comments on commit 0b038f5

Please sign in to comment.