Documentation
¶
Overview ¶
Package getoptlong is a pure-Go (no cgo) reimplementation of Ruby's GetoptLong — the getopt-style command-line option parser shipped with MRI (ruby/getoptlong 0.2.1, the version bundled with Ruby 4.0.5).
It is a faithful, byte-for-byte port of MRI's option-scanning algorithm: the same long/short/abbreviation matching, the same =-joined and separate arguments, the same bundled short flags, the same `--` terminator, the same three ordering modes (REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER) and the same error taxonomy (InvalidOption, MissingArgument, NeedlessArgument, AmbiguousOption) with MRI-identical messages.
Unlike Ruby's GetoptLong, which mutates the global ARGV, this package operates on an explicit argument slice owned by the Parser, so it is reusable and free of global state. The host (for example go-embedded-ruby's `rbgo`) binds ARGV to a Parser and reads the remaining, non-option arguments back from Parser.Args after processing.
The scanning is pure compute and needs no Ruby runtime; the package has no dependencies.
Index ¶
- type ArgumentFlag
- type Error
- type ErrorKind
- type Option
- type Ordering
- type Parser
- func (p *Parser) Each(fn func(name, argument string)) error
- func (p *Parser) Err() *Error
- func (p *Parser) ErrorMessage() string
- func (p *Parser) GetNext() (name, argument string, ok bool, err error)
- func (p *Parser) Ordering() Ordering
- func (p *Parser) Quiet() bool
- func (p *Parser) SetOptions(options ...Option) error
- func (p *Parser) SetOrdering(o Ordering) error
- func (p *Parser) SetQuiet(q bool)
- func (p *Parser) Terminated() bool
- type SpecError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArgumentFlag ¶
type ArgumentFlag int
ArgumentFlag describes whether an option takes an argument. The values match Ruby's GetoptLong::NO_ARGUMENT / REQUIRED_ARGUMENT / OPTIONAL_ARGUMENT.
const ( // NoArgument is GetoptLong::NO_ARGUMENT — the option takes no argument. NoArgument ArgumentFlag = 0 // RequiredArgument is GetoptLong::REQUIRED_ARGUMENT — the option must be // followed by an argument. RequiredArgument ArgumentFlag = 1 // OptionalArgument is GetoptLong::OPTIONAL_ARGUMENT — the option may be // followed by an argument. OptionalArgument ArgumentFlag = 2 )
type Error ¶
type Error struct {
// Kind is the concrete error class (InvalidOption, MissingArgument,
// NeedlessArgument, or AmbiguousOption).
Kind ErrorKind
// Message is the POSIX-format message, identical to Ruby's
// GetoptLong#error_message.
Message string
}
Error is the base of the GetoptLong error taxonomy; it corresponds to GetoptLong::Error (a StandardError in Ruby). Every parsing error returned by GetNext wraps one of the four concrete kinds and reports an MRI-identical message via Error.Error.
type ErrorKind ¶
type ErrorKind int
ErrorKind enumerates the concrete GetoptLong error classes.
const ( // InvalidOption is GetoptLong::InvalidOption — an unrecognised option. InvalidOption ErrorKind = iota // MissingArgument is GetoptLong::MissingArgument — a required argument is // absent. MissingArgument // NeedlessArgument is GetoptLong::NeedlessArgument — an argument was given // to a NO_ARGUMENT option. NeedlessArgument // AmbiguousOption is GetoptLong::AmbiguousOption — an abbreviation matches // more than one option. AmbiguousOption )
type Option ¶
type Option struct {
// Names is the canonical name (first) followed by any aliases.
Names []string
// Flag is the argument flag for this option.
Flag ArgumentFlag
}
Option defines a single option: a canonical name, its aliases, and the argument flag. It is the Go form of one array passed to Ruby's GetoptLong.new, e.g. ["--name", "-n", GetoptLong::REQUIRED_ARGUMENT].
Names is the list of name and aliases; the first element is the canonical name reported by GetNext. Each name must be either "-" followed by a single non-hyphen character, or "--" followed by one or more characters.
type Ordering ¶
type Ordering int
Ordering selects how options and non-option arguments are interpreted. The values match Ruby's GetoptLong::REQUIRE_ORDER / PERMUTE / RETURN_IN_ORDER.
const ( // RequireOrder stops option processing at the first non-option word; every // word after it is a non-option word (GetoptLong::REQUIRE_ORDER). RequireOrder Ordering = 0 // Permute lets options and non-option arguments mix in any order; the // non-option words are collected and returned at the end (GetoptLong::PERMUTE). // This is the default for a new Parser. Permute Ordering = 1 // ReturnInOrder treats every word as an option: a non-option word is // returned as an option whose name is "" and whose value is the word // (GetoptLong::RETURN_IN_ORDER). ReturnInOrder Ordering = 2 )
type Parser ¶
type Parser struct {
// Args is the working argument slice. GetNext consumes options from the
// front; on termination the collected non-option arguments are restored
// here, so after a full scan Args is exactly the remaining ARGV.
Args []string
// ProgName is prepended to error messages written to ErrorWriter, matching
// Ruby's $0. If empty, no program-name prefix is written.
ProgName string
// ErrorWriter receives error messages unless Quiet is set. Ruby writes them
// to $stderr; if ErrorWriter is nil, messages are discarded (but GetNext
// still returns the error). Set it to os.Stderr to mirror Ruby exactly.
ErrorWriter io.Writer
// contains filtered or unexported fields
}
Parser scans an argument slice for options. It is the Go counterpart of a GetoptLong instance. Configure it with New (or NewParser + SetOptions), set the ordering and quiet mode if needed, then call GetNext repeatedly. After processing, Args holds the remaining non-option arguments (the equivalent of the ARGV that Ruby leaves behind).
func New ¶
New returns a Parser configured to scan args for the given options, in the default PERMUTE ordering. It is the counterpart of GetoptLong.new. A bad option specification returns a *SpecError.
func NewParser ¶
func NewParser() *Parser
NewParser returns an empty Parser in the default PERMUTE ordering with no options configured. Add options with SetOptions. The argument slice defaults to nil; assign Parser.Args before scanning.
func (*Parser) Each ¶
Each calls fn with each successive option until processing ends. It corresponds to GetoptLong#each / #each_option. It stops and returns the error if GetNext reports one.
func (*Parser) Err ¶
Err returns the error that terminated processing, or nil if none. It corresponds to GetoptLong#error.
func (*Parser) ErrorMessage ¶
ErrorMessage returns the message of the error that terminated processing, or "" if none. It corresponds to GetoptLong#error_message.
func (*Parser) GetNext ¶
GetNext returns the next option as (name, argument, ok). name is the canonical option name (never an alias); argument is its value ("" when the option takes no argument or an optional argument was absent). ok is false when there are no more options — either the input is exhausted or an error occurred; check err for the latter. GetNext corresponds to GetoptLong#get / #get_option.
On error, GetNext returns ("", "", false) together with a non-nil *Error and records it (see Err / ErrorMessage).
func (*Parser) SetOptions ¶
SetOptions replaces the configured options. It corresponds to GetoptLong#set_options. It fails once processing has started, or if any specification is invalid (returning a *SpecError with an MRI-identical message). On any spec error the option tables are left empty, as in Ruby.
func (*Parser) SetOrdering ¶
SetOrdering sets the ordering. It corresponds to GetoptLong#ordering=. It fails once option processing has started. Unlike Ruby, this package does not consult the POSIXLY_CORRECT environment variable; pass RequireOrder explicitly for POSIX behaviour.
func (*Parser) SetQuiet ¶
SetQuiet sets quiet mode; when true, error messages are not written to ErrorWriter (GetNext still returns the error). It corresponds to GetoptLong#quiet=.
func (*Parser) Terminated ¶
Terminated reports whether option processing has finished. It corresponds to GetoptLong#terminated?.
