cliflags

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package cliflags rewrites argv before cobra parses it, absorbing two common slips an LLM makes when generating a command line:

  • camelCase / snake_case flag names — `--userId` / `--user_name` instead of the canonical kebab-case `--user-id` / `--user-name`;
  • a flag stuck to its value — `--limit100` instead of `--limit 100`.

A rewrite happens ONLY when the result is a flag the command tree actually defines (and, for the sticky split, an integer-typed one), so unknown tokens pass through untouched and still surface cobra's normal "unknown flag" error. Every rewrite is reported as a Correction so the caller can echo it back — fixing the call and teaching the canonical form in one go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Correction

type Correction struct {
	Original  string `json:"original"`
	Corrected string `json:"corrected"`
	Kind      string `json:"kind"` // "flag-name" | "sticky-value"
}

Correction records one argv rewrite, for echoing back to the user/agent.

func Normalize

func Normalize(args []string, info FlagInfo) (out []string, corrections []Correction)

Normalize rewrites args (argv without the program name) per the package doc. It returns the rewritten args and the list of corrections applied. Tokens after the `--` end-of-flags marker, short flags, and unknown long flags are left untouched.

type FlagInfo

type FlagInfo struct {
	Known   map[string]bool
	Numeric map[string]bool
}

FlagInfo is the set of flags known to the command tree. Known holds every kebab-case flag name; Numeric is the subset whose value is integer/float typed (the only flags eligible for the sticky-value split).

func Collect

func Collect(root *cobra.Command) FlagInfo

Collect walks the whole command tree and records every flag name (and which are numeric). It is intentionally tree-wide rather than per-command: argv has not been parsed yet, so the target subcommand is not known, and a flag that exists somewhere is a safe normalization target.

Jump to

Keyboard shortcuts

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