Documentation
¶
Overview ¶
Package broccli is meant to make handling command line interface easier. Define commands with arguments, flags, attach a handler to it and package will do all the parsing.
Index ¶
Constants ¶
const ( // ParamFlag sets param to be a command flag. ParamFlag // ParamFlag sets param to be a command arg. ParamArg // ParamFlag sets param to be an environment variable. ParamEnvVar )
Command param type.
const ( // TypeString requires param to be a string. TypeString = iota * 1 // TypeBool requires param to be a boolean. TypeBool // TypeInt requires param to be an integer. TypeInt // TypeFloat requires param to be a float. TypeFloat // TypeAlphanumeric requires param to contain numbers and latin letters only. TypeAlphanumeric // TypePathFile requires param to be a path to a file. TypePathFile )
Value types.
const ( // IsRequired means that the value is required. IsRequired // IsExistent is used with TypePathFile and requires file to exist. IsExistent // IsNotExistent is used with TypePathFile and requires file not to exist. IsNotExistent // IsDirectory is used with TypePathFile and requires file to be a directory. IsDirectory // IsRegularFile is used with TypePathFile and requires file to be a regular file. IsRegularFile // IsValidJSON is used with TypeString or TypePathFile with RegularFile to check if the contents are a valid JSON. IsValidJSON // AllowDots can be used only with TypeAlphanumeric and additionally allows flag to have dots. AllowDots // AllowUnderscore can be used only with TypeAlphanumeric and additionally allows flag to have underscore chars. AllowUnderscore // AllowHyphen can be used only with TypeAlphanumeric and additionally allows flag to have hyphen chars. AllowHyphen // AllowMultipleValues allows param to have more than one value separated by comma by default. // For example: AllowMany with TypeInt allows values like: 123 or 123,455,666 or 12,222 // AllowMany works only with TypeInt, TypeFloat and TypeAlphanumeric. AllowMultipleValues // SeparatorColon works with AllowMultipleValues and sets colon to be the value separator, instead of colon. SeparatorColon // SeparatorSemiColon works with AllowMultipleValues and sets semi-colon to be the value separator. SeparatorSemiColon )
Validation.
const VERSION = "3.1.0"
VERSION is the current version of the module.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broccli ¶
type Broccli struct {
// contains filtered or unexported fields
}
Broccli is main CLI application definition. It has a name, description, author which are printed out to the screen in the usage syntax. Each CLI have commands (represented by Command). Optionally, it is possible to require environment variables.
func NewBroccli ¶
NewBroccli returns pointer to a new Broccli instance. Name, usage and author are displayed on the syntax screen.
func (*Broccli) Command ¶
func (c *Broccli) Command( name, usage string, handler func(ctx context.Context, cli *Broccli) int, opts ...CommandOption, ) *Command
Command returns pointer to a new command with specified name, usage and handler. Handler is a function that gets called when command is executed. Additionally, there is a set of options that can be passed as arguments. Search for commandOption for more info.
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command represent a command which has a name (used in args when calling app), usage, a handler that is called. Such command can have flags and arguments. In addition to that, required environment variables can be set.
func (*Command) Arg ¶
func (c *Command) Arg( name, valuePlaceholder, usage string, types, flags int64, opts ...ParamOption, )
Arg adds an argument to a command and returns a pointer to Param instance. It is the same as adding flag except it does not have an alias.
func (*Command) Env ¶
func (c *Command) Env(name, usage string, types, flags int64, _ ...ParamOption)
Env adds a required environment variable to a command and returns a pointer to Param. It's arguments are very similar to ones in previous AddArg and AddFlag methods.
func (*Command) Flag ¶
func (c *Command) Flag( name, alias, valuePlaceholder, usage string, types, flags int64, opts ...ParamOption, )
Flag adds a flag to a command and returns a pointer to Param instance. Method requires name (eg. 'data' for '--data', alias (eg. 'd' for '-d'), placeholder for the value displayed on the 'help' screen, usage, type of the value and additional validation that is set up with bit flags, eg. IsRequired or AllowMultipleValues. If no additional flags are required, 0 should be used.
type CommandOption ¶ added in v3.1.0
type CommandOption func(opts *commandOptions)
CommandOption defines an optional configuration function for commands, intended for specific use cases. It should not be created manually; use one of the predefined functions below.
func OnPostValidation ¶
func OnPostValidation(fn func(c *Command) error) CommandOption
OnPostValidation attaches a function that is called once args, flags and env vars are validated.
type ParamOption ¶ added in v3.1.0
type ParamOption func(opts *paramOptions)
ParamOption defines an optional configuration function for args and flags, intended for specific use cases. It should not be created manually; use one of the predefined functions below.
func OnTrue ¶
func OnTrue(fn func(command *Command)) ParamOption
OnTrue executes a specified function when boolean flag is true.