Core\Command::Shell
Names the shell a completion script is written for.
Core\Command::Shell is declared in the class Core\Command. The method completions takes it.
A completion script is what makes a shell offer your program's own command names and options when somebody presses Tab. Each shell reads a different language for it, so a script is always written for one shell in particular. Core\Command::Shell has one case per shell Novis can write for — Bash, Zsh, Fish and Pwsh, which covers PowerShell 7 and Windows PowerShell alike. Hand one of them to Core\Command::completions and it writes the script from the commands your program already declares, so the completions cannot drift from the program.
Good to know: there is no case meaning "whatever shell this is". A program cannot tell reliably, and the script is a file somebody saves rather than something to print at a prompt, so the shell is named every time. There is also no case without a script writer behind it: a fifth shell would mean a fifth generator, never just a fifth name.
Examples
The four shells and where a script goes
<?nvs// Four shells, four languages for the same idea, and four places the file// belongs. Whichever one somebody uses, your program has to be told which,// so the shell is a case you pick and never a guess.
class Completions{ public static function shellName(Core\Command::Shell $shell): string { return match ($shell) { Core\Command::Shell::Bash => 'bash', Core\Command::Shell::Zsh => 'zsh', Core\Command::Shell::Fish => 'fish', Core\Command::Shell::Pwsh => 'powershell', }; }
// Where the generated file belongs, which is what somebody needs to be // told once they have one. The program here is called `tool`. public static function home(Core\Command::Shell $shell): string { return match ($shell) { Core\Command::Shell::Bash => '~/.local/share/bash-completion/completions/tool', Core\Command::Shell::Zsh => '~/.zsh/completions/_tool', Core\Command::Shell::Fish => '~/.config/fish/completions/tool.fish', Core\Command::Shell::Pwsh => "the file \$PROFILE names", }; }}
array<Core\Command::Shell> $everything = [ Core\Command::Shell::Bash, Core\Command::Shell::Zsh, Core\Command::Shell::Fish, Core\Command::Shell::Pwsh,];
foreach ($everything as Core\Command::Shell $shell) { echo Completions::shellName($shell), ' -> ', Completions::home($shell), "\n";}bash -> ~/.local/share/bash-completion/completions/toolzsh -> ~/.zsh/completions/_toolfish -> ~/.config/fish/completions/tool.fishpowershell -> the file $PROFILE namesGenerating a bash script
<?nvs// `Core\Command::completions` writes a completion script from the commands and// options you declare. The shell then offers exactly the commands and options// that the program accepts.
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; }}
// The script uses the program's name. For a file run from the command line,// that is the file name, so this example's file name appears in the output.echo Core\Command::completions(Core\Command::Shell::Bash);# bash completion for 02-generating-a-bash-script — generated by Core\Command::completions__02_generating_a_bash_script_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 __02_generating_a_bash_script_complete '02-generating-a-bash-script'Shipping a completions command
<?nvs// This program has its own `completions` command. The user types the name of// their shell, and the program writes the script to standard output. The user// can redirect it into a file with one command.
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 a completion script for your shell')] public static function completions(string $shell): uint { ?Core\Command::Shell $which = match ($shell) { 'bash' => Core\Command::Shell::Bash, 'zsh' => Core\Command::Shell::Zsh, 'fish' => Core\Command::Shell::Fish, 'pwsh' => Core\Command::Shell::Pwsh, 'powershell' => Core\Command::Shell::Pwsh, default => null, };
// An unknown shell name prints an error to standard error, and the // method returns status `2`. if ($which == null) { Core\Cli::write( 'no completion script is written for ' . $shell, { stream: Core\Cli::Stream::Err, newline: true }, ); return 2; }
// The script goes to standard output. The note about it goes to // standard error, so a redirect writes only the script to the file. Core\Cli::write(Core\Command::completions($which)); Core\Cli::write( 'save that where ' . $shell . ' looks for completions', { stream: Core\Cli::Stream::Err, newline: true }, ); return 0; }}
// In a real program, `Core\Command::run()` calls this method with what the user// typed. Here the code calls it twice, with `tcsh` and with `fish`.uint $refused = Tool::completions('tcsh');Core\Cli::write('tcsh ended with status ' . $refused, { stream: Core\Cli::Stream::Err, newline: true });
Tool::completions('fish');# fish completion for 03-shipping-a-completions-command — generated by Core\Command::completionscomplete -c '03-shipping-a-completions-command' -n '__fish_use_subcommand' -a 'deploy' -d 'Push the current build'complete -c '03-shipping-a-completions-command' -n '__fish_use_subcommand' -a 'completions' -d 'Write a completion script for your shell'