Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ErrDuplicateFlags when no env var parsing is requested #137

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fftest/test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (tc *ParseTest) Run(t *testing.T) {
parseFunc = fftoml.Parse
case ".env":
parseFunc = ffenv.Parse
opts = append(opts, ff.WithEnvVars())
default:
parseFunc = ff.PlainParser
}
Expand Down
13 changes: 13 additions & 0 deletions flag_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,19 @@ func TestFlagSet_invalid(t *testing.T) {
_ = fs.Bool('a', "apple", "this should panic")
})

t.Run("same fold short name", func(t *testing.T) {
defer func() {
if x := recover(); x == nil {
t.Logf("have not expected panic (%v)", x)
} else {
t.Errorf("want peace, have panic")
}
}()
fs := ff.NewFlagSet(t.Name())
_ = fs.Bool('a', "alpha", "this should be OK")
_ = fs.Bool('A', "apple", "this should be OK")
})

t.Run("duplicate long name", func(t *testing.T) {
defer func() {
if x := recover(); x == nil {
Expand Down
27 changes: 15 additions & 12 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@ func parse(fs Flags, args []string, options ...Option) error {
option(&pc)
}

// Index valid flags by env var key, to support .env config files (below).
env2flag := map[string]Flag{}
{
if err := fs.WalkFlags(func(f Flag) error {
for _, name := range getNameStrings(f) {
key := getEnvVarKey(name, pc.envVarPrefix)
if existing, ok := env2flag[key]; ok {
return fmt.Errorf("%s: %w (%s)", getNameString(f), ErrDuplicateFlag, getNameString(existing))
var env2flag map[string]Flag
if pc.envVarEnabled {
// Index valid flags by env var key, to support .env config files (below).
env2flag = map[string]Flag{}
{
if err := fs.WalkFlags(func(f Flag) error {
for _, name := range getNameStrings(f) {
key := getEnvVarKey(name, pc.envVarPrefix)
if existing, ok := env2flag[key]; ok {
return fmt.Errorf("%s: %w (%s)", getNameString(f), ErrDuplicateFlag, getNameString(existing))
}
env2flag[key] = f
}
env2flag[key] = f
return nil
}); err != nil {
return err
}
return nil
}); err != nil {
return err
}
}

Expand Down
28 changes: 28 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ff_test

import (
"embed"
"errors"
"flag"
"os"
"path/filepath"
Expand Down Expand Up @@ -428,3 +429,30 @@ func TestParse_stdfs(t *testing.T) {
t.Errorf("foo: want %q, have %q", want, have)
}
}

func TestParse_shortSameCase(t *testing.T) {
t.Parallel()

newFS := func() *ff.FlagSet {
fs := ff.NewFlagSet(t.Name())
_ = fs.String('a', "apple", "", "foo string")
_ = fs.String('A', "apricot", "", "FOO string")
return fs
}
args := []string{"-a", "-A"}

t.Run("no env", func(t *testing.T) {
t.Parallel()
if err := ff.Parse(newFS(), args); err != nil {
t.Error(err)
}
})
t.Run("WithEnv", func(t *testing.T) {
t.Parallel()
if err := ff.Parse(
newFS(), args, ff.WithEnvVars(),
); !errors.Is(err, ff.ErrDuplicateFlag) {
t.Errorf("wanted ErrDuplicateFlag, got %+v", err)
}
})
}