Skip to content

0.0.6: completion is built in: one engine answers from the command tree, and a shell's adapter only asks it - #13

Merged
Sunrisepeak merged 1 commit into
mainfrom
feat/completion-engine
Nov 26, 2026
Merged

Sunrisepeak merged 1 commit into
mainfrom
feat/completion-engine

Conversation

@Sunrisepeak

Copy link
Copy Markdown
Member

Completion is built into the library: one engine answers from the command tree, and a shell's script only asks it. Closes #9.

Change

  • app.completion() turns it on (off by default; an application that does not call it parses as before). app.completion({.command = "", .entry = "__complete"}) leaves out the visible command.
  • myapp completions <bash|zsh|fish|powershell> prints the adapter for a shell. An adapter is a few lines that pass the words before the cursor to myapp __complete … and list the answer; nothing of the command tree is in it, so a program that gains a command completes it without its users installing anything again.
  • myapp __complete <words…> answers with one candidate per line (value<TAB>description) and a last line :<directive> (0 nothing, 1 files, 2 directories). Both are answered where --help is: the parse returns ParseError of kind completion, which is_error() reports false.
  • The engine, completions::complete(cmd, words), is a pure function of the tree and the words. It reads each word by completions::classify, the reader the parse uses: --, --k, --k=v, -k, a subcommand, a positional.
  • The facts it reads are on the tree: .values({...}) (a closed set the parse checks, which help and completion list), .value_hint(ValueHint::file|directory), .complete(fn) (the program's candidates, called only while completing), Arg::repeatable(), App::hidden() (not listed in help or completions; parses and runs as any other).
  • snapshot(app): each command has its own -h/--help, and --version is the command's that has a version, as the parse answers them. Hidden commands are not in it.
  • The bash, zsh and fish generators are removed. generate_completions(...) and App::completions(Shell) keep their signatures and return the adapter. Shell::powershell is added.

Tests

tests/completions_test.cpp: nested subcommands, global options under a subcommand, --k=v, a value taken by an option, --, closed sets, callbacks, file hints, a repeatable positional, the entry's text, adapters that hold no tree, completion off by default, the parse refusing a value outside a set, a hidden command, and the shared classifier. The bash and fish adapters were exercised against examples/completions.

Documents

docs/api.md (Shell completion, the new builders), README, version 0.0.6.

@Sunrisepeak
Sunrisepeak merged commit a2eb9fb into main Nov 26, 2026
1 check passed
@Sunrisepeak
Sunrisepeak deleted the feat/completion-engine branch November 26, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

讨论:命令补全设计——原生脚本与内部补全引擎

1 participant