getopt

package
v0.20.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: BSD-2-Clause Imports: 4 Imported by: 1

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 program, consider using the flag package in the standard library instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Complete added in v0.18.0

func Complete(args []string, specs []*OptionSpec, cfg Config) ([]*Option, []string, Context)

Complete parses an argument list for completion. It returns the parsed options, the non-option arguments, and the context of the last argument. It tolerates unknown options, assuming that they take optional arguments.

Types

type Arity added in v0.18.0

type Arity uint

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

const (
	// The option takes no argument.
	NoArgument Arity = iota
	// The option requires 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 separate argument after the option (-o arg,
	// --long arg).
	RequiredArgument
	// The 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 (Arity) String added in v0.18.0

func (i Arity) String() string

type Config

type Config uint

Config configurates the parsing behavior.

const (
	// Stop parsing options after "--".
	StopAfterDoubleDash Config = 1 << iota
	// Stop parsing options before the first non-option argument.
	StopBeforeFirstNonOption
	// Allow long options to start with "-", and disallow short options.
	// Replicates the behavior of getopt_long_only and the flag package.
	LongOnly

	// Config to replicate the behavior of GNU's getopt_long.
	GNU = StopAfterDoubleDash
	// Config to replicate the behavior of BSD's getopt_long.
	BSD = StopAfterDoubleDash | StopBeforeFirstNonOption
)

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 *Option
	// Current partial long option name or argument. Non-empty when Type is
	// LongOption or Argument.
	Text string
}

Context describes the context of the last argument.

type ContextType

type ContextType uint

ContextType encodes how the last argument can be completed.

const (
	// OptionOrArgument indicates that the last element may be either a new
	// option or a new argument. Returned when it is an empty string.
	OptionOrArgument ContextType = iota
	// AnyOption indicates that the last element must be new option, short or
	// long. Returned when it is "-".
	AnyOption
	// 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 a non-option argument. The
	// partial argument is stored in Context.Text.
	Argument
)

func (ContextType) String

func (i ContextType) String() string

type Option

type Option struct {
	Spec     *OptionSpec
	Unknown  bool
	Long     bool
	Argument string
}

Option represents a parsed option.

func Parse added in v0.18.0

func Parse(args []string, specs []*OptionSpec, cfg Config) ([]*Option, []string, error)

Parse parses an argument list. It returns the parsed options, the non-option arguments, and any error.

type OptionSpec added in v0.18.0

type OptionSpec 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.
	Arity Arity
}

OptionSpec is a command-line option.

Jump to

Keyboard shortcuts

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