Documentation
¶
Overview ¶
Package options provides methods for working with command-line options
Index ¶
- Constants
- Variables
- func Add(name string, opt *V) error
- func Delete(name string) bool
- func F(opt string) string
- func Format(opt string) string
- func GetB(name string) bool
- func GetF(name string) float64
- func GetI(name string) int
- func GetS(name string) string
- func Has(name string) bool
- func Is(name string, value any) bool
- func Merge(opts ...string) string
- func Parse(optMap ...Map) (Arguments, Errors)
- func ParseOptionName(opt string) (string, string)
- func Q(opts ...string) string
- func Split(name string) []string
- type Argument
- func (a Argument) Base() Argument
- func (a Argument) Bool() (bool, error)
- func (a Argument) Clean() Argument
- func (a Argument) Dir() Argument
- func (a Argument) Ext() Argument
- func (a Argument) Float() (float64, error)
- func (a Argument) Int() (int, error)
- func (a Argument) Int64() (int64, error)
- func (a Argument) Is(value any) bool
- func (a Argument) IsAbs() bool
- func (a Argument) Match(pattern string) (bool, error)
- func (a Argument) String() string
- func (a Argument) ToLower() Argument
- func (a Argument) ToUpper() Argument
- func (a Argument) Uint() (uint64, error)
- type Arguments
- func (a Arguments) Append(args ...string) Arguments
- func (a Arguments) Filter(pattern string) Arguments
- func (a Arguments) Flatten() string
- func (a Arguments) Get(index int) Argument
- func (a Arguments) Has(index int) bool
- func (a Arguments) Last() Argument
- func (a Arguments) Strings() []string
- func (a Arguments) Unshift(args ...string) Arguments
- type Errors
- type Map
- type OptionError
- type Options
- func (o *Options) Add(name string, option *V) error
- func (o *Options) AddMap(optMap Map) Errors
- func (o *Options) Delete(name string) bool
- func (o *Options) GetB(name string) bool
- func (o *Options) GetF(name string) float64
- func (o *Options) GetI(name string) int
- func (o *Options) GetS(name string) string
- func (o *Options) Has(name string) bool
- func (o *Options) Is(name string, value any) bool
- func (o *Options) Parse(rawOpts []string, optMap ...Map) (Arguments, Errors)
- func (o *Options) Split(name string) []string
- type V
Examples ¶
- Add
- AddMap
- Argument.Base
- Argument.Bool
- Argument.Clean
- Argument.Dir
- Argument.Ext
- Argument.Float
- Argument.Int
- Argument.Int64
- Argument.Is
- Argument.IsAbs
- Argument.Match
- Argument.String
- Argument.ToLower
- Argument.ToUpper
- Argument.Uint
- Arguments.Append
- Arguments.Filter
- Arguments.Flatten
- Arguments.Get
- Arguments.Has
- Arguments.Last
- Arguments.Strings
- Arguments.Unshift
- Delete
- Format
- GetB
- GetF
- GetI
- GetS
- Has
- Is
- Map.Delete
- Map.Set
- Map.String
- NewArguments
- NewOptions
- Options.Add
- Options.AddMap
- Options.Delete
- Options.GetB
- Options.GetF
- Options.GetI
- Options.GetS
- Options.Has
- Options.Parse
- Options.Split
- Parse
- Split
- V.String
Constants ¶
const ( STRING uint8 = iota // String option INT // Int/Uint option BOOL // Boolean option FLOAT // Floating number option MIXED // String or boolean option )
Options types
const ( ERROR_UNSUPPORTED = iota ERROR_DUPLICATE_LONGNAME ERROR_DUPLICATE_SHORTNAME ERROR_OPTION_IS_NIL ERROR_EMPTY_VALUE ERROR_WRONG_FORMAT ERROR_CONFLICT ERROR_BOUND_NOT_SET ERROR_UNSUPPORTED_VALUE ERROR_UNSUPPORTED_ALIAS_LIST_FORMAT ERROR_UNSUPPORTED_CONFLICT_LIST_FORMAT ERROR_UNSUPPORTED_BOUND_LIST_FORMAT )
Error codes
Variables ¶
var ( // ErrNilOptions returns if options struct is nil ErrNilOptions = fmt.Errorf("Options struct is nil") // ErrNilMap returns if options map is nil ErrNilMap = fmt.Errorf("Options map is nil") // ErrEmptyName returns if option have no name ErrEmptyName = fmt.Errorf("One or more options do not have a name") )
var MergeSymbol = " "
MergeSymbol used for joining several mergeble options with string value
Functions ¶
func Delete ¶ added in v12.127.0
Delete deletes option with given name
You can use this method to remove options with private data such as passwords, tokens, etc.
Example ¶
Output:
func ParseOptionName ¶
ParseOptionName parses combined name and returns long and short options
Types ¶
type Argument ¶ added in v12.45.0
type Argument string
Argument is command argument
func (Argument) Base ¶ added in v12.45.0
Base is shorthand analog of path.Base
Example ¶
Output: Arguments: [run /srv/app//conf/myapp.conf] Clean: /srv/app/conf/myapp.conf Base: myapp.conf Dir: /srv/app/conf Ext: .conf IsAbs: true
func (Argument) Bool ¶ added in v12.45.0
Int converts argument to int
Example ¶
Output: Arguments: [release yes] Force: true
func (Argument) Clean ¶ added in v12.45.0
Clean is shorthand analog of path.Clean
Example ¶
Output: Arguments: [run /srv/app//conf/myapp.conf] Clean: /srv/app/conf/myapp.conf Base: myapp.conf Dir: /srv/app/conf Ext: .conf IsAbs: true
func (Argument) Dir ¶ added in v12.45.0
Dir is shorthand analog of path.Dir
Example ¶
Output: Arguments: [run /srv/app//conf/myapp.conf] Clean: /srv/app/conf/myapp.conf Base: myapp.conf Dir: /srv/app/conf Ext: .conf IsAbs: true
func (Argument) Ext ¶ added in v12.45.0
Ext is shorthand analog of path.Ext
Example ¶
Output: Arguments: [run /srv/app//conf/myapp.conf] Clean: /srv/app/conf/myapp.conf Base: myapp.conf Dir: /srv/app/conf Ext: .conf IsAbs: true
func (Argument) Float ¶ added in v12.45.0
Int converts argument to int
Example ¶
Output: Arguments: [ratio 2.37] Ratio: 2.37
func (Argument) Int ¶ added in v12.45.0
Int converts argument to int
Example ¶
Output: Arguments: [head file.txt 10] Lines: 10
func (Argument) Int64 ¶ added in v12.45.0
Int64 converts argument to int64
Example ¶
Output: Arguments: [head file.txt 10] Lines: 10
func (Argument) Is ¶ added in v12.49.0
Is returns true if argument equals to given value
Example ¶
Output: Arguments: [parse fileA.txt fileB.jpg fileC.txt] Command is "parse": true Command is "clear": false
func (Argument) IsAbs ¶ added in v12.45.0
IsAbs is shorthand analog of path.IsAbs
Example ¶
Output: Arguments: [run /srv/app//conf/myapp.conf] Clean: /srv/app/conf/myapp.conf Base: myapp.conf Dir: /srv/app/conf Ext: .conf IsAbs: true
func (Argument) Match ¶ added in v12.45.0
Match is shorthand analog of path.Match
Example ¶
Output: Arguments: [parse fileA.txt fileB.jpg] fileA.txt is match: true fileB.jpg is match: false
func (Argument) String ¶ added in v12.45.0
String converts argument to string
Example ¶
Output: Arguments: [head file.txt 10] Command: head
func (Argument) ToLower ¶ added in v12.45.0
ToLower returns argument converted to lower case
Example ¶
Output: Arguments: [add-user John] User: john
type Arguments ¶
type Arguments []Argument
Arguments is a slice with with command argument
func NewArguments ¶ added in v12.45.0
NewArguments creates new arguments slice from given strings
Example ¶
Output: Arguments: [head file.txt 10] Command: head File: file.txt Lines: 10
func (Arguments) Append ¶ added in v12.45.0
Append adds arguments to the end of the arguments slices
Example ¶
Output: Arguments: [head file.txt 10]
func (Arguments) Filter ¶ added in v12.45.0
Filter filters arguments by a given glob pattern. This method works only with files. It means that for a given path only the last part will be checked for pattern matching.
Example ¶
Output: Arguments: [parse fileA.txt fileB.jpg fileC.txt] Text files: [fileA.txt fileC.txt]
func (Arguments) Flatten ¶ added in v12.66.0
Flatten converts arguments to the string (useful for custom parsing logic)
Example ¶
Output: Arguments: head file.txt 10
func (Arguments) Get ¶
Get returns argument with given index
Example ¶
Output: Arguments: [head file.txt 10] Command: head File: file.txt Lines: 10
func (Arguments) Has ¶
Has returns true if arguments contains argument with given index
Example ¶
Output: Arguments: [head file.txt 10] Has command: true
func (Arguments) Last ¶ added in v12.45.0
Get returns the last argument
Example ¶
Output: Arguments: [head file.txt 10] Last argument: 10
type Errors ¶ added in v12.117.0
type Errors []error
Errors is a slice with errors
type Map ¶
Map is map with list of options
func (Map) Delete ¶ added in v12.108.0
Delete removes option from map
Example ¶
Output: Option -t/--test deleted
type OptionError ¶
OptionError is argument parsing error
func (OptionError) Error ¶
func (e OptionError) Error() string
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options is options struct
func NewOptions ¶
func NewOptions() *Options
NewOptions creates new options struct
Example ¶
Output: Arguments: [file.txt] User: bob Lines: 12
func (*Options) Add ¶
Add adds a new option
Example ¶
Output: Arguments: [file.txt] User: bob Lines: 12
func (*Options) AddMap ¶
AddMap adds map with supported options
Example ¶
Output: Arguments: [file.txt] User: bob Lines: 12
func (*Options) Delete ¶ added in v12.127.0
Delete deletes option with given name
You can use this method to remove options with private data such as passwords, tokens, etc.
Example ¶
Output: Arguments: [file.txt] Has user option: true Has lines option: false Has password option: false
func (*Options) GetB ¶
GetB returns option value as boolean
Example ¶
Output: Arguments: [file.txt] User: bob Force: true
func (*Options) GetF ¶
GetF returns option value as floating number
Example ¶
Output: Arguments: [file.txt] User: bob Ratio: 2.35
func (*Options) GetI ¶
GetI returns option value as integer
Example ¶
Output: Arguments: [file.txt] User: bob Lines: 12
func (*Options) GetS ¶
GetS returns option value as string
Example ¶
Output: Arguments: [file.txt] User: bob Lines: 12
func (*Options) Has ¶
Has checks if option with given name exists and set
Example ¶
Output: Arguments: [file.txt] Has user option: true Has lines option: false
type V ¶
type V struct { Type uint8 // option type Max float64 // maximum integer option value Min float64 // minimum integer option value Alias any // string or slice of strings with aliases Conflicts any // string or slice of strings with conflicts options Bound any // string or slice of strings with bound options Mergeble bool // option supports options value merging Value any // default value // contains filtered or unexported fields }
V is basic option struct