getopt

package module
v0.0.0-...-0df81d2 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2022 License: 0BSD Imports: 5 Imported by: 2

README

getopt

Package getopt provides a minimal, getopt(3)-like argument parsing implementation with POSIX compatible semantics.

Go Reference Tests

Example
package main

import (
	"fmt"

	"github.com/dmgk/getopt"
)

// go run example.go -ba42 -v -z -- -w arg1 arg2
func main() {
	// -a requires an argument
	// -b and -v have no arguments
	// -z may have an optional argument
	opts, err := getopt.New("a:bz::v")
	if err != nil {
		fmt.Printf("error creating scanner: %s\n", err)
		return
	}

	for opts.Scan() {
		opt, err := opts.Option()
		if err != nil {
			fmt.Printf("%s: error parsing option: %s\n", opts.ProgramName(), err)
			continue
		}

		if opt.HasArg() {
			fmt.Printf("%s: got option %q with arg %q\n", opts.ProgramName(), opt.Opt, opt)
		} else {
			fmt.Printf("%s: got option %q\n", opts.ProgramName(), opt.Opt)
		}
	}

	fmt.Printf("%s: remaining arguments: %v\n", opts.ProgramName(), opts.Args())
}
$ go run example.go -ba42 -v -z -- -w arg1 arg2
example: got option 'b'
example: got option 'a' with arg "42"
example: got option 'v'
example: got option 'z'
example: remaining arguments: [-w arg1 arg2]

Documentation

Overview

Package getopt provides a minimal, getopt(3)-like argument parsing implementation with POSIX compatible semantics.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InvalidOptionError

type InvalidOptionError byte

InvalidOptionError is returned when scanner encounters an option not listed in optstring.

func (InvalidOptionError) Error

func (e InvalidOptionError) Error() string

type MissingArgumentError

type MissingArgumentError byte

MissingArgumentError is returned when option is missing a required argument.

func (MissingArgumentError) Error

func (e MissingArgumentError) Error() string

type Option

type Option struct {
	// Option name
	Opt byte
	// Option argument, if any
	Arg *string
}

Option contains option name and optional argument value.

func (*Option) Float32

func (o *Option) Float32() (float32, error)

func (*Option) Float64

func (o *Option) Float64() (float64, error)

func (*Option) HasArg

func (o *Option) HasArg() bool

func (*Option) Int

func (o *Option) Int() (int, error)

func (*Option) Int32

func (o *Option) Int32() (int32, error)

func (*Option) Int64

func (o *Option) Int64() (int64, error)

func (*Option) String

func (o *Option) String() string

func (*Option) Uint

func (o *Option) Uint() (uint, error)

func (*Option) Uint32

func (o *Option) Uint32() (uint32, error)

func (*Option) Uint64

func (o *Option) Uint64() (uint64, error)

type Scanner

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

Scanner contains option scanner data.

func New

func New(optstring string) (*Scanner, error)

New returns a new options scanner using os.Args as the command line arguments source. The option string optstring may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. If optstring starts with ':' then all option argument are treated as optional.

func NewArgv

func NewArgv(optstring string, argv []string) (*Scanner, error)

New returns a new options scanner using passed argv as the command line argument source. The option string optstring may contain the following elements: individual characters, and characters followed by a colon to indicate an option argument is to follow. If optstring starts with ':' then all option argument are treated as optional.

Example
scanner, err := NewArgv("a:bz::v", []string{"getopt", "-ba42", "-v", "-z", "--", "-w", "arg1", "arg2"})
if err != nil {
	panic("error creating scanner: " + err.Error())
}

for scanner.Scan() {
	opt, err := scanner.Option()
	if err != nil {
		panic("error: " + err.Error())
	}

	if opt.HasArg() {
		fmt.Printf("%s: got option %q with arg %q\n", scanner.ProgramName(), opt.Opt, opt)
	} else {
		fmt.Printf("%s: got option %q\n", scanner.ProgramName(), opt.Opt)
	}
}
fmt.Printf("%s: remaining arguments: %q\n", scanner.ProgramName(), scanner.Args())
Output:

getopt: got option 'b'
getopt: got option 'a' with arg "42"
getopt: got option 'v'
getopt: got option 'z'
getopt: remaining arguments: ["-w" "arg1" "arg2"]

func (*Scanner) Args

func (s *Scanner) Args() []string

Args returns remaining command line arguments.

func (*Scanner) Option

func (s *Scanner) Option() (*Option, error)

Option returns the next option or an error when it encounters an unknown option or an option that is missing a required argument. If optstring starts with ':' then all arguments are treated as optional and missing arguments do not cause errors.

func (*Scanner) ProgramName

func (s *Scanner) ProgramName() string

ProgramName returns basename of argv[0].

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan advances options scanner to the next option. It returns false when there are no more options or parsing is terminated by "--".

Jump to

Keyboard shortcuts

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