option

package
v0.8.17 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCantQuoteString = errors.New("can't quote string")

ErrCantQuoteString is the error Quoted returns if a key or a value contains both a ' and a " which is an un-parseable option.

Functions

func Check

func Check(s string) []listener.Diagnostic

Check parses an option string returning Diagnostics

func Match

func Match(o1 Option, o2 Option) bool

Match returns true, if for all keys that are common to o1 and o2 their slices share at least one common element or both slices are empty

Types

type Option

type Option map[string][]string

Option is the result of Parse-ing an option string. An option string is of the form: `abc: value; y :value0 value1 ; ' z ' : '1st value for z' " 2nd value for z " 3rd; hidden;` i.e. a list of key:value value ...; or key; fragments, where key and value can be anything except "; : tab space cr lf" or anything quoted with single (') or double (") quoutes. If a key appears without a value in the list, it's single value is assumed to be "true". Whitespace is trimmed in the first form but not in the quoted form. There are no quotes of the same form allowed in the quoted form and there is no escaping. In the example above abc is [value], y is [value0 value1], " z " is the list of this three strings: "1st value for z" " 2nd value for z " "3rd", note the trimmed and not trimmed key/values. hidden is [true], so IsSet("hidden") returns true.

func Parse

func Parse(s string) (Option, error)

Parse parses a new Option map. Please note that "last occurrance wins", i.e. if a key is given more than once in s, the value list of the last occurrance is considered to be *the* value of the key. E.g. "a: a; a: 1 2 3;" returns an Option with a=[1 2 3]

func (Option) GetValue

func (o Option) GetValue(k string) string

GetValue looks up a particular option value. If doesn't exist or it has length zero an empty string is returned. If it has length one, the first element is returned. If it has length two or more, the slice is Sprint-ed and returned. If you need the splice, just use standard map lookup.

func (Option) IsGiven added in v0.6.12

func (o Option) IsGiven(k string) bool

IsGiven returns true if the key is present in the parsed map. The method exists to prevent a possible misinterpretation of IsSet() which checks if an option is truthy.

func (Option) IsSet

func (o Option) IsSet(k string) bool

IsSet returns true if the list at k is exactly one element long and this single value is one of [true 1 on set yes]

func (Option) Quoted added in v0.6.12

func (o Option) Quoted() (string, error)

Quoted returns a normalized parseable string represention of the Option. Boolean true flags, see IsSet, reproduce their original string value and "true" in case it was left out. It returns ErrCantQuoteString and the offending key/value, if the Option contains a key or a value that both contains a single and a double quote, which is not allowed by the parser but can be constructed via the standard map functionality of Option.

func (Option) String

func (o Option) String() string

String returns a human readable representation of options, not their raw input. If you need a re-parseable string use Quoted. Keys are sorted as strings and listed in order. Lists are represented by unquoted, comma-separated strings. Especially boolean true values, see IsSet(), are shown in their key-only form.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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