flagger/flagger.go

139 lines
4.1 KiB
Go
Raw Normal View History

2017-08-20 14:59:51 +05:00
// Flagger - arbitrary CLI flags parser.
//
// Copyright (c) 2017-2019, Stanislav N. aka pztrn.
2017-08-20 14:59:51 +05:00
//
2018-03-23 08:14:25 +05:00
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
2017-08-20 14:59:51 +05:00
//
2018-03-23 08:14:25 +05:00
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
2017-08-20 14:59:51 +05:00
//
2018-03-23 08:14:25 +05:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2017-08-20 14:59:51 +05:00
package flagger
import (
2018-03-23 08:14:25 +05:00
// stdlib
"errors"
"flag"
"os"
2018-03-23 08:14:25 +05:00
"sync"
2017-08-20 14:59:51 +05:00
)
// Flagger implements (kinda) extended CLI parameters parser. As it
// available from CommonContext, these flags will be available to
// whole application.
//
// It uses reflection to determine what kind of variable we should
// parse or get.
type Flagger struct {
2018-03-23 08:14:25 +05:00
// Flags that was added by user.
flags map[string]*Flag
flagsMutex sync.Mutex
2017-08-20 14:59:51 +05:00
2018-03-23 08:14:25 +05:00
// Flags that will be passed to flag module.
flagsBool map[string]*bool
flagsInt map[string]*int
flagsString map[string]*string
flagSet *flag.FlagSet
2017-08-20 14:59:51 +05:00
}
// AddFlag adds flag to list of flags we will pass to ``flag`` package.
2017-08-20 14:59:51 +05:00
func (f *Flagger) AddFlag(flag *Flag) error {
2018-03-23 08:14:25 +05:00
_, present := f.flags[flag.Name]
if present {
return errors.New("Cannot add flag '" + flag.Name + "' - already added!")
}
2017-08-20 14:59:51 +05:00
2018-03-23 08:14:25 +05:00
f.flags[flag.Name] = flag
return nil
2017-08-20 14:59:51 +05:00
}
// GetBoolValue returns boolean value for flag with given name.
2017-08-20 14:59:51 +05:00
// 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) {
2018-03-23 08:14:25 +05:00
fl, present := f.flagsBool[name]
if !present {
return false, errors.New("No such flag: " + name)
}
return (*fl), nil
2017-08-20 14:59:51 +05:00
}
// GetIntValue returns integer value for flag with given name.
2017-08-20 14:59:51 +05:00
// Returns integer on success and 0 on error.
func (f *Flagger) GetIntValue(name string) (int, error) {
2018-03-23 08:14:25 +05:00
fl, present := f.flagsInt[name]
if !present {
return 0, errors.New("No such flag: " + name)
}
return (*fl), nil
2017-08-20 14:59:51 +05:00
}
// GetStringValue returns string value for flag with given name.
2017-08-20 14:59:51 +05:00
// Returns string on success or empty string on error.
func (f *Flagger) GetStringValue(name string) (string, error) {
2018-03-23 08:14:25 +05:00
fl, present := f.flagsString[name]
if !present {
return "", errors.New("No such flag: " + name)
}
return (*fl), nil
2017-08-20 14:59:51 +05:00
}
// Initialize initializes Flagger.
2017-08-20 14:59:51 +05:00
func (f *Flagger) Initialize() {
logger.Print("Initializing CLI parameters parser...")
2017-08-20 14:59:51 +05:00
2018-03-23 08:14:25 +05:00
f.flags = make(map[string]*Flag)
2017-08-20 14:59:51 +05:00
2018-03-23 08:14:25 +05:00
f.flagsBool = make(map[string]*bool)
f.flagsInt = make(map[string]*int)
f.flagsString = make(map[string]*string)
f.flagSet = flag.NewFlagSet(applicationName, flag.ContinueOnError)
2017-08-20 14:59:51 +05:00
}
// Parse adds flags from flags map to flag package and parse
2017-08-20 14:59:51 +05:00
// 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
}
2018-03-23 08:14:25 +05:00
for name, fl := range f.flags {
if fl.Type == "bool" {
fdef := fl.DefaultValue.(bool)
f.flagsBool[name] = &fdef
f.flagSet.BoolVar(&fdef, name, fdef, fl.Description)
2018-03-23 08:14:25 +05:00
} else if fl.Type == "int" {
fdef := fl.DefaultValue.(int)
f.flagsInt[name] = &fdef
f.flagSet.IntVar(&fdef, name, fdef, fl.Description)
2018-03-23 08:14:25 +05:00
} else if fl.Type == "string" {
fdef := fl.DefaultValue.(string)
f.flagsString[name] = &fdef
f.flagSet.StringVar(&fdef, name, fdef, fl.Description)
2018-03-23 08:14:25 +05:00
}
}
2017-08-20 14:59:51 +05:00
logger.Print("Parsing CLI parameters:", os.Args)
err := f.flagSet.Parse(os.Args[1:])
if err != nil {
os.Exit(0)
}
2017-08-20 14:59:51 +05:00
}