mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-18 00:45:47 +00:00
* Tier-2 structured verb options: OptionSpec, initial migration (#2098) PR 3 of the AI-friendly roadmap (plans/plan-2098-llm.md). Infrastructure: - Add OptionSpec{Flag,Arg,Type,Desc,Repeatable,Values} to pkg/transformers/aaa_record_transformer.go alongside TransformerSetup. Type is one of: bool, string, int, float, csv-list, regex, filename, format, enum. For type=="enum", Values lists the valid choices. - Add Options []OptionSpec to TransformerSetup (nil = not yet migrated). - Emit Options in VerbInfoForJSON (omitempty so unmigrated verbs stay backward-compatible; agents check key presence for Tier-2 availability). UsageText is always present as the Tier-1 prose fallback. - Add VerbOptionsNilCheck() in aaa_verb_options_check.go: progress report of migrated vs. unmigrated verbs, analogous to FLAG_TABLE.NilCheck(). - Wire verb-options-nil-check into mlr help (internal/docgen section). Initial migration (5/70 verbs): - nothing: empty Options (no verb-specific options, explicitly migrated) - cat: -n (bool), -N (string), -g (csv-list), --filename, --filenum (bool) - head: -g (csv-list), -n (int) - tail: -g (csv-list), -n (int) - tee: -a, -p (bool) Tests: - 5 new unit tests in aaa_transformer_json_test.go covering migrated/ unmigrated paths, field population, JSON round-trip, and key-presence. - Regression test case 0003: mlr help verb-options-nil-check golden output. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * Migrate all 70 verbs to structured OptionSpec; bump catalog schema to v2 Completes the Tier-2 migration started in the previous commit. Every verb in TRANSFORMER_LOOKUP_TABLE now has a non-nil Options field. - Workflow-migrated all 65 remaining verbs. Each Setup var now carries Options: []OptionSpec{...} with Flag/Arg/Type/Desc fields. Verbs with no verb-specific options (altkv, check, group-like, nothing, etc.) use an empty slice to signal "migrated but no options." - Drop `omitempty` from VerbInfoForJSON.Options: empty slices were silently dropped, making migrated-no-option verbs indistinguishable from unmigrated ones in JSON. Without omitempty: null=unmigrated, []=migrated-no-options, [...]= migrated-with-options. Bump catalogSchemaVersion 1→2 for this shape change. - Replace the two "unmigrated-verb" unit tests (which used stats1 as an example) with TestAllVerbsFullyMigrated (asserts every verb has non-nil Options) and TestAllVerbsHaveOptionsKeyInJSON (asserts every migrated verb emits the "options" key in JSON). - Regenerate test/cases/cli-help/0003/expout: now reads "Verb options migration: 70/70 migrated." Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com> * remove a transitional helper * git rms * Render verb usage Options blocks from structured OptionSpec Each verb's usage message and its Tier-2 OptionSpec list previously duplicated the option text. New WriteVerbOptions (aaa_verb_usage.go) renders the "Options:" block from the specs: aligned flag column, descriptions word-wrapped at 80, uniform trailing -h|--help line. - OptionSpec gains Aliases (JSON "aliases") so long-form spellings like join's --lk|--left-keep-field-names survive in both outputs - All 70 verbs migrated; options literals hoisted to package-level vars (usage funcs can't reference their Setup var without a Go init cycle) - Hand-written per-option details the specs had condensed away are merged into Desc, enriching the JSON catalog - Non-option prose (examples, cross-references, dynamic accumulator listings) kept verbatim - Regenerated the six usage-embedding regression expectations and the two affected doc pages Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix pre-existing usage-text bugs surfaced by the OptionSpec migration - gap: usage said "One of -f or -g is required" but the parser takes -n or -g - seqgen: drop description line copy-pasted from cat ("Passes input records directly to output...") which contradicted "Discards the input record stream" - utf8-to-latin1: description read inverted ("from Latin-1 to UTF-8") - sec2gmtdate: usage said "../c/mlr" instead of "mlr" - top: document the accepted-but-undocumented --max flag - stats2: add linreg-pca to the -a enum values, matching the runtime accumulator table Regression expectations and docs regenerated accordingly. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Fix check usage sentence order; stats1 usage blank line to usage stream - check: the description's second and third lines were swapped, reading "Consumes records without printing any output, / Useful for doing a well-formatted check on input data. / with the exception that warnings are printed to stderr." - stats1: a bare fmt.Println() in the usage func wrote its blank line to process stdout instead of the usage output stream Regression expectation and docs regenerated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
263 lines
8.5 KiB
Go
263 lines
8.5 KiB
Go
// Machine-readable (JSON) help, for `mlr help --as-json` and friends.
|
|
//
|
|
// This assembles the structured catalogs exposed by the verb, function, flag,
|
|
// and keyword registries into a single document, so AI agents and other tooling
|
|
// can model Miller's surface without scraping the human-readable prose. The
|
|
// plain (non-`--as-json`) help behavior is unchanged; `--as-json` only switches
|
|
// the rendering.
|
|
//
|
|
// Two equivalent ways to opt in:
|
|
// - Per-call flag `--as-json` anywhere on a `mlr help ...` command line.
|
|
// - Env var MLR_HELP_JSON set to a truthy value (1, true, yes).
|
|
|
|
package help
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/johnkerl/miller/v6/pkg/cli"
|
|
"github.com/johnkerl/miller/v6/pkg/dsl/cst"
|
|
"github.com/johnkerl/miller/v6/pkg/transformers"
|
|
"github.com/johnkerl/miller/v6/pkg/version"
|
|
)
|
|
|
|
// catalogSchemaVersion is bumped whenever the shape of the JSON catalog
|
|
// changes. Agents and tools can use this (together with mlr_version) as a
|
|
// cache key: re-fetch only when either value changes.
|
|
const catalogSchemaVersion = 2
|
|
|
|
// CatalogForJSON is the top-level document emitted by `mlr help --as-json`
|
|
// with no further topic: the entire help catalog in one machine-readable
|
|
// object.
|
|
type CatalogForJSON struct {
|
|
MlrVersion string `json:"mlr_version"`
|
|
CatalogSchemaVersion int `json:"catalog_schema_version"`
|
|
Verbs []*transformers.VerbInfoForJSON `json:"verbs"`
|
|
Functions []*cst.FunctionInfoForJSON `json:"functions"`
|
|
Flags []*cli.FlagInfoForJSON `json:"flags"`
|
|
Keywords []*cst.KeywordInfoForJSON `json:"keywords"`
|
|
}
|
|
|
|
// wantJSONOutput returns true when the caller has opted in to JSON output via
|
|
// either the --as-json flag or a truthy MLR_HELP_JSON env var.
|
|
func wantJSONOutput(args []string) (bool, []string) {
|
|
if isTruthyEnv(os.Getenv("MLR_HELP_JSON")) {
|
|
// Env var wins; still strip any --as-json tokens so dispatch is clean.
|
|
_, rest := extractAsJSONFlag(args)
|
|
return true, rest
|
|
}
|
|
return extractAsJSONFlag(args)
|
|
}
|
|
|
|
// isTruthyEnv returns true for non-empty strings commonly used as boolean
|
|
// env-var truthy values: "1", "true", "yes" (case-insensitive).
|
|
func isTruthyEnv(v string) bool {
|
|
switch v {
|
|
case "1", "true", "True", "TRUE", "yes", "Yes", "YES":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// extractFlag removes every occurrence of flag from args, returning whether
|
|
// one was present along with the remaining args. The flag may appear anywhere
|
|
// on the command line.
|
|
func extractFlag(args []string, flag string) (bool, []string) {
|
|
found := false
|
|
kept := make([]string, 0, len(args))
|
|
for _, arg := range args {
|
|
if arg == flag {
|
|
found = true
|
|
} else {
|
|
kept = append(kept, arg)
|
|
}
|
|
}
|
|
return found, kept
|
|
}
|
|
|
|
// extractAsJSONFlag removes any "--as-json" token from args.
|
|
func extractAsJSONFlag(args []string) (bool, []string) { return extractFlag(args, "--as-json") }
|
|
|
|
// extractIndexFlag removes any "--index" token from args.
|
|
func extractIndexFlag(args []string) (bool, []string) { return extractFlag(args, "--index") }
|
|
|
|
// printAsJSON marshals v as indented JSON to stdout. Returns a process exit
|
|
// code.
|
|
func printAsJSON(v any) int {
|
|
bytes, err := json.MarshalIndent(v, "", " ")
|
|
if err != nil {
|
|
fmt.Printf("mlr help: could not render JSON: %v\n", err)
|
|
return 1
|
|
}
|
|
fmt.Println(string(bytes))
|
|
return 0
|
|
}
|
|
|
|
// IndexEntryForJSON is one entry in the lightweight capability index emitted
|
|
// by `mlr help --as-json --index`. It carries only the name and a one-line
|
|
// summary -- no bodies, examples, or usage_text -- so an agent can quickly
|
|
// scan the full surface before drilling into individual entries.
|
|
type IndexEntryForJSON struct {
|
|
Kind string `json:"kind"`
|
|
Name string `json:"name"`
|
|
Summary string `json:"summary"`
|
|
}
|
|
|
|
// buildIndex assembles the lightweight index over all four catalogs. Entries
|
|
// are sorted by kind (verb, function, flag, keyword) then by name within each
|
|
// kind, giving a deterministic, diffable output.
|
|
func buildIndex() []IndexEntryForJSON {
|
|
entries := make([]IndexEntryForJSON, 0)
|
|
|
|
for _, v := range transformers.GetVerbInfosForJSON() {
|
|
entries = append(entries, IndexEntryForJSON{Kind: "verb", Name: v.Name, Summary: v.Summary})
|
|
}
|
|
for _, f := range cst.BuiltinFunctionManagerInstance.GetFunctionInfosForJSON() {
|
|
entries = append(entries, IndexEntryForJSON{Kind: "function", Name: f.Name, Summary: firstLine(f.Help)})
|
|
}
|
|
for _, fl := range cli.FLAG_TABLE.GetFlagInfosForJSON() {
|
|
entries = append(entries, IndexEntryForJSON{Kind: "flag", Name: fl.Name, Summary: firstLine(fl.Help)})
|
|
}
|
|
for _, kw := range cst.GetKeywordInfosForJSON() {
|
|
entries = append(entries, IndexEntryForJSON{Kind: "keyword", Name: kw.Name, Summary: firstLine(kw.Help)})
|
|
}
|
|
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
ri, rj := kindRank(entries[i].Kind), kindRank(entries[j].Kind)
|
|
if ri != rj {
|
|
return ri < rj
|
|
}
|
|
return entries[i].Name < entries[j].Name
|
|
})
|
|
|
|
return entries
|
|
}
|
|
|
|
// firstLine returns the first non-empty line of s, suitable as a one-liner
|
|
// summary in the index and which output.
|
|
func firstLine(s string) string {
|
|
for _, line := range strings.Split(s, "\n") {
|
|
if t := strings.TrimSpace(line); t != "" {
|
|
return t
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
// kindRank returns the display rank for a catalog kind. Kinds are ordered
|
|
// verb < function < flag < keyword; anything else sorts last. Used by both
|
|
// buildIndex and whichSearch so the two sort orders stay in sync.
|
|
func kindRank(kind string) int {
|
|
switch kind {
|
|
case "verb":
|
|
return 0
|
|
case "function":
|
|
return 1
|
|
case "flag":
|
|
return 2
|
|
case "keyword":
|
|
return 3
|
|
}
|
|
return 4
|
|
}
|
|
|
|
// helpJSON dispatches `mlr help --as-json [--index] [topic [names...]]`. With
|
|
// no topic it emits the full catalog; with --index it emits the lightweight
|
|
// summary index; with a topic (verb/function/flag/keyword) it emits just those
|
|
// entries -- all of them if no names are given, or the named ones.
|
|
func helpJSON(args []string) int {
|
|
wantIndex, args := extractIndexFlag(args)
|
|
if wantIndex {
|
|
return printAsJSON(buildIndex())
|
|
}
|
|
|
|
if len(args) == 0 {
|
|
return printAsJSON(buildFullCatalog())
|
|
}
|
|
|
|
topic := args[0]
|
|
names := args[1:]
|
|
|
|
switch topic {
|
|
case "verb", "verbs":
|
|
return printAsJSON(collectVerbs(names))
|
|
case "function", "functions":
|
|
return printAsJSON(collectFunctions(names))
|
|
case "flag", "flags":
|
|
return printAsJSON(collectFlags(names))
|
|
case "keyword", "keywords":
|
|
return printAsJSON(collectKeywords(names))
|
|
default:
|
|
fmt.Printf("mlr help --as-json: unsupported topic \"%s\".\n", topic)
|
|
fmt.Printf("Supported: (no topic) for the full catalog, or one of: verb, function, flag, keyword.\n")
|
|
fmt.Printf("With --index: lightweight name+summary list across all catalog items.\n")
|
|
return 1
|
|
}
|
|
}
|
|
|
|
func buildFullCatalog() *CatalogForJSON {
|
|
return &CatalogForJSON{
|
|
MlrVersion: version.STRING,
|
|
CatalogSchemaVersion: catalogSchemaVersion,
|
|
Verbs: transformers.GetVerbInfosForJSON(),
|
|
Functions: cst.BuiltinFunctionManagerInstance.GetFunctionInfosForJSON(),
|
|
Flags: cli.FLAG_TABLE.GetFlagInfosForJSON(),
|
|
Keywords: cst.GetKeywordInfosForJSON(),
|
|
}
|
|
}
|
|
|
|
func collectVerbs(names []string) []*transformers.VerbInfoForJSON {
|
|
if len(names) == 0 {
|
|
return transformers.GetVerbInfosForJSON()
|
|
}
|
|
infos := make([]*transformers.VerbInfoForJSON, 0, len(names))
|
|
for _, name := range names {
|
|
if info := transformers.GetVerbInfoForJSON(name); info != nil {
|
|
infos = append(infos, info)
|
|
}
|
|
}
|
|
return infos
|
|
}
|
|
|
|
func collectFunctions(names []string) []*cst.FunctionInfoForJSON {
|
|
if len(names) == 0 {
|
|
return cst.BuiltinFunctionManagerInstance.GetFunctionInfosForJSON()
|
|
}
|
|
infos := make([]*cst.FunctionInfoForJSON, 0, len(names))
|
|
for _, name := range names {
|
|
if info := cst.BuiltinFunctionManagerInstance.GetFunctionInfoForJSON(name); info != nil {
|
|
infos = append(infos, info)
|
|
}
|
|
}
|
|
return infos
|
|
}
|
|
|
|
func collectFlags(names []string) []*cli.FlagInfoForJSON {
|
|
if len(names) == 0 {
|
|
return cli.FLAG_TABLE.GetFlagInfosForJSON()
|
|
}
|
|
infos := make([]*cli.FlagInfoForJSON, 0, len(names))
|
|
for _, name := range names {
|
|
if info := cli.FLAG_TABLE.GetFlagInfoForJSON(name); info != nil {
|
|
infos = append(infos, info)
|
|
}
|
|
}
|
|
return infos
|
|
}
|
|
|
|
func collectKeywords(names []string) []*cst.KeywordInfoForJSON {
|
|
if len(names) == 0 {
|
|
return cst.GetKeywordInfosForJSON()
|
|
}
|
|
infos := make([]*cst.KeywordInfoForJSON, 0, len(names))
|
|
for _, name := range names {
|
|
if info := cst.GetKeywordInfoForJSON(name); info != nil {
|
|
infos = append(infos, info)
|
|
}
|
|
}
|
|
return infos
|
|
}
|