cliparse

package
v0.1.151 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: GPL-2.0, GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package cliparse implements subflux's flat CLI grammar: a single ParseAndValidate pass that splits "--key value" / "--key=value" tokens, rejects unknown flags (with typo suggestions) and stray positionals, parses bool flags from the spec table, validates typed values, enforces required flags, and applies spec defaults. It also renders per-command and root help.

The design choice is deliberate: the CLI surface is auxiliary to subflux's HTTP/JSON API and web UI, so a heavyweight CLI framework (kong, cobra) would be over-engineered for ~15 flat subcommands. This package closes the user-visible gaps (missing --help, silent typos, silent type errors) in roughly one file.

Index

Constants

View Source
const (
	TypeString   = "string"
	TypeInt      = "int"
	TypeBool     = "bool"
	TypeDuration = "duration"
)

Flag type vocabulary for Flag.Type. An empty Type means TypeString.

Variables

This section is empty.

Functions

func HelpRequested

func HelpRequested(args []string) bool

HelpRequested returns true if args contains --help or -h. Call before invoking the parser so help short-circuits before validation runs.

func PrintHelp

func PrintHelp(w io.Writer, s *Spec)

PrintHelp writes formatted help text for s to w.

func PrintRootHelp

func PrintRootHelp(w io.Writer, specs []Spec)

PrintRootHelp writes a list of all subcommands to w. specs are listed in the order provided; the caller may sort or filter (e.g. drop hidden subcommands like "health").

func SuggestName

func SuggestName(input string, candidates []string) (string, bool)

SuggestName finds the closest match in candidates (by Levenshtein distance, max 2) to input. Returns the matched name and true; or empty string and false when nothing within distance 2.

Used by main.go to suggest "did you mean 'search'?" when an unknown subcommand is invoked.

Types

type Flag

type Flag struct {
	Name     string
	Help     string
	Type     string // TypeString (default), TypeInt, TypeBool, TypeDuration
	Default  string
	Required bool
}

Flag describes one CLI flag for a subcommand.

type Params added in v0.1.148

type Params struct {
	// contains filtered or unexported fields
}

Params is the validated result of one ParseAndValidate pass: typed access to flag values with spec defaults applied. Bool flags live in their own set; every other flag is stored in its string form (typed flags were already validated as parseable).

func ParseAndValidate added in v0.1.148

func ParseAndValidate(args []string, spec *Spec) (Params, error)

ParseAndValidate parses args (the argv slice after the subcommand name, e.g. os.Args[2:]) against spec in a single pass: "--key value" and "--key=value" forms are split (the "=" form on the FIRST "=", so values may themselves contain "="), unknown flags are rejected with a "did you mean" suggestion, stray positional tokens are rejected, bool flags are parsed from the spec table (bare "--flag", or "--flag=true|false"), typed values (int, duration) are validated, required flags are enforced, and spec defaults are applied for absent flags.

Preserved edge semantics (pinned by tests): a trailing non-bool flag without a value is treated as unset (a required flag then errors), a non-bool flag whose next token starts with "--" is a missing-value error (the "--name=--literal" form is the escape hatch for intentional flag-like values), a duplicated flag keeps the last value, and "--help" / "-h" / bare "--" / empty-name "--=v" tokens are skipped (help short-circuits in the dispatch preamble before this parse runs).

func (Params) Bool added in v0.1.148

func (p Params) Bool(name string) bool

Bool reports whether a bool-typed flag was set.

func (Params) Int added in v0.1.148

func (p Params) Int(name string) int

Int returns the value of an int-typed flag, or 0 when the flag is absent and its spec declares no default. Explicit values were validated during the parse, so a malformed value cannot reach this accessor.

func (Params) String added in v0.1.148

func (p Params) String(name string) string

String returns the value of a string-ish flag, or "" when the flag is absent and its spec declares no default.

type Spec

type Spec struct {
	// Run executes the subcommand with the parsed, validated params and
	// returns the process exit code. Receiving Params keeps parsing to
	// exactly one pass per invocation (a bare func() int would force
	// runners to reparse os.Args).
	Run      func(Params) int
	Name     string // e.g. "search"
	Synopsis string // one-line purpose, shown in root help
	Help     string // multi-line description for `<command> --help`
	Args     string // optional usage suffix, e.g. "<imdb-id>"
	Flags    []Flag
	// Hidden excludes the spec from root help. Internal vehicles (the
	// sync-worker child process) stay dispatchable without being
	// advertised as user commands.
	Hidden bool
}

Spec describes one subcommand: help metadata, the declared flag surface, and the runner dispatch invokes with the single parse's result.

func SortByName

func SortByName(specs []Spec) []Spec

SortByName returns specs sorted alphabetically by Name. Useful before passing to PrintRootHelp.

Jump to

Keyboard shortcuts

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