cliparse

package
v0.1.78 Latest Latest
Warning

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

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

Documentation

Overview

Package cliparse adds help generation, unknown-flag detection (with typo suggestions), and required/type validation on top of subflux's existing flag-parsing helpers. It does not replace the parser; it validates after the fact, so existing call sites remain backward compatible.

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 ~13 flat subcommands. This package closes the user-visible gaps (missing --help, silent typos, silent type errors) in roughly one file.

Index

Constants

This section is empty.

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 ParseArgs

func ParseArgs(args []string) (params map[string]string, download bool)

ParseArgs parses --key value pairs from the given argument slice. Returns the params map and whether --download was specified.

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.

func Validate

func Validate(args []string, params map[string]string, spec *Spec) error

Validate checks that args contains only flags declared in spec, that all required flags are present, and that typed flags (int, duration) parse cleanly. Returns nil on success; any error is human-readable and includes a "did you mean" suggestion for unknown flags whose edit distance to a known flag is at most 2.

args is the raw argv slice (after the subcommand name, e.g. os.Args[2:]); params is the result of clisearch.ParseArgs.

Types

type Flag

type Flag struct {
	Name     string
	Help     string
	Type     string // "string" (default), "int", "bool", "duration"
	Default  string
	Required bool
}

Flag describes one CLI flag for a subcommand.

type Spec

type Spec struct {
	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
}

Spec describes one subcommand. Used by PrintHelp and Validate.

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