Moved from flag module singleton usage to FlagSet and fixed tests.
This commit is contained in:
31
flagger.go
31
flagger.go
@@ -27,6 +27,7 @@ import (
|
||||
// stdlib
|
||||
"errors"
|
||||
"flag"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -45,13 +46,14 @@ type Flagger struct {
|
||||
flagsBool map[string]*bool
|
||||
flagsInt map[string]*int
|
||||
flagsString map[string]*string
|
||||
|
||||
flagSet *flag.FlagSet
|
||||
}
|
||||
|
||||
// Adds flag to list of flags we will pass to ``flag`` package.
|
||||
// AddFlag adds flag to list of flags we will pass to ``flag`` package.
|
||||
func (f *Flagger) AddFlag(flag *Flag) error {
|
||||
_, present := f.flags[flag.Name]
|
||||
if present {
|
||||
logger.Fatal("Cannot add flag '" + flag.Name + "' - already added!")
|
||||
return errors.New("Cannot add flag '" + flag.Name + "' - already added!")
|
||||
}
|
||||
|
||||
@@ -59,7 +61,7 @@ func (f *Flagger) AddFlag(flag *Flag) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// This function returns boolean value for flag with given name.
|
||||
// GetBoolValue returns boolean value for flag with given name.
|
||||
// Returns bool value for flag and nil as error on success
|
||||
// and false bool plus error with text on error.
|
||||
func (f *Flagger) GetBoolValue(name string) (bool, error) {
|
||||
@@ -70,7 +72,7 @@ func (f *Flagger) GetBoolValue(name string) (bool, error) {
|
||||
return (*fl), nil
|
||||
}
|
||||
|
||||
// This function returns integer value for flag with given name.
|
||||
// GetIntValue returns integer value for flag with given name.
|
||||
// Returns integer on success and 0 on error.
|
||||
func (f *Flagger) GetIntValue(name string) (int, error) {
|
||||
fl, present := f.flagsInt[name]
|
||||
@@ -80,7 +82,7 @@ func (f *Flagger) GetIntValue(name string) (int, error) {
|
||||
return (*fl), nil
|
||||
}
|
||||
|
||||
// This function returns string value for flag with given name.
|
||||
// GetStringValue returns string value for flag with given name.
|
||||
// Returns string on success or empty string on error.
|
||||
func (f *Flagger) GetStringValue(name string) (string, error) {
|
||||
fl, present := f.flagsString[name]
|
||||
@@ -90,7 +92,7 @@ func (f *Flagger) GetStringValue(name string) (string, error) {
|
||||
return (*fl), nil
|
||||
}
|
||||
|
||||
// Flagger initialization.
|
||||
// Initialize initializes Flagger.
|
||||
func (f *Flagger) Initialize() {
|
||||
logger.Print("Initializing CLI parameters parser...")
|
||||
|
||||
@@ -99,28 +101,35 @@ func (f *Flagger) Initialize() {
|
||||
f.flagsBool = make(map[string]*bool)
|
||||
f.flagsInt = make(map[string]*int)
|
||||
f.flagsString = make(map[string]*string)
|
||||
|
||||
f.flagSet = flag.NewFlagSet("flagger", flag.ContinueOnError)
|
||||
}
|
||||
|
||||
// This function adds flags from flags map to flag package and parse
|
||||
// Parse adds flags from flags map to flag package and parse
|
||||
// them. They can be obtained later by calling GetTYPEValue(name),
|
||||
// where TYPE is one of Bool, Int, String.
|
||||
func (f *Flagger) Parse() {
|
||||
// If flags was already parsed - do nothing.
|
||||
if f.flagSet.Parsed() {
|
||||
return
|
||||
}
|
||||
|
||||
for name, fl := range f.flags {
|
||||
if fl.Type == "bool" {
|
||||
fdef := fl.DefaultValue.(bool)
|
||||
f.flagsBool[name] = &fdef
|
||||
flag.BoolVar(&fdef, name, fdef, fl.Description)
|
||||
f.flagSet.BoolVar(&fdef, name, fdef, fl.Description)
|
||||
} else if fl.Type == "int" {
|
||||
fdef := fl.DefaultValue.(int)
|
||||
f.flagsInt[name] = &fdef
|
||||
flag.IntVar(&fdef, name, fdef, fl.Description)
|
||||
f.flagSet.IntVar(&fdef, name, fdef, fl.Description)
|
||||
} else if fl.Type == "string" {
|
||||
fdef := fl.DefaultValue.(string)
|
||||
f.flagsString[name] = &fdef
|
||||
flag.StringVar(&fdef, name, fdef, fl.Description)
|
||||
f.flagSet.StringVar(&fdef, name, fdef, fl.Description)
|
||||
}
|
||||
}
|
||||
|
||||
logger.Print("Parsing CLI parameters...")
|
||||
flag.Parse()
|
||||
f.flagSet.Parse(os.Args)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user