diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..dd00950 --- /dev/null +++ b/example/main.go @@ -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() +} diff --git a/exported.go b/exported.go index 13ed53b..b3db265 100644 --- a/exported.go +++ b/exported.go @@ -30,13 +30,15 @@ import ( ) var ( - logger LoggerInterface + logger LoggerInterface + applicationName string ) // New creates new Flagger instance. // If no logger will be passed - we will use default "log" module and will // print logs to stdout. -func New(l LoggerInterface) *Flagger { +func New(appName string, l LoggerInterface) *Flagger { + applicationName = appName if l == nil { lg := log.New(os.Stdout, "Flagger: ", log.LstdFlags) logger = LoggerInterface(lg) diff --git a/flagger.go b/flagger.go index ecc4227..97ff4d0 100644 --- a/flagger.go +++ b/flagger.go @@ -102,7 +102,7 @@ func (f *Flagger) Initialize() { f.flagsInt = make(map[string]*int) 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 @@ -130,6 +130,6 @@ func (f *Flagger) Parse() { } } - logger.Print("Parsing CLI parameters...") - f.flagSet.Parse(os.Args) + logger.Print("Parsing CLI parameters:", os.Args) + f.flagSet.Parse(os.Args[1:]) }