termi

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2019 License: MIT Imports: 12 Imported by: 0

README

Termi

Go Report Card Maintainability GoDoc

Termi is a library that enables applications flag parsing and help messages made simple. The benefit of builder like patterns, multiple names for the same flag, and be able to obtain the remaining arguments after parsing to use later makes for detailed application interfaces for command line applications.

If you regularly build applications that may work within a CI environment, can define settings variables to automatically load variables from the shell environment instead of passing them as flags. This way secrets can be stored within CI and exported at runtime given your CI of choice.

Documentation

Overview

Package termi is designed to make processing argument strings from the command line easier and enable nested commands within applications supporting their own different flags.

Index

Constants

View Source
const (
	EnvironmentTag     = "env"
	DescriptionTag     = "description"
	DefaultDescription = "expects %v as the value"
)

Variables

View Source
var (
	ErrInvalidType = errors.New("incorrect type being used, expected struct or pointer to struct")
)
View Source
var (
	// ErrorMissingBoolean allows for checking if we need to stumble when parsing flag arguments
	ErrorMissingBoolean = errors.New("non boolean value parsed")
)

Functions

func EnvironmentDescription

func EnvironmentDescription(obj interface{}) (map[string]string, error)

EnvironmentDescription fetches struct tags being used in order to print out to flag.Usage or other meaningful parts of the application

Types

type Boolean

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

Boolean defines an optional flag type which will default to true if the option is missing come parsing time.

func (*Boolean) IsFlag

func (b *Boolean) IsFlag(name string) bool

func (*Boolean) Set

func (b *Boolean) Set(value string) error

func (*Boolean) SetDescription

func (b *Boolean) SetDescription(description string) Flag

func (*Boolean) SetName

func (b *Boolean) SetName(name string) Flag

func (*Boolean) SetValue

func (b *Boolean) SetValue(value interface{}) Flag

func (*Boolean) String

func (b *Boolean) String() string

type Duration added in v1.0.0

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

func (*Duration) IsFlag added in v1.0.0

func (d *Duration) IsFlag(name string) bool

func (*Duration) Set added in v1.0.0

func (d *Duration) Set(value string) error

func (*Duration) SetDescription added in v1.0.0

func (d *Duration) SetDescription(description string) Flag

func (*Duration) SetName added in v1.0.0

func (d *Duration) SetName(name string) Flag

func (*Duration) SetValue added in v1.0.0

func (d *Duration) SetValue(value interface{}) Flag

func (*Duration) String added in v1.0.0

func (d *Duration) String() string

type Flag

type Flag interface {
	// SetDescription allows you to set the usage of the flag
	SetDescription(description string) Flag

	// SetValue allows you to pass the reference to the value you wish to
	// update with when parse is called.
	SetValue(value interface{}) Flag

	// SetName allows you to define what argument is associated with this flag.
	// Can be called multiple times
	SetName(name string) Flag

	// Set is called when parsing the argument within the FlagSet
	Set(value string) error

	// IsFlag allows the flag set to parse the command line arg
	IsFlag(name string) bool
}

Flag is a neat little abstraction that allows for simplified Command line arguments

func Must added in v0.3.0

func Must(f Flag, err error) Flag

Must allows for wrapping the generic flag parser to provide simple usage for the lazy developer if the err is not nil, it will panic.

func NewBoolean

func NewBoolean() Flag

func NewDuration added in v1.0.0

func NewDuration() Flag

func NewFlag added in v0.3.0

func NewFlag(value interface{}) (Flag, error)

NewFlag is a generic function that will return the correct concrete flag type so that the value will be correctly updated.

func NewInteger

func NewInteger() Flag

func NewString

func NewString() Flag

func NewUsignedInteger added in v0.3.0

func NewUsignedInteger() Flag

type FlagSet

type FlagSet interface {
	// SetDescription allows you to parse a text/template
	// in order to printed a more meaningful description
	// By default, the variables are:
	// - name         : name of the application
	// - GoVersion    : the version of go being used
	// - environments : a map of the variables that can be set by an env
	// - flags        : All the flags defined in the Set
	SetDescription(description string)

	// SetEnvironment allows you set parse a variable that is used
	// to gather environment values at runtime.
	SetEnvironment(variable interface{}) FlagSet

	// Register allows you to pass a flag object will be stored with this
	// Set
	Register(flag Flag) FlagSet

	// Parse will read all the strings passed and update flags when applied
	// then return the unused args.
	// When "--" is read in the args list, in remaining args will be returned unprocessed
	// after that point.
	Parse(args []string) ([]string, error)

	// ParseEnvironment will gather all environment variables and apply the values
	// to the stored environment values.
	ParseEnvironment() error

	// PrintDescription will print the stored description templated to the given writer.
	// If nill is passed, this function will exit early.
	PrintDescription(w io.Writer) error
}

FlagSet is my own version of the native 'flag.FlagSet' I felt as though it was missing features, so I am improving it as to how I like it.

func NewFlagSet

func NewFlagSet() FlagSet

type Integer

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

func (*Integer) IsFlag

func (i *Integer) IsFlag(name string) bool

func (*Integer) Set

func (i *Integer) Set(value string) error

func (*Integer) SetDescription

func (i *Integer) SetDescription(description string) Flag

func (*Integer) SetName

func (i *Integer) SetName(name string) Flag

func (*Integer) SetValue

func (i *Integer) SetValue(value interface{}) Flag

func (*Integer) String

func (i *Integer) String() string

type String

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

func (*String) IsFlag

func (s *String) IsFlag(name string) bool

func (*String) Set

func (s *String) Set(value string) error

func (*String) SetDescription

func (s *String) SetDescription(description string) Flag

func (*String) SetName

func (s *String) SetName(name string) Flag

func (*String) SetValue

func (s *String) SetValue(value interface{}) Flag

func (*String) String

func (s *String) String() string

type UnsignedInteger added in v0.3.0

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

func (*UnsignedInteger) IsFlag added in v0.3.0

func (u *UnsignedInteger) IsFlag(name string) bool

func (*UnsignedInteger) Set added in v0.3.0

func (u *UnsignedInteger) Set(value string) error

func (*UnsignedInteger) SetDescription added in v0.3.0

func (u *UnsignedInteger) SetDescription(description string) Flag

func (*UnsignedInteger) SetName added in v0.3.0

func (u *UnsignedInteger) SetName(name string) Flag

func (*UnsignedInteger) SetValue added in v0.3.0

func (u *UnsignedInteger) SetValue(value interface{}) Flag

func (*UnsignedInteger) String added in v0.3.0

func (u *UnsignedInteger) String() string

Jump to

Keyboard shortcuts

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