flagger

package module
v0.0.0-...-10878cb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2019 License: MIT Imports: 5 Imported by: 0

README

GoDoc Drone (self-hosted) Discord Keybase XLM Go Report Card

Flagger

Flagger is an arbitrary CLI flags parser, like argparse in Python. Flagger is able to parse boolean, integer and string flags.

Installation

go get -u -v go.dev.pztrn.name/flagger

Usage

Flagger requires logging interface to be passed on initialization. See loggerinterface.go for required logging functions. It is able to run with standart log package, in that case initialize flagger like:

flgr = flagger.New("My Super Program", flagger.LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
flgr.Initialize()

Adding a flag is easy, just fill Flag structure and pass to AddFlag() call:

flag_bool := Flag{
    Name: "boolflag",
    Description: "Boolean flag",
    Type: "bool",
    DefaultValue: true,
}
err := flgr.AddFlag(&flag_bool)
if err != nil {
    ...
}

After adding all neccessary flags you should issue Parse() call to get them parsed:

flgr.Parse()

After parsed they can be obtained everywhere you want, like:

val, err := flgr.GetBoolValue("boolflag")
if err != nil {
    ...
}

For more examples take a look at flagger_test.go file or at GoDoc.

Get help

If you want to report a bug - feel free to report it via Gitlab's issues system. Note that everything that isn't a bug report or feature request will be closed without any futher comments.

If you want to request some help (without warranties), propose a feature request or discuss flagger in any way - please use our mailing lists at flagger@googlegroups.com. To be able to send messages there you should subscribe by sending email to flagger+subscribe@googlegroups.com, subject and mail body can be random.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Flag

type Flag struct {
	// Flag name. It will be accessible using this name later.
	Name string
	// Description for help output.
	Description string
	// Type can be one of "bool", "int", "string".
	Type string
	// This value will be reflected.
	DefaultValue interface{}
}

Flag represents addable flag for Flagger.

type Flagger

type Flagger struct {
	// contains filtered or unexported fields
}

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.

func New

func New(appName string, l LoggerInterface) *Flagger

New creates new Flagger instance. If no logger will be passed - we will use default "log" module and will print logs to stdout.

func (*Flagger) AddFlag

func (f *Flagger) AddFlag(flag *Flag) error

AddFlag adds flag to list of flags we will pass to “flag“ package.

func (*Flagger) GetBoolValue

func (f *Flagger) GetBoolValue(name string) (bool, error)

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 (*Flagger) GetIntValue

func (f *Flagger) GetIntValue(name string) (int, error)

GetIntValue returns integer value for flag with given name. Returns integer on success and 0 on error.

func (*Flagger) GetStringValue

func (f *Flagger) GetStringValue(name string) (string, error)

GetStringValue returns string value for flag with given name. Returns string on success or empty string on error.

func (*Flagger) Initialize

func (f *Flagger) Initialize()

Initialize initializes Flagger.

func (*Flagger) Parse

func (f *Flagger) 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.

type LoggerInterface

type LoggerInterface interface {
	Fatal(v ...interface{})
	Print(v ...interface{})
}

LoggerInterface provide logging interface, so everyone can inject own logging handlers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL