From 53fd438f2d03dcc1452ad22caa90808fb00b2805 Mon Sep 17 00:00:00 2001 From: gdm85 Date: Wed, 19 Dec 2018 20:22:25 +0100 Subject: [PATCH] Add support for IsSpecified Removes Init method to standardize on NewFlagSet --- export_test.go | 1 + flag.go | 26 ++++++++++++-------------- flag_test.go | 28 ++++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/export_test.go b/export_test.go index 9318fee0..1d6f1e47 100644 --- a/export_test.go +++ b/export_test.go @@ -19,6 +19,7 @@ func ResetForTesting(usage func()) { name: os.Args[0], errorHandling: ContinueOnError, output: ioutil.Discard, + actual: map[string]*Flag{}, } Usage = usage } diff --git a/flag.go b/flag.go index 9d1e0ca5..f0dc431e 100644 --- a/flag.go +++ b/flag.go @@ -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] @@ -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 } @@ -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 @@ -606,6 +611,7 @@ func NewFlagSet(name string, errorHandling ErrorHandling) *FlagSet { name: name, errorHandling: errorHandling, interspersed: true, + actual: map[string]*Flag{}, } return f } @@ -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 -} diff --git a/flag_test.go b/flag_test.go index 82f7fae7..5cbb5c55 100644 --- a/flag_test.go +++ b/flag_test.go @@ -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 { @@ -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) @@ -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") + } +}