paramutil

package
v0.9.10 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package paramutil parses, renders and merges key=value params in several notations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Query: a=b&c=d after an optional "?"; unquoted, as the web percent-encodes instead.
	Query = NewParams("=", "&", WithOpener("?"))
	// Keyword: a=b c='d e' (libpq connection string).
	Keyword = NewParams("=", " ", WithQuote("'"))
	// Flags: --a=b -c="d e", keeping bare words (command, positionals, valueless flags) raw.
	Flags = NewParams("=", " ", WithKeyPrefix("--", "-"), WithQuote(`"`, "'"), AllowRaw())
	Comma = NewParams("=", ",", WithQuote(`"`, "'"))
	// Env: NUL-joined KEY=VALUE, any name, quotes are text; "=C:"-style Windows entries stay raw.
	Env = NewParams("=", "\x00", WithBadKeyChars(""))
)
View Source
var Argv = NewArgs("=", []string{"--", "-"})

Functions

This section is empty.

Types

type Arg

type Arg struct {
	Inline bool
	Values []string
}

Arg is a flag's values; Inline means they followed the equal sign rather than separate tokens.

type Args

type Args interface {
	Parse(tokens []string) Entries[Arg]
	// ParseInto merges into existing entries (must not be nil), left to right; an open flag
	// does not carry over between calls.
	ParseInto(entries Entries[Arg], tokens []string)
	Render(entries Entries[Arg]) []string
}

Args is the command-line notation, where a flag may take values from the following tokens. A non-flag token is a value of the open flag, else a positional; a repeated flag keeps its first position and its values are replaced, not appended.

func NewArgs

func NewArgs(equal string, prefixes []string) Args

type Entries

type Entries[V any] interface {
	// Set overwrites in place or appends; "" is a valid key.
	Set(key string, value V)
	// AddRaw appends unkeyed text, invisible to Get.
	AddRaw(raw string)
	Get(key string) (V, bool)
	// Len counts keyed and raw params alike.
	Len() int
	// List returns a fresh shallow copy, in order.
	List() []Param[V]
}

Entries is an ordered set of params: a key keeps its first position and its last value; raw entries are never merged. Not safe for concurrent use.

func NewEntries

func NewEntries[V any]() Entries[V]

type Param

type Param[V any] struct {
	Key   string
	Value V
	Raw   string
	Keyed bool
}

Param carries Key and Value when Keyed, Raw otherwise.

type Params

type Params interface {
	// Parse splits s into prefix and section; the joining opener/separator stays on the prefix
	// so Render round-trips. A string with no opener is read as a section ("a=b&c=d").
	Parse(s string) (string, Entries[string])
	// ParseTokens reads one pair per token; non-pairs are kept raw regardless of AllowRaw, and a
	// separator inside a token is plain text.
	ParseTokens(tokens []string) Entries[string]
	// ParseTokensInto merges into existing entries (must not be nil), left to right.
	ParseTokensInto(entries Entries[string], tokens []string)
	// Render appends the opener unless prefix is empty or already ends with opener or separator;
	// keys and raw entries are written unvalidated.
	Render(prefix string, entries Entries[string]) string
	// RenderTokens writes one token per entry, quoting only values a reader would unquote.
	RenderTokens(entries Entries[string]) []string
	// Apply overwrites existing keys in place and appends new ones in sorted order; a bare name
	// takes the notation's first key prefix.
	Apply(s string, params map[string]string) string
}

Params is a key=value&key=value notation (equal, separator, opener, key prefixes, quotes). Values are quoted only when they would not read back as themselves; there is no escape, so a value holding every quote cannot round-trip. A name (key minus prefix) must be non-empty and free of the opener and bad chars ("/@:" by default), or the token is not a pair: it stays in the prefix, or is kept raw under AllowRaw. --a and -a are distinct keys.

func NewParams

func NewParams(equal, sep string, opts ...ParamsOption) Params

NewParams: without WithOpener, sep also divides the section from any preceding text.

type ParamsOption

type ParamsOption func(*params)

func AllowRaw

func AllowRaw() ParamsOption

AllowRaw keeps non-pair text in place instead of ending the section at it.

func WithBadKeyChars

func WithBadKeyChars(chars string) ParamsOption

WithBadKeyChars replaces the default "/@:" (which stops URIs reading as pairs); "" allows any name.

func WithKeyPrefix

func WithKeyPrefix(prefixes ...string) ParamsOption

WithKeyPrefix makes a prefix mandatory for a pair; a bare name given to Apply takes the first.

func WithOpener

func WithOpener(open string) ParamsOption

WithOpener sets the section opener (e.g. "?"); without it the whole string is a section.

func WithQuote

func WithQuote(quotes ...string) ParamsOption

WithQuote sets value quotes, recognized only at a value's start; without quotes an empty value or one holding the separator cannot round-trip.

Jump to

Keyboard shortcuts

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