flagger

package module
v0.0.0-...-afcd13f Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: MIT Imports: 4 Imported by: 0

README

Flagger

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

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFlagAlreadyAdded appears when trying to register a flag with
	// an already used name (field Name).
	ErrFlagAlreadyAdded = errors.New("flag already added")

	// ErrNoSuchFlag appears when trying to request a value for a flag
	// that wasn't registered.
	ErrNoSuchFlag = errors.New("no such flag")
)

Functions

This section is empty.

Types

type Flag

type Flag struct {
	// This value will be reflected.
	DefaultValue interface{}
	// 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
}

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