configcmd.OneOf for alternative fields; completion.Files for paths
UndocumentedKeys reported the unused alternatives of a struct like SecretRef (env/file/value) as undocumented; a struct implementing the marker configcmd.OneOf now counts as documented with one alternative, and upgrade offers none of the others to a file that chose one. completion.Tree gets the key completion.Files: that level completes file and directory paths besides command names (bash, zsh, fish, PowerShell), for programs started as "<program> [path]" like yamc. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
83f4228e66
commit
131d82642c
6 files changed
+244
-13
No files matched your search
@@ -5,7 +5,12 @@
|
||||
//
|
||||
// The scripts are deliberately simple, word-based completion of command
|
||||
// and subcommand names (not flag-aware): they join the words typed so far
|
||||
// into a prefix and look up what may follow it.
|
||||
// into a prefix and look up what may follow it. A level that also takes a
|
||||
// file or directory path (a program without subcommands that is started
|
||||
// as "<program> [path]") adds the Files key; the shell then completes
|
||||
// paths there too:
|
||||
//
|
||||
// var tree = completion.Tree{completion.Files: nil, "manual": nil, ...}
|
||||
//
|
||||
// var tree = completion.Tree{
|
||||
// "serve": nil,
|
||||
@@ -41,6 +46,10 @@ func Leaf(names ...string) Tree {
|
||||
return t
|
||||
}
|
||||
|
||||
// Files is a Tree key, not a command: the level it is in also completes
|
||||
// file and directory paths. Its value is ignored.
|
||||
const Files = "<files>"
|
||||
|
||||
// Shells are the supported shells, in the order help texts list them.
|
||||
var Shells = []string{"bash", "zsh", "fish", "powershell"}
|
||||
|
||||
@@ -53,6 +62,7 @@ var safeWord = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:+-]*$`)
|
||||
type entry struct {
|
||||
prefix string
|
||||
words []string
|
||||
files bool // paths complete here too (Files)
|
||||
}
|
||||
|
||||
func flatten(tree Tree) ([]entry, error) {
|
||||
@@ -60,14 +70,19 @@ func flatten(tree Tree) ([]entry, error) {
|
||||
var walk func(prefix string, t Tree) error
|
||||
walk = func(prefix string, t Tree) error {
|
||||
names := make([]string, 0, len(t))
|
||||
files := false
|
||||
for n := range t {
|
||||
if n == Files {
|
||||
files = true
|
||||
continue
|
||||
}
|
||||
if !safeWord.MatchString(n) {
|
||||
return fmt.Errorf("completion: command name %q is not a plain word", n)
|
||||
}
|
||||
names = append(names, n)
|
||||
}
|
||||
sort.Strings(names)
|
||||
out = append(out, entry{prefix, names})
|
||||
out = append(out, entry{prefix, names, files})
|
||||
for _, n := range names {
|
||||
if sub := t[n]; len(sub) > 0 {
|
||||
p := n
|
||||
@@ -84,6 +99,15 @@ func flatten(tree Tree) ([]entry, error) {
|
||||
return out, walk("", tree)
|
||||
}
|
||||
|
||||
func anyFiles(entries []entry) bool {
|
||||
for _, e := range entries {
|
||||
if e.files {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Script returns the completion script for shell.
|
||||
func Script(program, shell string, tree Tree) (string, error) {
|
||||
if !safeWord.MatchString(program) {
|
||||
@@ -100,16 +124,30 @@ func Script(program, shell string, tree Tree) (string, error) {
|
||||
fmt.Fprintf(&b, "# %s bash completion. Install with:\n# eval \"$(%s completion bash)\" (bash 3.2 on macOS cannot source <(...))\n", program, program)
|
||||
fmt.Fprintf(&b, "%s() {\n local cur prefix\n cur=\"${COMP_WORDS[COMP_CWORD]}\"\n prefix=\"${COMP_WORDS[*]:1:COMP_CWORD-1}\"\n case \"$prefix\" in\n", fn)
|
||||
for _, e := range entries {
|
||||
fmt.Fprintf(&b, " %q) COMPREPLY=($(compgen -W %q -- \"$cur\")); return ;;\n", e.prefix, strings.Join(e.words, " "))
|
||||
fmt.Fprintf(&b, " %q) COMPREPLY=($(compgen -W %q -- \"$cur\"))", e.prefix, strings.Join(e.words, " "))
|
||||
if e.files {
|
||||
// One path per line, so names with spaces stay whole;
|
||||
// compopt (bash 4+) adds the trailing "/" and quoting.
|
||||
b.WriteString("\n local IFS=$'\\n'; COMPREPLY+=($(compgen -f -- \"$cur\")); compopt -o filenames 2>/dev/null")
|
||||
}
|
||||
b.WriteString("; return ;;\n")
|
||||
}
|
||||
fmt.Fprintf(&b, " *) COMPREPLY=() ;;\n esac\n}\ncomplete -F %s %s\n", fn, program)
|
||||
case "zsh":
|
||||
fmt.Fprintf(&b, "#compdef %s\n# %s zsh completion. Install with:\n# source <(%s completion zsh)\n", program, program, program)
|
||||
fmt.Fprintf(&b, "%s() {\n local prefix=\"${words[2,CURRENT-1]}\"\n local -a completions\n case \"$prefix\" in\n", fn)
|
||||
fmt.Fprintf(&b, "%s() {\n local prefix=\"${words[2,CURRENT-1]}\" files=0\n local -a completions\n case \"$prefix\" in\n", fn)
|
||||
for _, e := range entries {
|
||||
fmt.Fprintf(&b, " %q) completions=(%s) ;;\n", e.prefix, strings.Join(e.words, " "))
|
||||
f := ""
|
||||
if e.files {
|
||||
f = "; files=1"
|
||||
}
|
||||
fmt.Fprintf(&b, " %q) completions=(%s)%s ;;\n", e.prefix, strings.Join(e.words, " "), f)
|
||||
}
|
||||
fmt.Fprintf(&b, " *) completions=() ;;\n esac\n _describe 'command' completions\n}\n")
|
||||
fmt.Fprintf(&b, " *) completions=() ;;\n esac\n _describe 'command' completions\n")
|
||||
if anyFiles(entries) {
|
||||
b.WriteString(" (( files )) && _files\n")
|
||||
}
|
||||
b.WriteString("}\n")
|
||||
fmt.Fprintf(&b, "if (( $+functions[compdef] )); then\n compdef %s %s\nelse\n echo '%s: zsh completion needs \"autoload -U compinit && compinit\" run first (e.g. in ~/.zshrc)' >&2\nfi\n", fn, program, program)
|
||||
case "fish":
|
||||
fmt.Fprintf(&b, "# %s fish completion. Install with:\n# %s completion fish | source\n", program, program)
|
||||
@@ -125,6 +163,9 @@ func Script(program, shell string, tree Tree) (string, error) {
|
||||
quoted[i] = fmt.Sprintf("%q", w)
|
||||
}
|
||||
fmt.Fprintf(&b, " printf '%%s\\n' %s\n", strings.Join(quoted, " "))
|
||||
if e.files {
|
||||
b.WriteString(" __fish_complete_path (commandline -ct)\n")
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(&b, " end\nend\ncomplete -c %s -f -a '(%s)'\n", program, fn)
|
||||
case "powershell":
|
||||
@@ -143,7 +184,18 @@ func Script(program, shell string, tree Tree) (string, error) {
|
||||
}
|
||||
b.WriteString(" default { @() }\n }\n")
|
||||
b.WriteString(" $candidates | Where-Object { $_ -like \"$wordToComplete*\" } | ForEach-Object {\n")
|
||||
b.WriteString(" [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)\n }\n}\n")
|
||||
b.WriteString(" [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)\n }\n")
|
||||
var filePrefixes []string
|
||||
for _, e := range entries {
|
||||
if e.files {
|
||||
filePrefixes = append(filePrefixes, "'"+e.prefix+"'")
|
||||
}
|
||||
}
|
||||
if len(filePrefixes) > 0 {
|
||||
fmt.Fprintf(&b, " if (@(%s) -contains $prefix) {\n", strings.Join(filePrefixes, ", "))
|
||||
b.WriteString(" [System.Management.Automation.CompletionCompleters]::CompleteFilename($wordToComplete)\n }\n")
|
||||
}
|
||||
b.WriteString("}\n")
|
||||
default:
|
||||
return "", fmt.Errorf("%w (unknown shell %q)", ErrUsage, shell)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user