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

Add support for IsSpecified #35

Open
wants to merge 1 commit into
base: master
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 export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func ResetForTesting(usage func()) {
name: os.Args[0],
errorHandling: ContinueOnError,
output: ioutil.Discard,
actual: map[string]*Flag{},
}
Usage = usage
}
Expand Down
26 changes: 12 additions & 14 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,17 @@ func Lookup(name string) *Flag {
return CommandLine.formal[name]
}

// IsSpecified returns true if the named flag was specified via command-line flags.
func (f *FlagSet) IsSpecified(name string) bool {
_, ok := f.actual[name]
return ok
}

// IsSpecified returns true if the named flag was specified via command-line flags.
func IsSpecified(name string) bool {
return CommandLine.IsSpecified(name)
}

// Set sets the value of the named flag.
func (f *FlagSet) Set(name, value string) error {
flag, ok := f.formal[name]
Expand All @@ -225,9 +236,6 @@ func (f *FlagSet) Set(name, value string) error {
if err != nil {
return err
}
if f.actual == nil {
f.actual = make(map[string]*Flag)
}
f.actual[name] = flag
return nil
}
Expand Down Expand Up @@ -465,9 +473,6 @@ func (f *FlagSet) setFlag(flag *Flag, value string, origArg string) error {
return f.failf("invalid argument %q for %s: %v", value, origArg, err)
}
// mark as visited for Visit()
if f.actual == nil {
f.actual = make(map[string]*Flag)
}
f.actual[flag.Name] = flag

return nil
Expand Down Expand Up @@ -606,6 +611,7 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
name: name,
errorHandling: errorHandling,
interspersed: true,
actual: map[string]*Flag{},
}
return f
}
Expand All @@ -614,11 +620,3 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet {
func (f *FlagSet) SetInterspersed(interspersed bool) {
f.interspersed = interspersed
}

// Init sets the name and error handling property for a flag set.
// By default, the zero FlagSet uses an empty name and the
// ContinueOnError error handling policy.
func (f *FlagSet) Init(name string, errorHandling ErrorHandling) {
f.name = name
f.errorHandling = errorHandling
}
28 changes: 24 additions & 4 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,7 @@ func (f *flagVar) Set(value string) error {
}

func TestUserDefined(t *testing.T) {
var flags FlagSet
flags.Init("test", ContinueOnError)
flags := NewFlagSet("test", ContinueOnError)
var v flagVar
flags.VarP(&v, "v", "v", "usage")
if err := flags.Parse([]string{"--v=1", "-v2", "-v", "3"}); err != nil {
Expand All @@ -259,10 +258,9 @@ func TestUserDefined(t *testing.T) {
}

func TestSetOutput(t *testing.T) {
var flags FlagSet
flags := NewFlagSet("test", ContinueOnError)
var buf bytes.Buffer
flags.SetOutput(&buf)
flags.Init("test", ContinueOnError)
flags.Parse([]string{"--unknown"})
if out := buf.String(); !strings.Contains(out, "--unknown") {
t.Logf("expected output mentioning unknown; got %q", out)
Expand Down Expand Up @@ -348,3 +346,25 @@ func TestNoInterspersed(t *testing.T) {
t.Fatal("expected interspersed options/non-options to fail")
}
}

func TestIsSpecified(t *testing.T) {
f := NewFlagSet("test", ContinueOnError)
f.SetInterspersed(false)
f.Bool("alpha", true, "always true")
f.Bool("beta", false, "always false")
err := f.Parse([]string{"--beta"})
if err != nil {
t.Fatal("expected no error; got ", err)
}
args := f.Args()
if len(args) != 0 {
t.Fatal("expected no extra args")
}

if !f.IsSpecified("beta") {
t.Fatal("expected beta to be specified")
}
if f.IsSpecified("alpha") {
t.Fatal("expected alpha to not be specified")
}
}