Added example program, New() now requires application name, fixed -h and -help.

This commit is contained in:
Stanislav Nikitin 2018-12-18 04:02:01 +05:00
parent d02921ea77
commit ba5b52918e
3 changed files with 26 additions and 5 deletions

19
example/main.go Normal file
View File

@ -0,0 +1,19 @@
package main
import (
"gitlab.com/pztrn/flagger"
)
var f *flagger.Flagger
func main() {
f = flagger.New("testprogram", nil)
f.Initialize()
f.AddFlag(&flagger.Flag{
Name: "testflag",
Description: "Just a test flag",
Type: "bool",
DefaultValue: false,
})
f.Parse()
}

View File

@ -30,13 +30,15 @@ import (
) )
var ( var (
logger LoggerInterface logger LoggerInterface
applicationName string
) )
// New creates new Flagger instance. // New creates new Flagger instance.
// If no logger will be passed - we will use default "log" module and will // If no logger will be passed - we will use default "log" module and will
// print logs to stdout. // print logs to stdout.
func New(l LoggerInterface) *Flagger { func New(appName string, l LoggerInterface) *Flagger {
applicationName = appName
if l == nil { if l == nil {
lg := log.New(os.Stdout, "Flagger: ", log.LstdFlags) lg := log.New(os.Stdout, "Flagger: ", log.LstdFlags)
logger = LoggerInterface(lg) logger = LoggerInterface(lg)

View File

@ -102,7 +102,7 @@ func (f *Flagger) Initialize() {
f.flagsInt = make(map[string]*int) f.flagsInt = make(map[string]*int)
f.flagsString = make(map[string]*string) f.flagsString = make(map[string]*string)
f.flagSet = flag.NewFlagSet("flagger", flag.ContinueOnError) f.flagSet = flag.NewFlagSet(applicationName, flag.ContinueOnError)
} }
// Parse adds flags from flags map to flag package and parse // Parse adds flags from flags map to flag package and parse
@ -130,6 +130,6 @@ func (f *Flagger) Parse() {
} }
} }
logger.Print("Parsing CLI parameters...") logger.Print("Parsing CLI parameters:", os.Args)
f.flagSet.Parse(os.Args) f.flagSet.Parse(os.Args[1:])
} }