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 ¶
- func HelpRequested(args []string) bool
- func ParseArgs(args []string) (params map[string]string, download bool)
- func PrintHelp(w io.Writer, s *Spec)
- func PrintRootHelp(w io.Writer, specs []Spec)
- func SuggestName(input string, candidates []string) (string, bool)
- func Validate(args []string, params map[string]string, spec *Spec) error
- type Flag
- type Spec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HelpRequested ¶
HelpRequested returns true if args contains --help or -h. Call before invoking the parser so help short-circuits before validation runs.
func ParseArgs ¶
ParseArgs parses --key value pairs from the given argument slice. Returns the params map and whether --download was specified.
func PrintRootHelp ¶
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 ¶
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 ¶
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 ¶
SortByName returns specs sorted alphabetically by Name. Useful before passing to PrintRootHelp.