flagger

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2020 License: MIT Imports: 4 Imported by: 3

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:])
Help flag

A default "help" flag is included that will automatically print all of the available flags. For personalization, this must be initialized with your choice of message.

flags.AddHelp("Help flag text", "Message:")

This will print out the following when the help flag is passed:

Message:
  -h, --help                    Help flag text
  -b, --bool                    Test Bool
  -i, --integer                 Test Int
  -s, --string                  Test String

When the help flag is passed, an error named flags.ErrHelp is returned.

Version flag

A default "version" flag is included that will automatically print a message you pass. For personalization, this must be initialized with your choice of message.

flags.AddVersion("Text v1.0")

This will add a "-v" and "--version" flag to your application, which prints the message and then returns flags.ErrVersion.

Best to use it with your Version/Description function if applicable, instead of a static string.

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. The command package works as a singleton, and functions are run against the package. To create an instance:

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

commands.Add("command", &command{})

After all of the commands are in place, run the Parse Method to parse the flags and run the command specified. Note, the full Args array is required for commands.

err := commands.Parse(os.Args)
Help Command

As with the base package, a Help command is available to use. Using the "Help" command will print all commands and all flags for each command. The help must be defined as such:

commands.AddHelp("Message:")

Print out:

Message:
 help

 version

 cmda
  -h, --help                    CommandA Help
  -b, --bool                    Test Bool
  -i, --integer                 Test Int
  -s, --string                  Test cmdA String

 cmdb
  -h, --help                    CommandB Help
  -b, --bool                    Test Bool
  -i, --integer                 Test Int
  -s, --string                  Test cmdB String

On run, returns commands.ErrHelp

Help flag with your commands

Your commands can make usage of the help flag set in flagger as well. In the Prepare function of your command, you can use the following:

func (c *cmdA) Prepare(flags *flagger.Flags) {
  flags.AddHelp("CommandA Help", "Show CommandA flags")
  ... // Other flags
}

Print out:

$ app cmda -h
Show CommandA flags
  -h, --help                    CommandA Help
  ... # Other flags
Version Command

A version command is included as well and must be defined with a message:

commands.AddVersion("Version 1.0")

On run, returns commands.ErrVersion

Documentation

Index

Constants

View Source
const (
	// NAME of flagger
	NAME = "flagger"
	// MAJORVERSION of flagger
	MAJORVERSION = "1"
	// MINORVERSION of flagger
	MINORVERSION = "1"
	// VERSIONTAG of flagger
	VERSIONTAG = "2"
	// DESCRIPTION of flagger
	DESCRIPTION = "Flag handler written in Go"
)

Variables

View Source
var (
	// ErrNoFlags is the error returned when no flags are passed.
	// Useful as a check for when you want to proceed without flags.
	ErrNoFlags  = errors.New("No flags passed")
	ErrBadFlag  = errors.New("invalid flag")
	ErrBadValue = errors.New("invalid value")
	ErrNoValue  = errors.New("missing operand")
	ErrHelp     = errors.New("Help")
	ErrVersion  = errors.New("Version")
)

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 {
	Name string
	// contains filtered or unexported fields
}

Flags is the struct used by you, the user

func New

func New() *Flags

func (*Flags) AddHelp added in v1.1.0

func (f *Flags) AddHelp(txt string, msg string) error

func (*Flags) AddVersion added in v1.1.0

func (f *Flags) AddVersion(txt string, msg string) error

func (*Flags) Bool

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

Bool creates a bool flag, returns the bool usage is the string for the help flgs

func (*Flags) BoolVar

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

func (Flags) Help added in v1.1.0

func (f Flags) Help(msg 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) 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)

func (Flags) Usage added in v1.1.1

func (f Flags) Usage(usage string, msg 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