Documentation ¶
Index ¶
- Constants
- Variables
- func Add(args ...Arg) error
- func Err() error
- func GetLogger() *zap.Logger
- func NameOf(id Identifier) string
- func Ok()
- func Parse(args ...string) error
- func RawFlags() map[string][]string
- func RawKeyValues() map[string][]string
- func RawPositions() []string
- func ReferenceTo[T any](arg Arg) (*T, error)
- func RegisterFlagDeriver(name string, d FlagDeriver)
- func RegisterKeyValueDeriver(name string, d KeyValueDeriver)
- func RegisterPositionalDeriver(name string, d PositionalDeriver)
- func SetDescription(description string)
- func SetLogger(l *zap.Logger)
- func SetName(name string)
- func UpdateMetadata(u Updater, options ...Option)
- func UpdateValue(u Updater, input ...string) error
- func Using(elements ...any) error
- func Valid() error
- func ValidateDefaultValue[T any](arg TypedArg[T]) (T, error)
- func ValueOf[T any](arg Arg) (T, error)
- type Arg
- type Error
- type ErrorHandling
- type FileInfo
- type FileReader
- type FileWriter
- type Flag
- type FlagArg
- func FlagUsingVariable[T any](name string, v Variable[T], opts ...Option) *FlagArg[T]
- func FlagsUsingVariable[T any](name string, v Variable[[]T], opts ...Option) *FlagArg[[]T]
- func Help(helpRequested *bool, desc string) *FlagArg[bool]
- func NewFlag[T any](val *T, name string, parser parse.Parser[T], opts ...Option) *FlagArg[T]
- func NewFlagP[T any](val *T, name string, shorthand string, parser parse.Parser[T], opts ...Option) *FlagArg[T]
- func NewFlags[T any](val *[]T, name string, parser parse.Parser[T], options ...Option) *FlagArg[[]T]
- func NewFlagsP[T any](val *[]T, name string, shorthand string, parser parse.Parser[T], ...) *FlagArg[[]T]
- func (f *FlagArg[T]) Default() string
- func (f *FlagArg[T]) HasDefault() bool
- func (f *FlagArg[T]) IsIndicator() bool
- func (f *FlagArg[T]) IsParsed() bool
- func (f *FlagArg[T]) IsRepeatable() bool
- func (f *FlagArg[T]) IsRequired() bool
- func (f *FlagArg[T]) IsSupplied() bool
- func (f *FlagArg[T]) Name() string
- func (f *FlagArg[T]) Shorthand() string
- func (f *FlagArg[T]) Type() Type
- func (f *FlagArg[T]) Usage() string
- func (f *FlagArg[T]) ValueType() string
- func (f *FlagArg[T]) Variable() Variable[T]
- type FlagDeriver
- type IFlag
- type IKeyValue
- type IPipe
- type IPositional
- type Identifier
- type Key
- type KeyValueArg
- func KeyValueUsingVariable[T any](name string, v Variable[T], opts ...Option) *KeyValueArg[T]
- func KeyValuesUsingVariable[T any](name string, v Variable[[]T], opts ...Option) *KeyValueArg[[]T]
- func NewKeyValue[T any](variable *T, name string, p parse.Parser[T], opts ...Option) *KeyValueArg[T]
- func NewKeyValues[T any](variables *[]T, name string, p parse.Parser[T], opts ...Option) *KeyValueArg[[]T]
- func (k *KeyValueArg[T]) Default() string
- func (k *KeyValueArg[T]) HasDefault() bool
- func (k *KeyValueArg[T]) IsParsed() bool
- func (k *KeyValueArg[T]) IsRepeatable() bool
- func (k *KeyValueArg[T]) IsRequired() bool
- func (k *KeyValueArg[T]) IsSupplied() bool
- func (k *KeyValueArg[T]) Name() string
- func (k *KeyValueArg[T]) Shorthand() string
- func (k *KeyValueArg[T]) Type() Type
- func (k *KeyValueArg[T]) Usage() string
- func (k *KeyValueArg[T]) ValueType() string
- func (k *KeyValueArg[T]) Variable() Variable[T]
- type KeyValueDeriver
- type Metadata
- type Option
- type ParseError
- type Parser
- func (p *Parser) Add(args ...Arg) *Parser
- func (p *Parser) AddFlag(f IFlag, opts ...Option) *Parser
- func (p *Parser) AddKeyValue(kv IKeyValue, opts ...Option) *Parser
- func (p *Parser) AddPipe(pipe IPipe, options ...Option) *Parser
- func (p *Parser) AddPosition(a IPositional, opts ...Option) *Parser
- func (p *Parser) Complete(cmd *complete.Command)
- func (p *Parser) Err() error
- func (p *Parser) Ok() *Parser
- func (p *Parser) Parse(argv ...string)
- func (p *Parser) RawFlags() map[string][]string
- func (p *Parser) RawKeyValues() map[string][]string
- func (p *Parser) RawPositions() []string
- func (p *Parser) State(id Identifier) error
- func (p *Parser) Usage()
- func (p *Parser) Valid() error
- func (p *Parser) WithDescription(desc string) *Parser
- type Pipe
- type PipeArg
- func CSVPipe[T any](variable *T, parser parse.Parser[T], options ...Option) *PipeArg[T]
- func NewLinePipe[T any](variable *T, parser parse.Parser[T], options ...Option) *PipeArg[T]
- func NewPipeArg[T any](variable *T, parser parse.Parser[T], piper Piper, input FileReader, ...) *PipeArg[T]
- func PipeUsingVariable[T any](piper Piper, input FileReader, v Variable[T], options ...Option) *PipeArg[T]
- func (p *PipeArg[T]) Default() string
- func (p *PipeArg[T]) IsParsed() bool
- func (p *PipeArg[T]) IsRepeatable() bool
- func (p *PipeArg[T]) IsRequired() bool
- func (p *PipeArg[T]) IsSupplied() (cond bool)
- func (p *PipeArg[T]) Name() string
- func (p *PipeArg[T]) PipeDecode(in io.Reader) ([]string, error)
- func (p *PipeArg[T]) PipeInput() FileReader
- func (p *PipeArg[T]) Shorthand() string
- func (p *PipeArg[T]) Type() Type
- func (p *PipeArg[T]) Usage() string
- func (p *PipeArg[T]) Variable() Variable[T]
- type Piper
- type Position
- type PositionalArg
- func NewPosition[T any](variable *T, index int, parser parse.Parser[T], opts ...Option) *PositionalArg[T]
- func NewPositions[T any](variables *[]T, fromIndex int, parser parse.Parser[T], opts ...Option) *PositionalArg[[]T]
- func PositionUsingVariable[T any](index int, v Variable[T], opts ...Option) *PositionalArg[T]
- func PositionsUsingVariable[T any](fromIndex int, v Variable[[]T], opts ...Option) *PositionalArg[[]T]
- func (p *PositionalArg[T]) Default() string
- func (p *PositionalArg[T]) Index() int
- func (p *PositionalArg[T]) IsParsed() bool
- func (p *PositionalArg[T]) IsRepeatable() bool
- func (p *PositionalArg[T]) IsRequired() bool
- func (p *PositionalArg[T]) IsSupplied() bool
- func (p *PositionalArg[T]) Name() string
- func (p *PositionalArg[T]) Shorthand() string
- func (p *PositionalArg[T]) Type() Type
- func (p *PositionalArg[T]) Usage() string
- func (p *PositionalArg[T]) ValueType() string
- func (p *PositionalArg[T]) Variable() Variable[T]
- type PositionalDeriver
- type ScanError
- type SeparatedValuePiper
- type Set
- func (s *Set) AddFlag(f IFlag, opts ...Option) error
- func (s *Set) AddKeyValue(kv IKeyValue, opts ...Option) error
- func (s *Set) AddPipe(p IPipe, opts ...Option) error
- func (s *Set) AddPosition(a IPositional, opts ...Option) error
- func (s *Set) Args() []Arg
- func (s *Set) ByShorthand(sh string) Arg
- func (s *Set) Flag(name string) IFlag
- func (s *Set) Flags() []IFlag
- func (s *Set) Get(id Identifier) Arg
- func (s *Set) Has(id Identifier) bool
- func (s *Set) Key(name string) IKeyValue
- func (s *Set) KeyValues() []IKeyValue
- func (s *Set) Pipe() IPipe
- func (s *Set) Pos(index int) IPositional
- func (s *Set) PosS(sindex string) IPositional
- func (s *Set) Positions() []IPositional
- type Type
- type TypedArg
- type Updater
- type Variable
Constants ¶
const ( ContinueOnError = ErrorHandling(flag.ContinueOnError) ExitOnError = ErrorHandling(flag.ExitOnError) PanicOnError = ErrorHandling(flag.PanicOnError) )
const PipeName = "MAIN"
Variables ¶
var (
System = Must("default")
)
Functions ¶
func NameOf ¶
func NameOf(id Identifier) string
NameOf is a helper function for getting the name of an Arg from an Identifier.
func RawKeyValues ¶
func RawPositions ¶
func RawPositions() []string
func ReferenceTo ¶
ReferenceTo is a helper function for retrieving a reference to the value for an Arg.
func RegisterFlagDeriver ¶
func RegisterFlagDeriver(name string, d FlagDeriver)
RegisterFlagDeriver adds a FlagDeriver to the program so that it can be detected and used within struct-tags.
func RegisterKeyValueDeriver ¶
func RegisterKeyValueDeriver(name string, d KeyValueDeriver)
RegisterKeyValueDeriver adds a KeyValueDeriver to the program so that it can be detected and used within struct-tags.
func RegisterPositionalDeriver ¶
func RegisterPositionalDeriver(name string, d PositionalDeriver)
RegisterPositionalDeriver adds a PositionalDeriver to the program so that it can be detected and used within struct-tags.
func SetDescription ¶
func SetDescription(description string)
func UpdateMetadata ¶
UpdateMetadata is the public helper function to update the Metadata of some Arg.
func UpdateValue ¶
UpdateValue is a public helper function to update the value of some Arg.
func ValidateDefaultValue ¶
ValidateDefaultValue is a helper function for retrieving and attempting to parse the actual typed default value of an Arg.
Types ¶
type Arg ¶
type Arg interface { Identifier Updater // Name returns the key/identifier of the Arg, this should be unique for each Type. Name() string // Type indicates the type of command-line argument the Arg is, returns one of: // // > FlagType; // // > KeyValueType; // // > PositionType; // // > PipeType; Type() Type // Shorthand is the single character alias/identifier for the Arg, if applicable // // Can be updated via the WithAlias Option Shorthand() string // Usage returns a usage of the Arg for the user // // Can be updated via the WithUsage Option Usage() string // Default returns the default string value for the Arg // // Can be updated via the WithDefault Option, if applicable Default() string // IsRequired returns true if the Arg is required // // Can be updated via the AsRequired, AsOptional Option(s) IsRequired() bool // IsParsed returns true if the Arg has been parsed IsParsed() bool // IsSupplied returns true if the Arg was supplied by the user IsSupplied() bool // IsRepeatable returns true if the Arg can be supplied multiple times IsRepeatable() bool }
Arg is the shared behaviour of all command-line input types (FlagType, KeyValueType and PositionType). It essentially exposes an API similar to the behaviour seen in the standard 'flags' package, extending support to key-value and positional arguments.
Metadata is attached to each Arg giving each arg some mutable properties which can be managed via Option(s).
Each Arg has its own go-argumentVariable which it is responsible for updating:
import ( . "github.com/runaek/clap" "github.com/runaek/clap/pkg/parse" ) var ( strVal string numVal int myArg = NewKeyValue[string](&strVal, "arg1", parse.String, <options>...) // arg1=Test123 => strVal=Test123 numFlag = NewFlag[int](&numVal, "amount", parse.Int, <options>...) // --amount=123 => numVal=123 parser = clap.New("program-Id"). Add(myArg, numFlag). OK() ) ... func main() { parser.Parse() parser.OK() ... // do stuff with 'strVal' and 'numVal' }
func Derive ¶
Derive attempts to construct a number of Arg dynamically from some struct-tags.
The tags are applied on the fields to be bound to the Arg (i.e. at runtime, the Field will hold the value of the Arg). There are 4 formats for each of the types and all can be prefixed with a '!' which will mark the Arg as required:
1. KeyValueArg: `cli:"@<name>|<alias>:<deriver>"
2. FlagArg: `cli:"-<name>|<alias>:<deriver>"
3. PositionalArg: `cli:"#<index>:<deriver>"
4. PositionalArg(s): `cli:"#<index>...:<deriver>"
Where the 'deriver' is the name assigned to the deriver when it was registered (using one of RegisterKeyValueDeriver, RegisterFlagDeriver or RegisterPositionalDeriver).
Usage strings can be supplied by creating a string field in the struct called <Name>Usage, the same can be done for a default value (i.e. <Name>Default). At runtime, the values held by the variable will be used for the respective usage/default.
Package `github.com/runaek/clap/pkg/derivers` provides support to a number of the built-in Go types. The program `github.com/runaek/clap/cmd/generate_derivers` is a codegen tool for helping generate FlagDeriver, KeyValueDeriver and PositionalDeriver implementations using a specified parse.Parse[T].
type Args struct { // a "name" or "N" KeyValueArg Name string `cli:"!@name|N:string"` // usage/description for the KeyValueArg NameUsage string // default value for the KeyValueArg NameDefault string // a variable number of string args, starting from the first index Values []string `cli:"#1...:string"` // usage/description for the positional args ValuesUsage string } ... var ( args = Args{ NameUsage: "Supply your name.", NameDefault: "John", ValuesUsage: "A number of values to be supplied." } parser = clap.Must("demo", &args) ) func main() { parser.Parse() parser.Ok() // use args }
NOTE: Fields should be defined in the same order you would expect to add them to a Parser manually (i.e. Field referring to the 1st positional argument must come before the Field referring to the 2nd positional argument).
func Generalize ¶
Generalize is a helper function for converting some TypedArg[T] -> Arg.
type Error ¶
type Error string
Error is a simple indicator for some error that occurs.
The value of the error should indicate the 'cause' of the problem and context should be provided by the returning process.
const ( ErrUnidentified Error = "unidentified argument" ErrMissing Error = "argument is missing" ErrDuplicate Error = "identifier already exists" ErrInvalidIndex Error = "invalid positional index" ErrPipeExists Error = "pipe already exists" ErrUnknownType Error = "unrecognised argument type" ErrInvalid Error = "invalid argument syntax" ErrHelp Error = "help requested" )
const (
ErrDeriverNotFound Error = "deriver not found"
)
const (
ErrInvalidType Error = "invalid type"
)
type ErrorHandling ¶
type ErrorHandling int
type FileReader ¶
FileReader represents some time of file-like object that would be used as an input we can read from.
Usually, in this package, values of this type will be set to os.Stdin by default
type FileWriter ¶
FileWriter represents some type of file-like object that would be used as an output we can write to.
Usually, in this package, values of this type will be set to os.Stdout by default.
type FlagArg ¶
A FlagArg argument of some type T.
func FlagUsingVariable ¶
FlagUsingVariable is a constructor for a FlagArg using a Id and some Variable.
func FlagsUsingVariable ¶
FlagsUsingVariable is a constructor for a repeatable FlagArg using a Id and some Variable.
func NewFlagP ¶
func NewFlagP[T any](val *T, name string, shorthand string, parser parse.Parser[T], opts ...Option) *FlagArg[T]
NewFlagP is a constructor for some new *FlagArg[T] with a shorthand.
func NewFlags ¶
func NewFlags[T any](val *[]T, name string, parser parse.Parser[T], options ...Option) *FlagArg[[]T]
NewFlags is a constructor for a repeatable *FlagArg[[]T].
func NewFlagsP ¶
func NewFlagsP[T any](val *[]T, name string, shorthand string, parser parse.Parser[T], options ...Option) *FlagArg[[]T]
NewFlagsP is a constructor for a repeatable *FlagArg[[]T] with a shorthand.
func (*FlagArg[T]) HasDefault ¶
func (*FlagArg[T]) IsIndicator ¶
func (*FlagArg[T]) IsRepeatable ¶
func (*FlagArg[T]) IsRequired ¶
func (*FlagArg[T]) IsSupplied ¶
type FlagDeriver ¶
A FlagDeriver is responsible for constructing a FlagArg dynamically.
type IFlag ¶
type IFlag interface { Arg // IsIndicator returns true if the FlagArg does not require a value (e.g. a bool or C) IsIndicator() bool // HasDefault returns true if the flag has a defined default string value HasDefault() bool // contains filtered or unexported methods }
IFlag is the interface satisfied by a FlagArg.
type IKeyValue ¶
type IKeyValue interface { Arg // HasDefault returns true if the Arg has a defined default string value HasDefault() bool // contains filtered or unexported methods }
IKeyValue is the interface satisfied by a KeyValueArg.
type IPipe ¶
type IPipe interface { Arg // PipeInput returns the FileReader wrapping the underlying input for the pipe - this is usually os.input PipeInput() FileReader // PipeDecode decodes the contents of the pipe into string arguments to be parsed later PipeDecode(io.Reader) ([]string, error) // contains filtered or unexported methods }
IPipe is the interface satisfied by a PipeArg.
type IPositional ¶
type IPositional interface { Arg // Index returns the index (position) of the PositionalArg (or the starting index of the remaining args if // ArgRemaining is true) Index() int // contains filtered or unexported methods }
IPositional is the interface satisfied by a PositionalArg.
type Identifier ¶
type Identifier interface {
// contains filtered or unexported methods
}
An Identifier represents something we can use to identify an Arg.
type KeyValueArg ¶
A KeyValueArg represents a key=value argument where the key is a string and the value is a string representation for some type T.
Should be created by the functions: NewKeyValue, NewKeyValues, KeyValueUsingVariable and KeyValuesUsingVariable.
func KeyValueUsingVariable ¶
func KeyValueUsingVariable[T any](name string, v Variable[T], opts ...Option) *KeyValueArg[T]
KeyValueUsingVariable allows a KeyValueArg to be constructed using a Variable.
func KeyValuesUsingVariable ¶
func KeyValuesUsingVariable[T any](name string, v Variable[[]T], opts ...Option) *KeyValueArg[[]T]
KeyValuesUsingVariable allows a repeatable KeyValueArg to be constructed using Variable.
func NewKeyValue ¶
func NewKeyValue[T any](variable *T, name string, p parse.Parser[T], opts ...Option) *KeyValueArg[T]
NewKeyValue is a constructor for a key-value argument from the command-line.
func NewKeyValues ¶
func NewKeyValues[T any](variables *[]T, name string, p parse.Parser[T], opts ...Option) *KeyValueArg[[]T]
NewKeyValues is a constructor for a repeatable key-valued arguments from the command-line.
Automatically converts the Func[T] into a Func[[]T] via parse.Slice - use KeyValuesUsingVariable to be able to change this behaviour as required.
func (*KeyValueArg[T]) Default ¶
func (k *KeyValueArg[T]) Default() string
func (*KeyValueArg[T]) HasDefault ¶
func (k *KeyValueArg[T]) HasDefault() bool
func (*KeyValueArg[T]) IsParsed ¶
func (k *KeyValueArg[T]) IsParsed() bool
func (*KeyValueArg[T]) IsRepeatable ¶
func (k *KeyValueArg[T]) IsRepeatable() bool
func (*KeyValueArg[T]) IsRequired ¶
func (k *KeyValueArg[T]) IsRequired() bool
func (*KeyValueArg[T]) IsSupplied ¶
func (k *KeyValueArg[T]) IsSupplied() bool
func (*KeyValueArg[T]) Name ¶
func (k *KeyValueArg[T]) Name() string
func (*KeyValueArg[T]) Shorthand ¶
func (k *KeyValueArg[T]) Shorthand() string
func (*KeyValueArg[T]) Type ¶
func (k *KeyValueArg[T]) Type() Type
func (*KeyValueArg[T]) Usage ¶
func (k *KeyValueArg[T]) Usage() string
func (*KeyValueArg[T]) ValueType ¶
func (k *KeyValueArg[T]) ValueType() string
func (*KeyValueArg[T]) Variable ¶
func (k *KeyValueArg[T]) Variable() Variable[T]
type KeyValueDeriver ¶
type KeyValueDeriver interface {
DeriveKeyValue(v any, name string, opts ...Option) (IKeyValue, error)
}
A KeyValueDeriver is responsible for constructing a KeyValueArg dynamically.
type Metadata ¶
type Metadata struct {
// contains filtered or unexported fields
}
Metadata holds the metadata for some Arg.
Metadata can only be updated through Option implementations.
func NewMetadata ¶
NewMetadata is a constructor for a new Metadata.
func (*Metadata) HasDefault ¶
func (*Metadata) IsRequired ¶
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
An Option describes a change to some *Metadata.
func WithDefault ¶
WithDefault adds a default string value for some Arg, if applicable.
type ParseError ¶
type ParseError struct { Id Identifier Cause error }
ParseError indicates that an error occurred parsing the variable for some argument.
func ErrParsing ¶
func ErrParsing(id Identifier, cause error) *ParseError
ErrParsing is a constructor for a ParseError.
func (*ParseError) Error ¶
func (err *ParseError) Error() string
func (*ParseError) Unwrap ¶
func (err *ParseError) Unwrap() error
type Parser ¶
type Parser struct { // Id for the Parser Id string // Name of the program Name string // Description of the program Description string // ErrorHandling mode to be used by the Parser ErrorHandling ErrorHandling // underlying Set for the Parser - holding all the Arg types the Parser is responsible for *Set // Shift shifts the Parser along, ignoring the first 'shifted' arguments Shift int // Strict defines whether unrecognised tokens (e.g. flag/keys) are ignored, or return ErrUnidentified Strict bool // SuppressUsage stops the Usage from being written out when an error occurs SuppressUsage bool // SuppressValidation stops validation errors (i.e. adding arguments to the Parser) from breaking // the program SuppressValidation bool Stdin FileReader Stdout FileWriter Stderr FileWriter // contains filtered or unexported fields }
A Parser contains and parses a number of flag, key-value or positional inputs from the command-line.
Once all desired Arg have been added to the Parser (see Add), the arguments can be parsed using the Parse method.
By default, a Parser will read from os.Stdin and write to os.Stdout and os.Stderr - these fields can be changed as required to any FileReader or FileWriter.
func New ¶
New creates a new command-line argument Parser with ErrorHandling mode ContinueOnError.
`elements` can be either concrete Arg implementations, or structs with Arg(s) defined via struct-tags, which will be derived at runtime.
func NewAt ¶
func NewAt(name string, errHandling ErrorHandling, elements ...any) (*Parser, error)
NewAt is a constructor for a Parser at a specific ErrorHandling level.
If no elements are supplied, NewAt is guaranteed to return a nil error.
func NewParser ¶
func NewParser(n string, errHandling ErrorHandling) *Parser
NewParser is a constructor for a new command-line argument Parser.
func (*Parser) AddKeyValue ¶
AddKeyValue adds a key-value argument to the Parser.
func (*Parser) AddPosition ¶
func (p *Parser) AddPosition(a IPositional, opts ...Option) *Parser
AddPosition adds a positional argument to the Parser.
func (*Parser) Complete ¶
func (p *Parser) Complete(cmd *complete.Command)
Complete attaches arguments and flags to the completion Command for autocompletion support.
func (*Parser) Ok ¶
Ok checks the state of the Parser (both parse and validation errors, unless SuppressValidation is set)
Returns the *Parser for convenience.
func (*Parser) Parse ¶
Parse command-line input.
Parse does not return an error, instead, during the run any errors that occur are collected and stored internally to be retrieved via the Err method. If the ErrorHandling is not set to ContinueOnError, then errors during Parse will cause the program to either panic of exit.
func (*Parser) RawFlags ¶
RawFlags returns the raw flag arguments and their associated values detected by the Parser.
func (*Parser) RawKeyValues ¶
RawKeyValues returns the raw key-value arguments detected by the Parser.
func (*Parser) RawPositions ¶
RawPositions returns the raw ordered positional arguments detected by the Parser.
func (*Parser) State ¶
func (p *Parser) State(id Identifier) error
State checks the 'state' of some Arg by its Identifier *after* Parse has been called (always returning nil before).
func (*Parser) Valid ¶
Valid returns validation error(s) that occurred trying to add arguments to the Parser.
func (*Parser) WithDescription ¶
WithDescription sets a description for the Parser.
type Pipe ¶
type Pipe string
Pipe is an Identifier for the PipeArg.
Any value of Pipe will Identify the singular PipeArg in a Parser.
type PipeArg ¶
type PipeArg[T any] struct { // contains filtered or unexported fields }
PipeArg represents *the* (there can only be a single PipeArg defined per Parser) command-line inpu provided from the Stdout of another program.
func CSVPipe ¶
CSVPipe is a constructor for a PipeArg which reads comma-separated values from a pipe supplied via the command-line.
func NewLinePipe ¶
NewLinePipe is a constructor for a PipeArg which reads new lines from a pipe supplied via the command-line.
func NewPipeArg ¶
func PipeUsingVariable ¶
func (*PipeArg[T]) IsRepeatable ¶
IsRepeatable returns false - a pipe can only be supplied once.
func (*PipeArg[T]) IsRequired ¶
IsRequired returns false - a pipe is not required.
func (*PipeArg[T]) IsSupplied ¶
IsSupplied checks if a pipe has been supplied & data has been written to the pipe.
func (*PipeArg[T]) PipeInput ¶
func (p *PipeArg[T]) PipeInput() FileReader
type Piper ¶
A Piper is responsible for decoding the data from a pipe into raw command-line arguments
type PositionalArg ¶
type PositionalArg[T any] struct { // contains filtered or unexported fields }
A PositionalArg represents a particular index (or indexes) of positional arguments representing some type T.
Should be created by the NewPosition and NewPositions functions.
func NewPosition ¶
func NewPosition[T any](variable *T, index int, parser parse.Parser[T], opts ...Option) *PositionalArg[T]
NewPosition is a constructor for a positional argument at some index.
func NewPositions ¶
func NewPositions[T any](variables *[]T, fromIndex int, parser parse.Parser[T], opts ...Option) *PositionalArg[[]T]
NewPositions is a constructor for a number of positional arguments starting from some index.
func PositionUsingVariable ¶
func PositionUsingVariable[T any](index int, v Variable[T], opts ...Option) *PositionalArg[T]
func PositionsUsingVariable ¶
func PositionsUsingVariable[T any](fromIndex int, v Variable[[]T], opts ...Option) *PositionalArg[[]T]
func (*PositionalArg[T]) Default ¶
func (p *PositionalArg[T]) Default() string
func (*PositionalArg[T]) Index ¶
func (p *PositionalArg[T]) Index() int
func (*PositionalArg[T]) IsParsed ¶
func (p *PositionalArg[T]) IsParsed() bool
func (*PositionalArg[T]) IsRepeatable ¶
func (p *PositionalArg[T]) IsRepeatable() bool
func (*PositionalArg[T]) IsRequired ¶
func (p *PositionalArg[T]) IsRequired() bool
func (*PositionalArg[T]) IsSupplied ¶
func (p *PositionalArg[T]) IsSupplied() bool
func (*PositionalArg[T]) Name ¶
func (p *PositionalArg[T]) Name() string
func (*PositionalArg[T]) Shorthand ¶
func (p *PositionalArg[T]) Shorthand() string
func (*PositionalArg[T]) Type ¶
func (p *PositionalArg[T]) Type() Type
func (*PositionalArg[T]) Usage ¶
func (p *PositionalArg[T]) Usage() string
func (*PositionalArg[T]) ValueType ¶
func (p *PositionalArg[T]) ValueType() string
func (*PositionalArg[T]) Variable ¶
func (p *PositionalArg[T]) Variable() Variable[T]
type PositionalDeriver ¶
type PositionalDeriver interface {
DerivePosition(v any, index int, opts ...Option) (IPositional, error)
}
A PositionalDeriver is responsible for constructing a PositionalArg dynamically.
type ScanError ¶
type ScanError struct { Tokens []string // Tokens are the offending tokens Cause error // Cause is the error that the tokens lead to }
ScanError indicates that there was an error scanning the command-line inpu.
func ErrScanning ¶
ErrScanning is a constructor for a ScanError.
type SeparatedValuePiper ¶
type SeparatedValuePiper struct {
Separator string
}
type Set ¶
type Set struct {
// contains filtered or unexported fields
}
A Set is a container for a command-line Arg(s) of any Type.
func NewSetWithHelp ¶
func NewSetWithHelp() *Set
func (*Set) AddFlag ¶
AddFlag adds a flag argument to the Parser.
Returns ErrDuplicate if the key or alias already exists in the Set.
func (*Set) AddKeyValue ¶
AddKeyValue adds a key-value argument to the Set.
Returns ErrDuplicate if the key or alias already exists in the Set.
func (*Set) AddPipe ¶
AddPipe adds a pipe argument to the Set.
Returns ErrPipeExists if a pipe already exists in the Set.
func (*Set) AddPosition ¶
func (s *Set) AddPosition(a IPositional, opts ...Option) error
AddPosition adds a positional argument to the Set.
The Set expects positional arguments to be supplied in order and returns a wrapped ErrInvalidIndex if arguments are specified in an invalid order.
func (*Set) ByShorthand ¶
ByShorthand returns the Arg for the given shorthand identifier, if it exists, otherwise nil.
func (*Set) Get ¶
func (s *Set) Get(id Identifier) Arg
Get returns an Arg with the given Identifier, if it exists in the Set.
func (*Set) Has ¶
func (s *Set) Has(id Identifier) bool
Has returns true if there is an Arg with the given Identifier in the Set.
func (*Set) Key ¶
Key returns the key-value argument for the given Id/identifier, if it exists, otherwise nil.
func (*Set) Pos ¶
func (s *Set) Pos(index int) IPositional
Pos returns the positional argument at the supplied index, if it exists, otherwise nil.
func (*Set) PosS ¶
func (s *Set) PosS(sindex string) IPositional
PosS is a wrapper around Pos which accepts a string representation of the integer position.
func (*Set) Positions ¶
func (s *Set) Positions() []IPositional
Positions returns all positional arguments within the Set.
type Type ¶
type Type int
Type indicates the 'type' of input being processed (either a flag, key-value argument or a positional argument).
func TypeOf ¶
func TypeOf(id Identifier) Type
TypeOf is a helper function for getting the type of Arg from an Identifier.
type TypedArg ¶
type TypedArg[T any] interface { Arg // Variable returns the underlying Variable for the Arg Variable() Variable[T] }
TypedArg is the generic interface that is satisfied by all Arg implementations.
This is a convenience interface that provides access to the underlying generic Variable.
type Updater ¶
type Updater interface {
// contains filtered or unexported methods
}
Updater is the shared private behaviour shared by all Arg which allows mutable *Metadata fields to be updated and the underlying value/variable of an Arg to be updated.
type Variable ¶
type Variable[T any] interface { // Update parses the given input and attempts to update the underlying variable Update(...string) error // Ref returns a reference to the underlying variable Ref() *T // Unwrap returns the value of the underlying variable Unwrap() T // Parser returns the Func for the variable Parser() parse.Parser[T] }
A Variable refers to some program variable that can be parsed from string input.
Internally, a Variable uses a Func to parse some string input into the underlying variable.
func NewVariable ¶
NewVariable is a constructor for a Variable.
func NewVariables ¶
NewVariables is a constructor for a Variable that has an underlying argumentVariable of slice-type.
This is a helper function which converts the supplied Func into one that supports slices via Slice.
A specific Func can be used by creating a Variable using NewVariablesWithParser.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
examples
|
|
internal
|
|
pkg
|
|
flag
Package flag provides helper functions for creating clap.FlagArg(s) with common types.
|
Package flag provides helper functions for creating clap.FlagArg(s) with common types. |
keyvalue
Package keyvalue provides helper functions for creating clap.KeyValueArg(s) with common types.
|
Package keyvalue provides helper functions for creating clap.KeyValueArg(s) with common types. |
pos
Package pos provides helper functions for creating clap.PositionalArg(s) with common types.
|
Package pos provides helper functions for creating clap.PositionalArg(s) with common types. |