option

package
v1.17.1 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithChoices

func WithChoices[T interface {
	Option
	SetChoices(...string)
}](opt T, choices ...string) T

WithChoices restricts an option to the given values and returns it, so that it may be declared inside an option list.

func WithDefault

func WithDefault[T interface {
	Option
	SetDefault(string)
}](opt T, value string) T

WithDefault gives an option the value it takes when no argument names it, and returns it.

func WithMetavar

func WithMetavar[T interface {
	Option
	SetMetavar(string)
}](opt T, metavar string) T

WithMetavar names an option's argument in the help message, and returns it. A positional is named this way: its metavar is what the help calls it, having no option name to go by.

func WithNargs

func WithNargs[T interface {
	Option
	SetNargs(Nargs)
}](opt T, nargs Nargs) T

WithNargs sets how many arguments an option takes, and returns it. It is chiefly for positionals, which may take one, an optional one, or any number.

func WithOptionalArgument

func WithOptionalArgument[T interface {
	Option
	SetNargs(Nargs)
	SetConst(string)
}](opt T, constValue string) T

WithOptionalArgument lets an option's argument be omitted, in which case it is set to constValue, and returns it.

Types

type BoolOption

type BoolOption struct {
	Value *bool
	// contains filtered or unexported fields
}

BoolOption takes no argument and records that the option was present.

func NewBoolOption

func NewBoolOption(shortName rune, longName string, usage string, required bool, value *bool) *BoolOption

func (*BoolOption) GetChoices

func (base *BoolOption) GetChoices() []string

func (*BoolOption) GetConst

func (base *BoolOption) GetConst() string

func (*BoolOption) GetDefault

func (base *BoolOption) GetDefault() *string

func (*BoolOption) GetLongName

func (base *BoolOption) GetLongName() string

func (*BoolOption) GetMetavar

func (base *BoolOption) GetMetavar() string

func (*BoolOption) GetNargs

func (base *BoolOption) GetNargs() Nargs

func (*BoolOption) GetRequired

func (base *BoolOption) GetRequired() bool

func (*BoolOption) GetShortName

func (base *BoolOption) GetShortName() string

GetShortName returns the option's short name, or the empty string when it has none. A zero ShortName must not render as "\x00": that is a name like any other, and every option lacking a short name would collide on it.

func (*BoolOption) GetUsage

func (base *BoolOption) GetUsage() string

func (*BoolOption) Set

func (boolOption *BoolOption) Set(_ string) error

func (*BoolOption) SetChoices

func (base *BoolOption) SetChoices(choices ...string)

func (*BoolOption) SetConst

func (base *BoolOption) SetConst(value string)

func (*BoolOption) SetDefault

func (base *BoolOption) SetDefault(value string)

func (*BoolOption) SetMetavar

func (base *BoolOption) SetMetavar(metavar string)

func (*BoolOption) SetNargs

func (base *BoolOption) SetNargs(nargs Nargs)

type ChoiceNormalizer

type ChoiceNormalizer interface {
	NormalizeChoice(string) (string, error)
}

ChoiceNormalizer is implemented by options whose values have more than one spelling, so that choices may be compared in a single form. An option that does not implement it has its choices compared against the argument as written.

type ChoicesProvider

type ChoicesProvider interface {
	GetChoices() []string
}

ChoicesProvider is implemented by options that accept only certain values. Every option built by this package implements it; a hand-written Option may.

type ConstProvider

type ConstProvider interface {
	GetConst() string
}

ConstProvider is implemented by NargsOptional options, supplying the value to use when the option is given without an argument.

type CountedOption

type CountedOption struct {
	Count *int
	// contains filtered or unexported fields
}

CountedOption takes no argument and counts how many times the option was given.

func NewCountedOption

func NewCountedOption(shortName rune, longName string, usage string, required bool, count *int) *CountedOption

func (*CountedOption) GetChoices

func (base *CountedOption) GetChoices() []string

func (*CountedOption) GetConst

func (base *CountedOption) GetConst() string

func (*CountedOption) GetDefault

func (base *CountedOption) GetDefault() *string

func (*CountedOption) GetLongName

func (base *CountedOption) GetLongName() string

func (*CountedOption) GetMetavar

func (base *CountedOption) GetMetavar() string

func (*CountedOption) GetNargs

func (base *CountedOption) GetNargs() Nargs

func (*CountedOption) GetRequired

func (base *CountedOption) GetRequired() bool

func (*CountedOption) GetShortName

func (base *CountedOption) GetShortName() string

GetShortName returns the option's short name, or the empty string when it has none. A zero ShortName must not render as "\x00": that is a name like any other, and every option lacking a short name would collide on it.

func (*CountedOption) GetUsage

func (base *CountedOption) GetUsage() string

func (*CountedOption) Set

func (countedOption *CountedOption) Set(_ string) error

func (*CountedOption) SetChoices

func (base *CountedOption) SetChoices(choices ...string)

func (*CountedOption) SetConst

func (base *CountedOption) SetConst(value string)

func (*CountedOption) SetDefault

func (base *CountedOption) SetDefault(value string)

func (*CountedOption) SetMetavar

func (base *CountedOption) SetMetavar(metavar string)

func (*CountedOption) SetNargs

func (base *CountedOption) SetNargs(nargs Nargs)

type DefaultProvider

type DefaultProvider interface {
	GetDefault() *string
}

DefaultProvider is implemented by options that are set to a value when no argument names them. The value is nil when no default was declared, which is what distinguishes having no default from defaulting to the empty string.

type FileOption

type FileOption struct {
	File *os.File
	Flag int
	Mode os.FileMode
	// contains filtered or unexported fields
}

FileOption opens the path it is given with Flag and Mode, as os.OpenFile does.

func NewFileOption

func NewFileOption(shortName rune, longName string, usage string, required bool, file *os.File) *FileOption

NewFileOption returns a FileOption that opens the path for reading. Use NewFileOptionExtra to choose the flag and mode.

func NewFileOptionExtra

func NewFileOptionExtra(
	shortName rune,
	longName string,
	usage string,
	required bool,
	flag int,
	mode os.FileMode,
	file *os.File,
) *FileOption

func (*FileOption) GetChoices

func (base *FileOption) GetChoices() []string

func (*FileOption) GetConst

func (base *FileOption) GetConst() string

func (*FileOption) GetDefault

func (base *FileOption) GetDefault() *string

func (*FileOption) GetLongName

func (base *FileOption) GetLongName() string

func (*FileOption) GetMetavar

func (base *FileOption) GetMetavar() string

func (*FileOption) GetNargs

func (base *FileOption) GetNargs() Nargs

func (*FileOption) GetRequired

func (base *FileOption) GetRequired() bool

func (*FileOption) GetShortName

func (base *FileOption) GetShortName() string

GetShortName returns the option's short name, or the empty string when it has none. A zero ShortName must not render as "\x00": that is a name like any other, and every option lacking a short name would collide on it.

func (*FileOption) GetUsage

func (base *FileOption) GetUsage() string

func (*FileOption) Set

func (fileOption *FileOption) Set(in string) error

func (*FileOption) SetChoices

func (base *FileOption) SetChoices(choices ...string)

func (*FileOption) SetConst

func (base *FileOption) SetConst(value string)

func (*FileOption) SetDefault

func (base *FileOption) SetDefault(value string)

func (*FileOption) SetMetavar

func (base *FileOption) SetMetavar(metavar string)

func (*FileOption) SetNargs

func (base *FileOption) SetNargs(nargs Nargs)

type IntOption

type IntOption struct {
	Value *int
	// contains filtered or unexported fields
}

IntOption stores the argument it is given as an int, replacing any previous value.

func NewIntOption

func NewIntOption(shortName rune, longName string, usage string, required bool, value *int) *IntOption

func (*IntOption) GetChoices

func (base *IntOption) GetChoices() []string

func (*IntOption) GetConst

func (base *IntOption) GetConst() string

func (*IntOption) GetDefault

func (base *IntOption) GetDefault() *string

func (*IntOption) GetLongName

func (base *IntOption) GetLongName() string

func (*IntOption) GetMetavar

func (base *IntOption) GetMetavar() string

func (*IntOption) GetNargs

func (base *IntOption) GetNargs() Nargs

func (*IntOption) GetRequired

func (base *IntOption) GetRequired() bool

func (*IntOption) GetShortName

func (base *IntOption) GetShortName() string

GetShortName returns the option's short name, or the empty string when it has none. A zero ShortName must not render as "\x00": that is a name like any other, and every option lacking a short name would collide on it.

func (*IntOption) GetUsage

func (base *IntOption) GetUsage() string

func (*IntOption) NormalizeChoice

func (intOption *IntOption) NormalizeChoice(in string) (string, error)

NormalizeChoice renders an int the one way, so that a choice of "7" also admits "007" and "+7".

func (*IntOption) Set

func (intOption *IntOption) Set(in string) error

func (*IntOption) SetChoices

func (base *IntOption) SetChoices(choices ...string)

func (*IntOption) SetConst

func (base *IntOption) SetConst(value string)

func (*IntOption) SetDefault

func (base *IntOption) SetDefault(value string)

func (*IntOption) SetMetavar

func (base *IntOption) SetMetavar(metavar string)

func (*IntOption) SetNargs

func (base *IntOption) SetNargs(nargs Nargs)

type IntsOption

type IntsOption struct {
	Value *[]int
	// contains filtered or unexported fields
}

IntsOption appends each argument it is given to a slice of ints.

func NewIntsOption

func NewIntsOption(shortName rune, longName string, usage string, required bool, value *[]int) *IntsOption

func (*IntsOption) GetChoices

func (base *IntsOption) GetChoices() []string

func (*IntsOption) GetConst

func (base *IntsOption) GetConst() string

func (*IntsOption) GetDefault

func (base *IntsOption) GetDefault() *string

func (*IntsOption) GetLongName

func (base *IntsOption) GetLongName() string

func (*IntsOption) GetMetavar

func (base *IntsOption) GetMetavar() string

func (*IntsOption) GetNargs

func (base *IntsOption) GetNargs() Nargs

func (*IntsOption) GetRequired

func (base *IntsOption) GetRequired() bool

func (*IntsOption) GetShortName

func (base *IntsOption) GetShortName() string

GetShortName returns the option's short name, or the empty string when it has none. A zero ShortName must not render as "\x00": that is a name like any other, and every option lacking a short name would collide on it.

func (*IntsOption) GetUsage

func (base *IntsOption) GetUsage() string

func (*IntsOption) NormalizeChoice

func (intsOption *IntsOption) NormalizeChoice(in string) (string, error)

NormalizeChoice renders an int the one way, so that a choice of "7" also admits "007" and "+7".

func (*IntsOption) Set

func (intsOption *IntsOption) Set(in string) error

func (*IntsOption) SetChoices

func (base *IntsOption) SetChoices(choices ...string)

func (*IntsOption) SetConst

func (base *IntsOption) SetConst(value string)

func (*IntsOption) SetDefault

func (base *IntsOption) SetDefault(value string)

func (*IntsOption) SetMetavar

func (base *IntsOption) SetMetavar(metavar string)

func (*IntsOption) SetNargs

func (base *IntsOption) SetNargs(nargs Nargs)

type Nargs

type Nargs string

Nargs describes how many arguments an option consumes.

const (
	// NargsOne marks an option that consumes exactly one argument. It is the zero value, so an
	// option that does not say otherwise takes one argument.
	NargsOne Nargs = ""
	// NargsNone marks an option that takes no argument, such as a flag or a counter.
	NargsNone Nargs = "0"
	// NargsOptional marks an option whose argument may be omitted. Given without one, it is set to
	// its constant instead.
	NargsOptional Nargs = "?"
	// NargsAtLeastOne marks an option that may be given repeatedly, accumulating its arguments, and
	// must be given at least once.
	NargsAtLeastOne Nargs = "+"
	// NargsAny is NargsAtLeastOne without the obligation: it accumulates, and may be left out.
	NargsAny Nargs = "*"
)

func (Nargs) IsVariadic

func (nargs Nargs) IsVariadic() bool

IsVariadic reports whether an arity accumulates its arguments rather than taking a fixed number.

func (Nargs) Minimum

func (nargs Nargs) Minimum() int

Minimum returns how many arguments an arity must be given.

type Option

type Option interface {
	Set(string) error
	GetShortName() string
	GetLongName() string
	GetUsage() string
	GetRequired() bool
	GetNargs() Nargs
	GetMetavar() string
}

Option is a single command-line option, addressable by a short name, a long name, or both.

type StringOption

type StringOption struct {
	Value *string
	// contains filtered or unexported fields
}

StringOption stores the argument it is given, replacing any previous value.

func NewStringOption

func NewStringOption(shortName rune, longName string, usage string, required bool, value *string) *StringOption

func (*StringOption) GetChoices

func (base *StringOption) GetChoices() []string

func (*StringOption) GetConst

func (base *StringOption) GetConst() string

func (*StringOption) GetDefault

func (base *StringOption) GetDefault() *string

func (*StringOption) GetLongName

func (base *StringOption) GetLongName() string

func (*StringOption) GetMetavar

func (base *StringOption) GetMetavar() string

func (*StringOption) GetNargs

func (base *StringOption) GetNargs() Nargs

func (*StringOption) GetRequired

func (base *StringOption) GetRequired() bool

func (*StringOption) GetShortName

func (base *StringOption) GetShortName() string

GetShortName returns the option's short name, or the empty string when it has none. A zero ShortName must not render as "\x00": that is a name like any other, and every option lacking a short name would collide on it.

func (*StringOption) GetUsage

func (base *StringOption) GetUsage() string

func (*StringOption) Set

func (stringOption *StringOption) Set(in string) error

func (*StringOption) SetChoices

func (base *StringOption) SetChoices(choices ...string)

func (*StringOption) SetConst

func (base *StringOption) SetConst(value string)

func (*StringOption) SetDefault

func (base *StringOption) SetDefault(value string)

func (*StringOption) SetMetavar

func (base *StringOption) SetMetavar(metavar string)

func (*StringOption) SetNargs

func (base *StringOption) SetNargs(nargs Nargs)

type StringsOption

type StringsOption struct {
	Value *[]string
	// contains filtered or unexported fields
}

StringsOption appends each argument it is given to a slice of strings.

func NewStringsOption

func NewStringsOption(shortName rune, longName string, usage string, required bool, value *[]string) *StringsOption

func (*StringsOption) GetChoices

func (base *StringsOption) GetChoices() []string

func (*StringsOption) GetConst

func (base *StringsOption) GetConst() string

func (*StringsOption) GetDefault

func (base *StringsOption) GetDefault() *string

func (*StringsOption) GetLongName

func (base *StringsOption) GetLongName() string

func (*StringsOption) GetMetavar

func (base *StringsOption) GetMetavar() string

func (*StringsOption) GetNargs

func (base *StringsOption) GetNargs() Nargs

func (*StringsOption) GetRequired

func (base *StringsOption) GetRequired() bool

func (*StringsOption) GetShortName

func (base *StringsOption) GetShortName() string

GetShortName returns the option's short name, or the empty string when it has none. A zero ShortName must not render as "\x00": that is a name like any other, and every option lacking a short name would collide on it.

func (*StringsOption) GetUsage

func (base *StringsOption) GetUsage() string

func (*StringsOption) Set

func (stringsOption *StringsOption) Set(in string) error

func (*StringsOption) SetChoices

func (base *StringsOption) SetChoices(choices ...string)

func (*StringsOption) SetConst

func (base *StringsOption) SetConst(value string)

func (*StringsOption) SetDefault

func (base *StringsOption) SetDefault(value string)

func (*StringsOption) SetMetavar

func (base *StringsOption) SetMetavar(metavar string)

func (*StringsOption) SetNargs

func (base *StringsOption) SetNargs(nargs Nargs)

Jump to

Keyboard shortcuts

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