Skip to content

Commit

Permalink
Merge pull request #18 from rsteube/optargdelimiter-defaultvalue
Browse files Browse the repository at this point in the history
OptargDelimiter: default value
  • Loading branch information
rsteube authored Jan 20, 2023
2 parents b1839ba + efffd37 commit ca4c80e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
54 changes: 25 additions & 29 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,6 @@ type Flag struct {
OptargDelimiter rune
}

func (f Flag) getOptargDelimiter() string {
if f.OptargDelimiter == 0 {
return "="
}
return string(f.OptargDelimiter)
}

// Value is the interface to the dynamic value stored in a flag.
// (The default value is represented as a string.)
type Value interface {
Expand Down Expand Up @@ -874,12 +867,13 @@ func (f *FlagSet) Var(value Value, name string, usage string) {
func (f *FlagSet) VarNF(value Value, name string, shorthand string, usage string) *Flag {
// Remember the default value as a string; it won't change.
flag := &Flag{
Name: name,
Shorthand: shorthand,
Style: NameAsShorthand,
Usage: usage,
Value: value,
DefValue: value.String(),
Name: name,
Shorthand: shorthand,
Style: NameAsShorthand,
Usage: usage,
Value: value,
DefValue: value.String(),
OptargDelimiter: '=',
}
f.AddFlag(flag)
return flag
Expand All @@ -894,11 +888,12 @@ func (f *FlagSet) VarN(value Value, name, shorthand, usage string) {
func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag {
// Remember the default value as a string; it won't change.
flag := &Flag{
Name: name,
Shorthand: shorthand,
Usage: usage,
Value: value,
DefValue: value.String(),
Name: name,
Shorthand: shorthand,
Usage: usage,
Value: value,
DefValue: value.String(),
OptargDelimiter: '=',
}
f.AddFlag(flag)
return flag
Expand All @@ -913,12 +908,13 @@ func (f *FlagSet) VarP(value Value, name, shorthand, usage string) {
func (f *FlagSet) VarSF(value Value, name string, shorthand string, usage string) *Flag {
// Remember the default value as a string; it won't change.
flag := &Flag{
Name: name,
Shorthand: shorthand,
Style: ShorthandOnly,
Usage: usage,
Value: value,
DefValue: value.String(),
Name: name,
Shorthand: shorthand,
Style: ShorthandOnly,
Usage: usage,
Value: value,
DefValue: value.String(),
OptargDelimiter: '=',
}
f.AddFlag(flag)
return flag
Expand Down Expand Up @@ -1052,7 +1048,7 @@ func stripUnknownFlagValue(args []string) []string {

func (f *FlagSet) findLongFlag(s string) (*Flag, bool) {
for formalName, flag := range f.formal {
if name := strings.SplitN(s, flag.getOptargDelimiter(), 2)[0]; f.normalizeFlagName(name) == formalName {
if name := strings.SplitN(s, string(flag.OptargDelimiter), 2)[0]; f.normalizeFlagName(name) == formalName {
return flag, true
}
}
Expand All @@ -1071,7 +1067,7 @@ func (f *FlagSet) parseLongArg(s string, args []string, fn parseFunc) (outArgs [

delimiter := "="
if exists {
delimiter = flag.getOptargDelimiter()
delimiter = string(flag.OptargDelimiter)
}
split := strings.SplitN(name, delimiter, 2)

Expand Down Expand Up @@ -1121,7 +1117,7 @@ func (f *FlagSet) parseLongArg(s string, args []string, fn parseFunc) (outArgs [

func (f *FlagSet) findShortFlag(s string) (*Flag, bool) {
for shorthand, flag := range f.shorthands {
if name := strings.SplitN(s, flag.getOptargDelimiter(), 2)[0]; name == shorthand {
if name := strings.SplitN(s, string(flag.OptargDelimiter), 2)[0]; name == shorthand {
return flag, true
}
}
Expand Down Expand Up @@ -1174,9 +1170,9 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parse

var value string
if len(shorthands) > 2 &&
((shorthands[1] == '=' && f.IsPosix()) || (strings.Contains(shorthands, flag.getOptargDelimiter()) && !f.IsPosix())) {
((shorthands[1] == '=' && f.IsPosix()) || (strings.Contains(shorthands, string(flag.OptargDelimiter)) && !f.IsPosix())) {
// '-f=arg'
value = strings.SplitN(shorthands, flag.getOptargDelimiter(), 2)[1]
value = strings.SplitN(shorthands, string(flag.OptargDelimiter), 2)[1]
outShorts = ""
} else if flag.NoOptDefVal != "" {
// '-f' (arg was optional)
Expand Down
3 changes: 2 additions & 1 deletion golangflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func PFlagFromGoFlag(goflag *goflag.Flag) *Flag {
Value: wrapFlagValue(goflag.Value),
// Looks like golang flags don't set DefValue correctly :-(
//DefValue: goflag.DefValue,
DefValue: goflag.Value.String(),
DefValue: goflag.Value.String(),
OptargDelimiter: '=',
}
// Ex: if the golang flag was -v, allow both -v and --v to work
if len(flag.Name) == 1 {
Expand Down

0 comments on commit ca4c80e

Please sign in to comment.