cliparser

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package cliparser parses argv into a map of stable config keys using precomputed flag definitions. Code generators build Def values at generate time; runtime parsing does not use reflection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigKeyPath

func ConfigKeyPath(prefix, key string) string

ConfigKeyPath builds the stable config key "prefix.key".

func DefaultLongName

func DefaultLongName(prefix, key string) string

DefaultLongName builds the default long flag name without leading dashes: "{prefix}-{key}", with '.' in both prefix and key replaced by '-'.

Types

type Def

type Def struct {
	// ConfigKey is the stable overlay key (e.g. "webserver.port").
	ConfigKey string
	// Longs are long flag names without leading dashes (e.g. "webserver-port" or "port").
	Longs []string
	// Shorts are optional single-letter short names without leading dashes (e.g. "p").
	Shorts []string
	// Help is optional usage text.
	Help string
	// Env is an optional exact environment variable override. Empty derives the
	// name from the first long option; "-" disables environment loading.
	Env string
	// Kind selects value parsing behavior.
	Kind Kind
	// UsesOptOverride is true when names came from an opt-style override.
	UsesOptOverride bool
}

Def is one CLI flag definition mapping long/short names to a stable config key. Aligns with data:cli-flag-def / decision:cli-flag-naming.

func BuildDefs

func BuildDefs(fields []FieldMeta) ([]Def, error)

BuildDefs builds flag definitions for a slice of field metadata (codegen helper entry point).

func DefFromField

func DefFromField(m FieldMeta) (Def, error)

DefFromField builds a Def from FieldMeta using decision:cli-flag-naming rules.

type FieldMeta

type FieldMeta struct {
	// Prefix is the Bind prefix / table name (e.g. "webserver").
	Prefix string
	// Key is the relative field key under the prefix (e.g. "port" or "tls.enabled").
	Key string
	// Opt is an optional override of the form "long" or "long,short" (no dashes).
	// When set, only those names are registered; default --prefix-key is suppressed.
	Opt string
	// Help is optional CLI help text.
	Help string
	// Env is an optional exact environment variable override, or "-" to disable
	// environment loading for this field.
	Env string
	// Kind defaults to KindString when zero value is left as KindString.
	Kind Kind
}

FieldMeta is codegen-facing field metadata used to build Def values without runtime tags.

type Kind

type Kind int

Kind classifies how a flag consumes argv values.

const (
	// KindString is a single string value flag (--flag value or --flag=value).
	KindString Kind = iota
	// KindBool is a boolean flag (--flag, --flag=true|false).
	KindBool
	// KindArray accumulates repeated flag occurrences into a multi-value list.
	KindArray
)

type Result

type Result struct {
	// Values maps stable config keys to a single string (last write wins for KindString/KindBool).
	Values map[string]string
	// Multi maps stable config keys to accumulated values for KindArray flags.
	Multi map[string][]string
	// Rest is non-flag arguments after flags (or after "--").
	Rest []string
}

Result is the outcome of parsing argv against flag definitions.

func Parse

func Parse(args []string, defs []Def) (Result, error)

Parse matches argv against defs and returns stable config key → raw values. Only flags that appear in argv are present in Values/Multi; missing flags are absent. Runtime uses only the provided Def list—no reflection.

func ParseInterspersed added in v0.1.12

func ParseInterspersed(args []string, defs []Def) (Result, error)

ParseInterspersed parses flags anywhere before "--" and collects every non-flag token in Rest. It is intended for subcommands, whose options may appear before or after positional arguments.

Directories

Path Synopsis
Package codegen emits Go source that constructs cliparser.Def slices for Bind-style fields.
Package codegen emits Go source that constructs cliparser.Def slices for Bind-style fields.

Jump to

Keyboard shortcuts

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