getopt

package
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2018 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package getopt implements a command-line argument parser.

It tries to cover all common styles of option syntaxes, and provides context information when given a partial input. It is mainly useful for writing completion engines and wrapper programs.

If you are looking for an option parser for your go programm, consider using the flag package in the standard library instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config uint

Config configurates the parsing behavior.

const (
	// DoubleDashTerminatesOptions indicates that all elements after an argument
	// "--" are treated as arguments.
	DoubleDashTerminatesOptions Config = 1 << iota
	// FirstArgTerminatesOptions indicates that all elements after the first
	// argument are treated as arguments.
	FirstArgTerminatesOptions
	// LongOnly indicates that long options may be started by either one or two
	// dashes, and short options are not allowed. Should replicate the behavior
	// of getopt_long_only and the
	// flag package of the Go standard library.
	LongOnly
	// GNUGetoptLong is a configuration that should replicate the behavior of
	// GNU getopt_long.
	GNUGetoptLong = DoubleDashTerminatesOptions
	// POSIXGetopt is a configuration that should replicate the behavior of
	// POSIX getopt.
	POSIXGetopt = DoubleDashTerminatesOptions | FirstArgTerminatesOptions
)

func (Config) HasAll

func (conf Config) HasAll(flags Config) bool

HasAll tests whether a configuration has all specified flags set.

func (Config) String

func (i Config) String() string

type Context

type Context struct {
	// The nature of the context.
	Type ContextType
	// Current option, with a likely incomplete Argument. Non-nil when Type is
	// OptionArgument.
	Option *ParsedOption
	// Current partial long option name or argument. Non-empty when Type is
	// LongOption or Argument.
	Text string
}

Context indicates what may come after the supplied argument list.

type ContextType

type ContextType uint

ContextType encodes what may be appended to the last element of the argument list.

const (
	// NewOptionOrArgument indicates that the last element may be either a new
	// option or a new argument. Returned when it is an empty string.
	NewOptionOrArgument ContextType = iota
	// NewOption indicates that the last element must be new option, short or
	// long. Returned when it is "-".
	NewOption
	// NewLongOption indicates that the last element must be a new long option.
	// Returned when it is "--".
	NewLongOption
	// LongOption indicates that the last element is a long option, but not its
	// argument. The partial name of the long option is stored in Context.Text.
	LongOption
	// ChainShortOption indicates that a new short option may be chained.
	// Returned when the last element consists of a chain of options that take
	// no arguments.
	ChainShortOption
	// OptionArgument indicates that the last element list must be an argument
	// to an option. The option in question is stored in Context.Option.
	OptionArgument
	// Argument indicates that the last element is an argument. The partial
	// argument is stored in Context.Text.
	Argument
)

func (ContextType) String

func (i ContextType) String() string

type Getopt

type Getopt struct {
	Options []*Option
	Config  Config
}

Getopt specifies the syntax of command-line arguments.

func (*Getopt) Parse

func (g *Getopt) Parse(elems []string) ([]*ParsedOption, []string, *Context)

Parse parses an argument list.

type HasArg

type HasArg uint

HasArg indicates whether an option takes an argument, and whether it is required.

const (
	// NoArgument indicates that an option takes no argument.
	NoArgument HasArg = iota
	// RequiredArgument indicates that an option must take an argument. The
	// argument can come either directly after a short option (-oarg), after a
	// long option followed by an equal sign (--long=arg), or as a subsequent
	// argument after the option (-o arg, --long arg).
	RequiredArgument
	// OptionalArgument indicates that an option takes an optional argument.
	// The argument can come either directly after a short option (-oarg) or
	// after a long option followed by an equal sign (--long=arg).
	OptionalArgument
)

func (HasArg) String

func (i HasArg) String() string

type Option

type Option struct {
	// Short option. Set to 0 for long-only.
	Short rune
	// Long option. Set to "" for short-only.
	Long string
	// Whether the option takes an argument, and whether it is required.
	HasArg HasArg
}

Option is a command-line option.

type ParsedOption

type ParsedOption struct {
	Option   *Option
	Long     bool
	Argument string
}

ParsedOption represents a parsed option.

Jump to

Keyboard shortcuts

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