getopt

package
v0.0.0-...-c4f6918 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: BSD-3-Clause, BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package getopt provides simple command-line argument parsing, similar to the C function getopt described by POSIX. Optional arguments are not supported.

Index

Constants

View Source
const (
	// The value EndOption is returned when a non-option argument
	// is encountered or there are no arguments left.
	EndOption = -1
)

Variables

View Source
var (
	// ErrOption is returned when an invalid option is encountered.
	ErrOption = errors.New("getopt: option not supported")
	// ErrNoArg is returned when a required option argument is missing.
	ErrNoArg = errors.New("getopt: no argument given")
)

Functions

This section is empty.

Types

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

A Parser holds the slice of strings containing the arguments given on the command line (the first one being the program's name), the index of the argument currently processed, the position of the next character to parse, the options string, a boolean telling whether the last option has an argument, a boolean telling whether all options have been parsed.

func NewParser

func NewParser(args []string, opts string) *Parser

NewParser returns a pointer to a Parser initialized with the given args and opts. The first item of args will be skipped by the parser. This allows the direct use of os.Args as args array. The opts string has the same format as the one used by the C function getopt described by POSIX. Optional arguments are not supported.

func (*Parser) Args

func (p *Parser) Args() []string

Args returns a slice of strings containing the arguments that were not processed yet.

func (*Parser) OptArg

func (p *Parser) OptArg() string

OptArg returns the argument of the last option returned by Option, or the empty string if none was given.

func (*Parser) Option

func (p *Parser) Option() (rune, error)

Option returns the next option encountered as a rune and an error value. It returns (EndOption, nil) when a non-option argument is seen or arguments are exhausted. The error is not nil if the option is not valid or its required argument is missing.

Jump to

Keyboard shortcuts

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