Core\Command::completions
Core\Command::completions(Core\Command::Shell $shell): stringCore\Command::completions( Core\Command::Shell $shell, ): string
Writes a completion script for your program, so a shell can finish command names and options while somebody is typing them.
The script comes from the same table the usage page does: every command your program declares with #[Core\Command], and every option each one takes. You choose the shell, and Novis writes bash, zsh, fish or PowerShell, each in that shell's own syntax. Add a command to your program and the next script has it, with nothing else to update.
A script completes two things: the command word, and the options of whichever command the first word already named. It never completes a value, because a declared type is not a list of values.
Good to know: the result is plain text ending with a newline. It is something to redirect into the shell's completion directory once, not something to print at a terminal. A program with no name of its own throws a LogicError, which is every way of starting a program except from a command line.
Parameters
$shellCore\Command::ShellThe shell to write the script for.
Return value
Returns string. The script as plain text, ending with a newline. Save it in the completion directory of the shell.
Errors
LogicErrorThe program has no name to register the script for. This is the case everywhere except in a command-line run, such as in a web request.
Examples
A script for bash
<?nvs// Shows how to write the completion script for one shell and print it.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy( string $target, #[Core\Option(short: 'n', about: 'Print what would happen')] bool $dryRun = false, ): uint { return 0; }
#[Core\Command(name: 'version', about: 'Print the version and stop')] public static function version(): uint { return 0; }}
// Both commands and both spellings of the option are in the script below, and// none of them is written here. They come from the declarations above.echo Core\Command::completions(Core\Command::Shell::Bash);# bash completion for 01-a-script-for-bash — generated by Core\Command::completions__01_a_script_for_bash_complete() { local cur="${COMP_WORDS[COMP_CWORD]}" if [ "$COMP_CWORD" -eq 1 ]; then COMPREPLY=($(compgen -W 'deploy version' -- "$cur")) return fi local options='' case "${COMP_WORDS[1]}" in 'deploy') options='-n --dryRun' ;; esac COMPREPLY=($(compgen -W "$options" -- "$cur"))}complete -F __01_a_script_for_bash_complete '01-a-script-for-bash'The same table in fish
<?nvs// Shows the same command table written for another shell. Each shell gets a// script in its own syntax, so the one you get is one you can read and edit.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy( string $target, #[Core\Option(short: 'n', about: 'Print what would happen')] bool $dryRun = false, ): uint { return 0; }
#[Core\Command(name: 'version', about: 'Print the version and stop')] public static function version(): uint { return 0; }}
// `fish` takes one `complete` line per command and per option spelling. The// text after `-d` is what `about:` said.echo Core\Command::completions(Core\Command::Shell::Fish);# fish completion for 02-the-same-table-in-fish — generated by Core\Command::completionscomplete -c '02-the-same-table-in-fish' -n '__fish_use_subcommand' -a 'deploy' -d 'Push the current build'complete -c '02-the-same-table-in-fish' -n '__fish_seen_subcommand_from deploy' -s 'n' -d 'Print what would happen'complete -c '02-the-same-table-in-fish' -n '__fish_seen_subcommand_from deploy' -l 'dryRun' -d 'Print what would happen'complete -c '02-the-same-table-in-fish' -n '__fish_use_subcommand' -a 'version' -d 'Print the version and stop'Your own completions command
<?nvs// Shows the everyday use: your program has a `completions` command, so somebody// runs `tool completions fish > ~/.config/fish/completions/tool.fish` once and// gets command names and options from then on.
class Tool{ #[Core\Command(name: 'deploy', about: 'Push the current build')] public static function deploy(string $target): uint { return 0; }
#[Core\Command(name: 'completions', about: 'Write the completion script for a shell')] public static function completions(Core\Command::Shell $shell): uint { // The word after `completions` becomes the case of `Core\Command::Shell` // it names. A word that names no case is a usage error. echo Core\Command::completions($shell); return 0; }}
// This is what `tool completions fish` prints. The `completions` command is in// the script too, because it is a command like any other.Tool::completions(Core\Command::Shell::Fish);# fish completion for 03-your-own-completions-command — generated by Core\Command::completionscomplete -c '03-your-own-completions-command' -n '__fish_use_subcommand' -a 'deploy' -d 'Push the current build'complete -c '03-your-own-completions-command' -n '__fish_use_subcommand' -a 'completions' -d 'Write the completion script for a shell'