Documentation
¶
Overview ¶
Package optparse is a pure-Go (no cgo) reimplementation of the deterministic, interpreter-independent core of Ruby's OptionParser (stdlib `optparse`).
It models option *specifications* (the `on(...)` definitions), tokenizes an argv, performs long/short/abbreviation matching, handles `=`/bundled/`--[no-]`/ optional-argument forms, coerces values (Integer/Float/Array/custom lists), and reports the full OptionParser::ParseError taxonomy with MRI-exact message/reason/args/recover. It is the parsing engine; the per-match Ruby blocks registered by `on(...)` are *not* run here — Parse returns the ordered matches and coerced values, and the consumer (e.g. rbgo) dispatches the blocks.
The behavior mirrors MRI 4.0.5 / go-embedded-ruby's pure-Ruby OptionParser byte-for-byte across a broad differential corpus.
Index ¶
- Constants
- type ArgStyle
- type ErrorKind
- type GetoptsResult
- type Match
- type ParseError
- type Parser
- func (p *Parser) Define(opts []string, coerce string, list, values []string) int
- func (p *Parser) Getopts(argv []string, specs ...string) (map[string]*GetoptsResult, error)
- func (p *Parser) Help() string
- func (p *Parser) On(spec Spec) int
- func (p *Parser) OnHead(spec Spec) int
- func (p *Parser) Order(argv []string, nonOpt func(string)) (matches []Match, rest []string, err error)
- func (p *Parser) ParseBang(argv []string) (matches []Match, rest []string, err error)
- func (p *Parser) Permute(argv []string) (matches []Match, rest []string, err error)
- func (p *Parser) Separator(text string) *Parser
- func (p *Parser) Summarize() []string
- type Spec
Constants ¶
const ( CoerceNone = "" CoerceString = "String" CoerceInteger = "Integer" CoerceFloat = "Float" CoerceArray = "Array" // CoerceList selects the candidate-list converter built from Spec.List. CoerceList = "List" )
Coercion names the value converter applied to a matched argument before it is reported in a Match. The empty string means "no coercion" (the raw string is reported). Built-in names mirror MRI's accept defaults; CoerceList carries an explicit candidate set instead (see Spec.List).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrorKind ¶
type ErrorKind int
ErrorKind identifies which node of the OptionParser::ParseError tree an error belongs to. It maps 1:1 onto MRI's exception classes.
const ( // KindParseError is the base OptionParser::ParseError. KindParseError ErrorKind = iota // KindInvalidOption is OptionParser::InvalidOption. KindInvalidOption // KindMissingArgument is OptionParser::MissingArgument. KindMissingArgument // KindInvalidArgument is OptionParser::InvalidArgument. KindInvalidArgument // KindAmbiguousOption is OptionParser::AmbiguousOption. KindAmbiguousOption // KindAmbiguousArgument is OptionParser::AmbiguousArgument (an InvalidArgument). KindAmbiguousArgument // KindNeedlessArgument is OptionParser::NeedlessArgument. KindNeedlessArgument )
type GetoptsResult ¶
type GetoptsResult struct {
// TakesArg is true for "name:" specs (the option takes an argument).
TakesArg bool
// Flag is the boolean value for a no-argument option (false if absent).
Flag bool
// Str is the supplied argument for an arg-taking option, or nil if absent.
Str *string
}
GetoptsResult is the value of one getopts entry: a boolean flag, or a string argument (nil Str + TakesArg means the arg-taking option was absent).
type Match ¶
type Match struct {
// SpecIndex is the index (registration order) of the matched Spec.
SpecIndex int
// Value is the coerced argument. For a flag it is a bool: true, or false for
// the negated form of a "--[no-]flag". For an optional argument that was not
// supplied it is nil. Otherwise it is the coerced argument (string, int64,
// *big.Int, float64, []string, or a candidate-list value).
Value any
// Raw is the argument string as it appeared (before coercion), or "" for a flag.
Raw string
}
Match is one resolved option occurrence. The consumer dispatches the Ruby block registered for SpecIndex, passing Value.
type ParseError ¶
type ParseError struct {
// Kind selects the ParseError subtype.
Kind ErrorKind
// Args are the offending tokens, in MRI order. Error() renders them as
// "<reason>: <args joined by space>".
Args []string
}
ParseError is the Go analogue of OptionParser::ParseError. Its Error string, Reason, Args, Class and Recover all match MRI byte-for-byte.
func (*ParseError) Class ¶
func (e *ParseError) Class() string
Class returns the MRI exception class name (e.g. "OptionParser::InvalidOption"), for a consumer that re-raises the matching Ruby exception.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error renders the message exactly as MRI's #message: "reason: arg arg".
func (*ParseError) Reason ¶
func (e *ParseError) Reason() string
Reason returns the MRI class-level reason string (e.g. "invalid option").
func (*ParseError) Recover ¶
func (e *ParseError) Recover(argv []string) []string
Recover prepends this error's offending Args to argv and returns it, mirroring MRI's ParseError#recover (argv[0,0] = @args).
type Parser ¶
type Parser struct {
// Banner is the first help line; empty means "Usage: <ProgramName> [options]".
Banner string
// ProgramName is used in the default banner; empty means "optparse".
ProgramName string
// SummaryWidth is the option-column width (MRI default 32).
SummaryWidth int
// SummaryIndent is the left indent (MRI default " ").
SummaryIndent string
// contains filtered or unexported fields
}
Parser holds the registered option specs plus the help/summary layout. The zero value is not ready; use New.
func (*Parser) Define ¶
Define registers an option from raw MRI `on(...)` flag/description strings, returning the assigned spec index. It is sugar over MakeSpec + On.
func (*Parser) Getopts ¶
Getopts implements MRI #getopts: it parses each single-letter/word from the spec strings (a trailing ":" means the option takes an argument), registers the corresponding switches, parses argv with ParseBang, and returns a map from option name to its result. It mirrors the pure-Ruby OptionParser#getopts, including its scan grammar (so "ab" is one name "ab", not two flags).
On a parse error the partially-built result and the error are returned.
func (*Parser) Order ¶
func (p *Parser) Order(argv []string, nonOpt func(string)) (matches []Match, rest []string, err error)
Order is MRI #order!: parsing stops at the first non-option, which (with all the remaining argv) is returned as rest. If nonOpt is non-nil it is instead called for each leading non-option token and parsing continues (MRI's block form).
func (*Parser) ParseBang ¶
ParseBang consumes argv as MRI #parse! (== #permute!): options anywhere, with operands gathered and returned as rest. It returns the ordered matches (for the consumer to dispatch the blocks), the leftover operands, and the first *ParseError encountered.
type Spec ¶
type Spec struct {
// Short are the short forms, each including the leading dash (e.g. "-v").
Short []string
// Long are the long forms, each including the leading dashes (e.g. "--verbose").
// For a negatable switch this holds the affirmative form only ("--color"); the
// "--no-color" alias is synthesized at registration time.
Long []string
// ArgStyle is none/required/optional.
ArgStyle ArgStyle
// ArgName is the placeholder shown in help (e.g. "VALUE"), or "" if none.
ArgName string
// Negatable marks a "--[no-]flag" switch.
Negatable bool
// Coerce names the value converter (CoerceInteger, CoerceFloat, ...). Empty
// means no coercion.
Coerce string
// List is the ordered candidate set for CoerceList. For a value→value map use
// Keys with parallel Values; for a plain list leave Values nil and the key is
// returned as itself.
List []string
// Values, when non-nil and the same length as List, supplies the value each
// candidate maps to (MRI's Hash form of `on(...)`). Stored as the raw matched
// candidate string; the consumer maps it back to a Ruby object.
Values []string
// Desc are the description lines shown in help.
Desc []string
}
Spec is the parsed form of one MRI `on(...)` declaration. A consumer builds it from the Ruby call (rbgo maps the `on(*args, &block)` arguments onto these fields) and registers it with Parser.On; the matching Ruby block is held by the consumer and dispatched against the resulting Match.
func MakeSpec ¶
MakeSpec builds a Spec from the string arguments of an MRI `on(...)` call (the option flag strings such as "-v", "--verbose", "--name VALUE", "--[no-]color", "--name [VALUE]", and any trailing description strings). The optional coerce argument supplies a type/list converter that MRI would pass as a Class, Array, or Hash positional; pass "" for none.
For a candidate list, set coerce to CoerceList and provide list (and optionally values); for a built-in type pass CoerceInteger/CoerceFloat/CoerceArray/ CoerceString. rbgo maps the Ruby `on(*args)` positional arguments onto these.
