Skip to content

Commit

Permalink
WIP: flagcodec: enable flag normalization
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Nov 29, 2023
1 parent b5639b3 commit d7b7a0f
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions pkg/flagcodec/flagcodec.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ type Val struct {
}

type Flags struct {
command string
args map[string]Val
keys []string
command string
args map[string]Val
keys []string
processFlagName func(string) string
}

// ParseArgvKeyValue parses a clean (trimmed) argv whose components
Expand All @@ -67,8 +68,9 @@ func ParseArgvKeyValue(args []string) *Flags {
// The command is supplied explicitely as parameter.
func ParseArgvKeyValueWithCommand(command string, args []string) *Flags {
ret := &Flags{
command: command,
args: make(map[string]Val),
command: command,
args: make(map[string]Val),
processFlagName: func(v string) string { return v },
}
for _, arg := range args {
fields := strings.SplitN(arg, "=", 2)
Expand All @@ -81,6 +83,29 @@ func ParseArgvKeyValueWithCommand(command string, args []string) *Flags {
return ret
}

func normalizeFlagName(v string) string {
if len(v) == 3 && v[0] == '-' && v[1] == '-' {
// single char, double dash flag (ugly?), fix it
return v[1:]
}
// everything else pass through silently
return v
}

// EnableNormalizeFlags optionally enables flag normalization.
// The canonical representation of flags in this package is:
// * single-dash for one-char flags (-v, -h)
// * double-dash for multi-char flags (--foo, --long-option)
// pflag allows one-char to have one or two dashes. For flagcodec
// these were different options. When normalization is enabled,
// though, all flag names are processed to adhere to the canonical
// representation, so flagcodec will treat `--v` and `-v` to
// be the same flag. Since this is possibly breaking change,
// this treatment is opt-in.
func (fl *Flags) EnableNormalizeFlags() {
fl.processFlagName = normalizeFlagName
}

func (fl *Flags) recordFlag(name string) {
if _, ok := fl.args[name]; !ok {
fl.keys = append(fl.keys, name)
Expand All @@ -99,13 +124,15 @@ func (fl *Flags) forgetFlag(name string) {
}

func (fl *Flags) SetToggle(name string) {
name = fl.processFlagName(name)
fl.recordFlag(name)
fl.args[name] = Val{
Kind: FlagToggle,
}
}

func (fl *Flags) SetOption(name, data string) {
name = fl.processFlagName(name)
fl.recordFlag(name)
fl.args[name] = Val{
Kind: FlagOption,
Expand All @@ -114,6 +141,7 @@ func (fl *Flags) SetOption(name, data string) {
}

func (fl *Flags) Delete(name string) {
name = fl.processFlagName(name)
fl.forgetFlag(name)
delete(fl.args, name)
}
Expand All @@ -139,6 +167,7 @@ func (fl *Flags) Argv() []string {
}

func (fl *Flags) GetFlag(name string) (Val, bool) {
name = fl.processFlagName(name)
if val, ok := fl.args[name]; ok {
return val, ok
}
Expand Down

0 comments on commit d7b7a0f

Please sign in to comment.