mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-19 09:24:14 +00:00
* initial attempt
* fix bash
* fix zsh
* Add shell-completion docs page
Documents the new 'mlr completion {bash,zsh}' feature: the then-chain
context model, install instructions for bash and zsh (including the macOS
bash-3.2 'eval' caveat and zsh compinit self-init), and examples of
context-aware completion. Added to the nav under "Miller in more detail".
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* Add enum value completion for format and separator flags
Completes the argument value for arg-taking main flags whose values are a
known set: file-format names for -i/-o/--io, separator aliases for
--ifs/--ofs/--ips/etc., and regex-separator aliases for --ifs-regex/--ips-regex.
Other arg-taking flags continue to fall back to filename completion.
Candidate sets come from new cli getters (GetFileFormatNames,
GetSeparatorAliasNames, GetSeparatorRegexAliasNames) that read the same maps
Miller uses at runtime, so there is no separate list to keep in sync. The
command-line walk now records which flag a value position belongs to.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* Include format-conversion keystroke-savers in bare-dash completion
Reverts the suppression of --c2j/--x2y-style flags from 'mlr -<TAB>'. The full
set of main flags (297) is now offered, matching what is valid on the command
line. GetFlagNames no longer takes an includeSuppressed argument.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* Complete terminal subcommands and top-level help/version flags
'mlr <TAB>' now offers subcommand names (help, version, repl, regtest, script,
completion, terminal-list) alongside verb names, and 'mlr -<TAB>' offers the
top-level terminal flags (-h, --help, --version, --bare-version, and the help
shorthands -g/-l/-L/-f/-F/-k/-K). Subcommand names are offered only as the
first non-flag token, where they are valid.
To let the completion engine know these names without an import cycle
(pkg/terminals imports pkg/terminals/completion), the canonical terminal names
and version-flag spellings are factored into a new leaf package
pkg/terminals/registry, imported by pkg/terminals, pkg/climain, and completion.
The help-flag spellings come from a new help.GetTerminalFlagNames derived from
the existing shorthand table, so nothing drifts.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* Complete 'mlr help' topics and topic arguments
'mlr help <TAB>' now completes help topics (flags, verb, function, keyword,
list-verbs, ...), and topics that take a name argument complete it too:
'mlr help verb <TAB>' -> verb names, 'mlr help function <TAB>' -> function
names, 'mlr help keyword <TAB>' -> keyword names, 'mlr help flag <TAB>' ->
flag names. 'mlr completion <TAB>' completes bash/zsh.
A terminal subcommand consumes the rest of the command line, so the walk now
returns a ctxTerminalArgs context carrying the terminal name and the words
typed after it. New getters supply the candidate names without drift:
help.GetTopicNames, help.GetFunctionNames/GetKeywordNames (wrapping new
cst.BuiltinFunctionManager.GetBuiltinFunctionNames and cst.GetKeywordNames).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
* docs-neaten
* Move flag-value-candidate logic into pkg/cli; fix verb-flag collision
The mapping of which flags take a format/separator/regex-separator argument is
flag metadata, so it now lives with the flags in pkg/cli as
cli.FlagValueCandidates, alongside the existing GetFileFormatNames /
GetSeparatorAliasNames getters, replacing the maps that were in
pkg/terminals/completion/value_completion.go (now removed).
This also fixes a bug: value completion now applies only to main flags, not to
identically-spelled verb flags. Previously 'mlr uniq -o <TAB>' offered file
formats because uniq's -o (an output field name) collided with the main -o
format flag; it now correctly falls back to filename completion.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
159 lines
4.8 KiB
Go
159 lines
4.8 KiB
Go
// Per-verb flag discovery for shell completion.
|
|
//
|
|
// Miller's verbs do not expose a structured flag table the way main flags do;
|
|
// each verb hand-rolls its own CLI parser. Rather than maintain a parallel
|
|
// hand-written table that could drift from the parsers, we scrape each verb's
|
|
// usage text -- which is the same source of truth shown to users by
|
|
// `mlr <verb> --help` -- for its flag names and arity.
|
|
//
|
|
// The scrape relies on Miller's consistent usage-text convention:
|
|
//
|
|
// -f {comma-separated field names} ... (takes an argument: brace follows)
|
|
// -n ... (no argument)
|
|
// -x|--complement ... (alternate spellings, '|'-separated)
|
|
//
|
|
// A handful of verbs document a flag without the `{...}` convention (e.g.
|
|
// `put -s name=value`); those are corrected via verbFlagArityOverrides. Getting
|
|
// arity slightly wrong only affects the tolerant command-line walk in rare
|
|
// adversarial cases (a flag value that looks like `then` or a verb name); it
|
|
// never crashes and never produces a wrong command line.
|
|
|
|
package completion
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/johnkerl/miller/v6/pkg/transformers"
|
|
)
|
|
|
|
// verbFlagInfo is the scraped flag metadata for one verb.
|
|
type verbFlagInfo struct {
|
|
names []string // flag spellings, in usage-text order
|
|
takesArg map[string]bool // spelling -> whether it consumes an argument
|
|
}
|
|
|
|
// verbFlagArityOverrides corrects arity for flags whose usage text does not use
|
|
// the `-flag {arg}` convention. Keyed by verb, then by flag spelling, with the
|
|
// value being whether the flag takes an argument.
|
|
var verbFlagArityOverrides = map[string]map[string]bool{
|
|
// `put`/`filter` document `-s name=value` and `-e {expression}` etc.; the
|
|
// `-s` form has no braces, so the scraper would under-detect its arity.
|
|
"put": {"-s": true},
|
|
"filter": {"-s": true},
|
|
}
|
|
|
|
// verbFlagCache memoizes scrapes within a single process invocation.
|
|
var verbFlagCache = map[string]*verbFlagInfo{}
|
|
|
|
// verbFlagNames returns the flag spellings to offer as completion candidates
|
|
// for the given verb.
|
|
func verbFlagNames(verb string) []string {
|
|
return getVerbFlagInfo(verb).names
|
|
}
|
|
|
|
// verbFlagTakesArg reports whether `flag` is a known flag of `verb` and whether
|
|
// it consumes a following argument value.
|
|
func verbFlagTakesArg(verb string, flag string) (found bool, takesArg bool) {
|
|
info := getVerbFlagInfo(verb)
|
|
ta, ok := info.takesArg[flag]
|
|
return ok, ta
|
|
}
|
|
|
|
func getVerbFlagInfo(verb string) *verbFlagInfo {
|
|
if info, ok := verbFlagCache[verb]; ok {
|
|
return info
|
|
}
|
|
info := scrapeVerbFlagInfo(verb)
|
|
verbFlagCache[verb] = info
|
|
return info
|
|
}
|
|
|
|
func scrapeVerbFlagInfo(verb string) *verbFlagInfo {
|
|
info := &verbFlagInfo{
|
|
names: []string{},
|
|
takesArg: map[string]bool{},
|
|
}
|
|
|
|
usage, ok := captureVerbUsage(verb)
|
|
if ok {
|
|
parseUsageFlags(usage, info)
|
|
}
|
|
|
|
// Apply arity overrides, and ensure overridden flags are offered as
|
|
// candidates even if scraping missed them.
|
|
if overrides, ok := verbFlagArityOverrides[verb]; ok {
|
|
for flag, takesArg := range overrides {
|
|
if _, seen := info.takesArg[flag]; !seen {
|
|
info.names = append(info.names, flag)
|
|
}
|
|
info.takesArg[flag] = takesArg
|
|
}
|
|
}
|
|
|
|
return info
|
|
}
|
|
|
|
// parseUsageFlags extracts flag spellings and arity from verb usage text.
|
|
func parseUsageFlags(usage string, info *verbFlagInfo) {
|
|
for _, line := range strings.Split(usage, "\n") {
|
|
trimmed := strings.TrimLeft(line, " \t")
|
|
if !strings.HasPrefix(trimmed, "-") {
|
|
continue
|
|
}
|
|
fields := strings.Fields(trimmed)
|
|
if len(fields) == 0 {
|
|
continue
|
|
}
|
|
// A following `{...}` token signals an argument-taking flag.
|
|
takesArg := len(fields) >= 2 && strings.HasPrefix(fields[1], "{")
|
|
|
|
// The leading token may bundle alternate spellings, e.g. `-h|--help`
|
|
// or `-x|--complement` or `-tr|-rt`.
|
|
for _, name := range strings.Split(fields[0], "|") {
|
|
name = strings.TrimRight(name, ":,")
|
|
if name == "-" || name == "--" || !strings.HasPrefix(name, "-") {
|
|
continue
|
|
}
|
|
if _, seen := info.takesArg[name]; !seen {
|
|
info.names = append(info.names, name)
|
|
}
|
|
// If any spelling on the line takes an argument, all do.
|
|
if takesArg || info.takesArg[name] {
|
|
info.takesArg[name] = true
|
|
} else {
|
|
info.takesArg[name] = false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// captureVerbUsage runs a verb's UsageFunc and returns its text. UsageFunc
|
|
// writes to an *os.File, so we capture it through an os.Pipe.
|
|
func captureVerbUsage(verb string) (string, bool) {
|
|
setup := transformers.LookUp(verb)
|
|
if setup == nil || setup.UsageFunc == nil {
|
|
return "", false
|
|
}
|
|
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
|
|
done := make(chan string, 1)
|
|
go func() {
|
|
var buf bytes.Buffer
|
|
_, _ = io.Copy(&buf, r)
|
|
done <- buf.String()
|
|
}()
|
|
|
|
setup.UsageFunc(w)
|
|
_ = w.Close()
|
|
text := <-done
|
|
_ = r.Close()
|
|
|
|
return text, true
|
|
}
|