subcommand

package module
v0.0.0-...-b5e2f53 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 5 Imported by: 0

README

go-subcommand

Build Status

Option parser for go programs a la git/mercurial/go loosely inspired by Ruby's OptionParser.

It allows to build CLIs easily supporting syntax similar to:

program --prog_option1 opt1 --prog_switch1 subcommand --subcommand_opt1 opt1 --subcommand_switch more_arguments

Documentation

Overview

Package subcommand is an option parsing utility a la git/mercurial/go loosely inspired by Ruby's OptionParser

Index

Constants

View Source
const (
	PARSER_HELP_TEMPLATE = `` /* 361-byte string literal not displayed */

	COMMAND_HELP_TEMPLATE = `` /* 244-byte string literal not displayed */

)

Variables

This section is empty.

Functions

This section is empty.

Types

type Arity

type Arity struct {
	Count       int
	Description string
}

type Command

type Command struct {
	//Name
	Name      string
	ShortDesc string //Command help line
	LongDesc  string
	// contains filtered or unexported fields
}

Command aggregates different flags under a common name. Every time a command is found during the parsing process the associated function is executed.

func (*Command) AddOption

func (c *Command) AddOption(long, short, shortDesc, longDesc, values string, fn FlagFunction) *Flag

Adds a new option to the command to be used as "--option OPTION" (expects a value after the flag) in the command line The short definition has no length restriction but it should be significantly shorter that its long counterpart, it can be an empty string. The function fn receives the name of the option and its value Example: command.AddOption("path","p",setPath)//option [...]

func setPath(option,value string){
     printf("According the option %v the path is set to %v",option,value);
}

func (*Command) AddSwitch

func (c *Command) AddSwitch(long string, short string, shortDesc string, fn FlagFunction) *Flag

Adds a new switch to the command to be used as "--switch" (expects no value after the flag) in the command line The short definition has no length restriction but it should be significantly shorter that its long counterpart, it can be an empty string. The function fn receives two strings, the first is the switch name and the second is just an empty string Example: command.AddSwitch("verbose","v",setVerbose)//option [...]

func setVerbose(switch string){
     printf("I'm get to get quite talkative! I'm set to be %v ",switch);
}

func (Command) Arity

func (c Command) Arity() Arity

func (*Command) Flags

func (c *Command) Flags() []Flag

getFlags returns a slice containing the c's flags

func (*Command) MandatoryFlags

func (c *Command) MandatoryFlags() []Flag

func (*Command) NonMandatoryFlags

func (c *Command) NonMandatoryFlags() []Flag

func (Command) Parent

func (c Command) Parent() *Command

Returns the command parent

func (*Command) SetArity

func (c *Command) SetArity(arity int, description string) *Command

Set arity: -1 accepts infinite arguments. Other restricts the arity to the given num

type CommandFunction

type CommandFunction func(string, ...string) error

Convinience type for funcions passed to commands

type Flag

type Flag struct {
	//long definition (--option OPTION)
	Long string
	//Short definition (-o )
	Short string
	//Short description
	ShortDesc string
	//Long description
	LongDesc string
	//Possible values
	Values string
	//FlagType, option or switch
	Type FlagType

	//Says if the flag is optional or mandatory
	Mandatory bool
	// contains filtered or unexported fields
}

Flag structure

func (Flag) FlagStringPrefix

func (f Flag) FlagStringPrefix() string

func (*Flag) Must

func (f *Flag) Must(isIt bool)

Must sets the flag as mandatory. The parser will raise an error in case it isn't present in the arguments TODO make sure that switches are not allowed to get mandatory

func (Flag) String

func (f Flag) String() string

Gets a help friendly flag representation: -o,--option OPTION This option does this and that -s,--switch This is a switch -i,--ignoreme [IGNOREME] Optional option

type FlagFunction

type FlagFunction func(string, string) error

Convinience type for funcions passed flags

type FlagType

type FlagType int

FlagType defines the different flag types. Options have values associated to the flag, Switches have no value associated.

const (
	Option FlagType = iota
	Switch
)

type Flagged

type Flagged interface {
	Flags() []Flag
}

Access to flags

type Parser

type Parser struct {
	Command
	Commands map[string]*Command
	// contains filtered or unexported fields
}

Parser contains other commands. It's the data structure and its name should be the program's name.

func NewParser

func NewParser(program string) *Parser

NewParser constructs a parser for program name given

func (*Parser) AddCommand

func (p *Parser) AddCommand(name string, shortDesc string, longDesc string, fn CommandFunction) *Command

AddCommand inserts a new subcommand to the parser. The callback fn receives as first argument the command name followed by the left overs of the parsing process Example: command "hello" prints the non flags (options and switches) arguments. The associated callback should be something like

func processCommand(commandName string,args ...string){
     fmt.Printf("The command %v says:\n",commandName)
     for _,arg:= rage args{
             fmt.Printf("%v \n",arg)
     }
}

func (*Parser) OnCommand

func (p *Parser) OnCommand(fn CommandFunction)

First level execution when parsing. The passed function is exectued taking the leftovers until the first command ./prog -switch left1 left2 command in this case name will be prog, and left overs left1 and left2

func (*Parser) Parse

func (p *Parser) Parse(args []string) (leftOvers []string, err error)

Parse parses the arguments executing the associated functions for each command and flag. It returns the left overs if some non-option strings or commands were not processed. Errors are returned in case an unknown flag is found or a mandatory flag was not supplied. The set of function calls to be performed are carried in order and once the parsing process is done

func (*Parser) PostFlags

func (p *Parser) PostFlags(fn func() error)

Execute this function once the flags have been consumed. This can be used to dinamically add commands depending on the flags' state

func (*Parser) SetHelp

func (p *Parser) SetHelp(name string, description string, fn CommandFunction) *Command

Sets the help command. There is one default implementation automatically added when the parser is created.

type ParsingError

type ParsingError struct {
	Description string
	Command     Command
}

func (ParsingError) Error

func (e ParsingError) Error() string

Jump to

Keyboard shortcuts

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