flagger

package module
v0.0.0-...-0631b9e Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2019 License: MIT Imports: 4 Imported by: 2

README

flagger

POSIX-like CLI Flag interpreter

Example

See "test" folder for working examples

flagger allows much more freedom to the user when passing in flags. It also allows flags to have multiple variations, such as a short and long form. The following application has the available flags:

  -b, --bool, --boolean         Bool Flag
  -n, --newBool                 Another Bool Flag
  -i, --integer                 Integer Flag
  -s, --string                  String Flag

Flags can be used in short or long form. Assignments for values works with either a space or an "="

$ ./goapp -b --integer 4 --string="hello"

Short Flags (single "-" and 1 letter) can be grouped together, any flags with assignments must come last in a group.

$ ./goapp -bi 4 -ns="hello"

Usage

flagger follows the same methodology that the flags implementation in the Standard Library has. To get started, you have to first create a "Flags" object. It is best to use the function "New" to create these objects as this will also initialize the variables inside the object

flags := flagger.New()

Now you are able to add new flags to it in multiple ways.

// Creating a Flag will also return a pointer value
boolFlag := flags.Bool("Bool Flag", "-b", "--bool")
//It can be accessed by using *variable
fmt.Println(*boolFlag)

//You can also use the "Var" functions to manually assign a pointer
intFlag := 5
flags.IntVar(&intFlag, "Int Flag", "-i", "--integer")

Once all of your flags are in place, you can call the Parse() method to process the available flags. Parse accepts a slice of strings that are the flags, and returns a slice of strings of any data not associated with a flag and an error if applicable.

data, err := flags.Parse(os.Args[1:])

Commands

flagger also has a sub-package named "Commands" that allows for variations of flags based on a root command given. For instance:

$ ./goapp new -bn
  #Output of command "new"

$ ./goapp run -bni 9
  #Output of command "run"

Each command has its own set of flags. To use command in an application, it is recommended that you create an object with the "New()" function

cmd := commands.New()

To create a valid command, you must create a data type that satisfies the "Commander" interface. The most basic this could be is:

type command struct {}
func (c *command) Prepare(flags *flagger.Flags) {}
func (c *command) Action(s []string, flags *flagger.Flags) error { return nil }

Once you have your data, you can use the function "Add" to place them into the Commands object

c := command{}
cmd.Add("command", &c)

After all of the commands are in place, run the Parse Method to parse the flags and run the command specified.

err := cmd.Parse(os.Args[1:])

Documentation

Index

Constants

View Source
const (
	NAME     = "flagger"
	MAJORVER = "0"
	MINORVER = "9"
	VERTAG   = "-beta"
	DESC     = "Flag handler"
)

Variables

View Source
var (
	NoFlags = errors.New("missing operand")
)

Functions

func Info

func Info() string

Info returns a formatted string of Version and the Description

func Version

func Version() string

Version returns a formatted string of the name/version number

Types

type Flag

type Flag struct {
	Usage string

	Value FlagValue
	// contains filtered or unexported fields
}

func (Flag) Print

func (f Flag) Print() string

type FlagValue

type FlagValue interface {
	String() string
	Set(string) error
}

type Flags

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

func New

func New() *Flags

func (*Flags) Bool

func (f *Flags) Bool(usage string, flgs ...string) *bool

func (*Flags) BoolVar

func (f *Flags) BoolVar(b *bool, usage string, flgs ...string)

func (*Flags) Int

func (f *Flags) Int(def int, usage string, flgs ...string) *int

func (*Flags) IntVar

func (f *Flags) IntVar(i *int, def int, usage string, flgs ...string)

func (Flags) Parse

func (f Flags) Parse(flags []string) ([]string, error)

func (Flags) Print

func (f Flags) Print(msg string)

func (*Flags) String

func (f *Flags) String(def string, usage string, flgs ...string) *string

func (*Flags) StringVar

func (f *Flags) StringVar(s *string, def string, usage string, flgs ...string)

func (*Flags) Uint

func (f *Flags) Uint(def uint, usage string, flgs ...string) *uint

func (*Flags) UintVar

func (f *Flags) UintVar(i *uint, def uint, usage string, flgs ...string)

type Getter

type Getter interface {
	FlagValue
	Get() interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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