mojo

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2018 License: GPL-3.0 Imports: 2 Imported by: 2

README

Mojo

GoDoc GoReportCard TravisCI

Dynamic flag parser for Go.

Documentation

Overview

Package mojo implements dynamic flag parsing.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidFlag            = fmt.Errorf("mojo: invalid flag")
	ErrIncompleteMultipleFlag = fmt.Errorf("mojo: incomplete multiple flag")
	ErrFlagNotFound           = fmt.Errorf("mojo: flag not found")
	ErrArgumentNotFound       = fmt.Errorf("mojo: argument not found")

	// ErrUnconfiguredFlag occurs during parsing when a flag that does not
	// exist in the configuration is found.
	ErrUnconfiguredFlag = fmt.Errorf("mojo: unconfigured flag")

	// ErrUnexpectedArrayFlag occurs when more than one flag with the same name
	// is found when only one is requested.
	ErrUnexpectedArrayFlag = fmt.Errorf("mojo: unexpected array flag")
)

Possible wrapped errors.

Functions

This section is empty.

Types

type ArgumentError

type ArgumentError struct {
	Index int
	Err   error
}

ArgumentError represents an argument error.

func (ArgumentError) Error

func (err ArgumentError) Error() string

type ArgumentObject

type ArgumentObject struct {
	Value string
}

ArgumentObject represents an argument that has been parsed.

type CommandConfig

type CommandConfig struct {
	Name     string
	Commands []CommandConfig
	Flags    []FlagConfig
}

CommandConfig contains configuration for a command.

func (CommandConfig) Command

func (c CommandConfig) Command(name string) (CommandConfig, bool)

Command returns the command configuration for the command of the given name.

func (CommandConfig) Flag

func (c CommandConfig) Flag(name string) (FlagConfig, bool)

Flag returns the flag configuration for the command of the given name.

type CommandObject

type CommandObject struct {
	Name string
}

CommandObject represents a command that has been parsed.

type Config

type Config struct {
	Root CommandConfig

	// DisallowUnconfiguredFlags indicates whether unconfigured flags are
	// not allowed.
	//
	// If it isn't allowed, then unconfigured flags will result in an
	// invalid flag error.
	DisallowUnconfiguredFlags bool

	// AllowMultipleFlags indicates whether combining multiple flags
	// (e.g. ls -al) is allowed.
	//
	// If it is allowed, such flags will be parsed as multiple flags with
	// the last one containing a value unless indicated otherwise by
	// configuration. If it isn't allowed, they will be parsed as a single
	// flag instead.
	AllowMutipleFlags bool

	// DisallowCombinedFlagValues indicates whether combining the flag and
	// value together (e.g. --flag=value) is not allowed.
	//
	// If it isn't allowed, then these flags will be taken literally, with
	// their name set to whatever the combined flag and value is.
	DisallowCombinedFlagValues bool

	// DisallowDoubleDash indicates whether the double dash (i.e. --) is
	// not allowed.
	//
	// If it is allowed, it will be parsed as an argument. However, if it
	// isn't allowed, then an error will occur since it will be treated
	// as a flag without a name.
	DisallowDoubleDash bool
}

Config contains configuration that defines how to parse certain objects.

type FlagConfig

type FlagConfig struct {
	Name string
	Bool bool
}

FlagConfig contains configuration for a flag.

type FlagError

type FlagError struct {
	Name string
	Err  error
}

FlagError represents a flag error.

func (FlagError) Error

func (err FlagError) Error() string

type FlagObject

type FlagObject struct {
	Name  string
	Value string

	// Bool indicates whether this flag was a bool flag.
	//
	// This means that the flag was passed without a value.
	Bool bool

	// MultipleFlagsStart indicates whether this flag was the start of
	// multiple flags (e.g. ls -al).
	//
	// Check AllowMultipleFlags in Config for more information.
	MultipleFlagsStart bool

	// MultipleFlagsEnd indicates whether this flag was the end of multiple
	// flags (e.g. ls -al).
	//
	// Check AllowMultipleFlags in Config for more information.
	MultipleFlagsEnd bool

	// CombinedFlagValues indicates whether this flag was combined
	// (i.e. --flag=value).
	//
	// Check DisallowCombinedFlagValues in Config for more information.
	CombinedFlagValues bool
}

FlagObject represents a flag that has been parsed.

type Object

type Object interface {
	// contains filtered or unexported methods
}

Object represents a command, flag or argument.

type Objects

type Objects []Object

Objects is a list of objects which represents some parsed arguments.

func Parse

func Parse(conf Config, args []string) (Objects, error)

Parse parses the given arguments into objects using the given configuration.

The first argument given should be the name of the root command (e.g. git).

func (Objects) Argument

func (objs Objects) Argument(i int) (ArgumentObject, error)

Argument returns the argument at the given index.

func (Objects) ArrayFlag

func (objs Objects) ArrayFlag(name string) []FlagObject

ArrayFlag returns the flags with the given name in order.

func (Objects) Assemble

func (objs Objects) Assemble() ([]string, error)

Assemble assembles the given objects back into arguments.

func (Objects) Flag

func (objs Objects) Flag(name string) (FlagObject, error)

Flag returns the first flag with the given name.

An error will be returned if there are no flags found or if there is more than one flag found.

Jump to

Keyboard shortcuts

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