Skip to content

Commit

Permalink
up: rename FlagMeta.Flag to FlagMeta.flag
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 27, 2022
1 parent 517b05e commit c1d2ceb
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (fs *Flags) Bool(name, shorts string, defVal bool, desc string) *bool {

// binding option to flag.FlagSet
p := fs.fSet.Bool(name, defVal, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)

return p
}
Expand All @@ -411,7 +411,7 @@ func (fs *Flags) boolOpt(p *bool, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.BoolVar(p, name, defVal, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// --- float option
Expand All @@ -430,7 +430,7 @@ func (fs *Flags) float64Opt(p *float64, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.Float64Var(p, name, defVal, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// --- string option
Expand All @@ -442,7 +442,7 @@ func (fs *Flags) Str(name, shorts string, defValue, desc string) *string {

// binding option to flag.FlagSet
p := fs.fSet.String(name, defValue, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)

return p
}
Expand All @@ -462,7 +462,7 @@ func (fs *Flags) strOpt(p *string, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.StringVar(p, meta.Name, defVal, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// --- intX option
Expand All @@ -474,7 +474,7 @@ func (fs *Flags) Int(name, shorts string, defValue int, desc string) *int {

// binding option to flag.FlagSet
p := fs.fSet.Int(name, defValue, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)

return p
}
Expand All @@ -493,7 +493,7 @@ func (fs *Flags) intOpt(p *int, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.IntVar(p, name, defValue, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// Int64 binding an int64 option flag, return pointer
Expand All @@ -503,7 +503,7 @@ func (fs *Flags) Int64(name, shorts string, defValue int64, desc string) *int64

// binding option to flag.FlagSet
p := fs.fSet.Int64(name, defValue, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)

return p
}
Expand All @@ -522,7 +522,7 @@ func (fs *Flags) int64Opt(p *int64, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.Int64Var(p, name, defVal, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// --- uintX option
Expand All @@ -534,7 +534,7 @@ func (fs *Flags) Uint(name, shorts string, defVal uint, desc string) *uint {

// binding option to flag.FlagSet
p := fs.fSet.Uint(name, defVal, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)

return p
}
Expand All @@ -553,7 +553,7 @@ func (fs *Flags) uintOpt(p *uint, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.UintVar(p, name, uint(defVal), meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// Uint64 binding an int option flag, return pointer
Expand All @@ -562,7 +562,7 @@ func (fs *Flags) Uint64(name, shorts string, defVal uint64, desc string) *uint64
name = fs.checkFlagInfo(meta)

p := fs.fSet.Uint64(name, defVal, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)

return p
}
Expand All @@ -582,7 +582,7 @@ func (fs *Flags) uint64Opt(p *uint64, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.Uint64Var(p, name, uint64(defVal), meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// Var binding an custom var option flag
Expand All @@ -603,7 +603,7 @@ func (fs *Flags) varOpt(p flag.Value, meta *FlagMeta) {

// binding option to flag.FlagSet
fs.fSet.Var(p, name, meta.Desc)
meta.Flag = fs.fSet.Lookup(name)
meta.flag = fs.fSet.Lookup(name)
}

// Required flag option name(s)
Expand Down Expand Up @@ -903,8 +903,8 @@ func (fs *Flags) FSetArgs() []string { return fs.fSet.Args() }

// FlagMeta for an flag(option/argument)
type FlagMeta struct {
// Flag value
Flag *flag.Flag
// go flag value
flag *flag.Flag
// Name of flag and description
Name, Desc string
// default value for the flag option
Expand All @@ -913,6 +913,8 @@ type FlagMeta struct {
defVal *structs.Value
// short names. eg: ["o", "a"]
Shorts []string
// EnvVar allow set flag value from ENV var
EnvVar string

// --- advanced settings

Expand Down Expand Up @@ -940,6 +942,7 @@ func (m *FlagMeta) initCheck() string {
desc := strings.Trim(m.Desc, "; ")
if strings.ContainsRune(desc, ';') {
// format: desc;required
// format: desc;required;env TODO parse ENV var
parts := strutil.SplitNTrimmed(desc, ";", 2)
if ln := len(parts); ln > 1 {
bl, err := strutil.Bool(parts[1])
Expand Down Expand Up @@ -1006,6 +1009,11 @@ func (m *FlagMeta) Validate(val string) error {
return nil
}

// Flag value
func (m *FlagMeta) Flag() *flag.Flag {
return m.flag
}

// DValue wrap the default value
func (m *FlagMeta) DValue() *stdutil.Value {
if m.defVal == nil {
Expand Down

0 comments on commit c1d2ceb

Please sign in to comment.