Select Git revision
CompletionScriptCommand.ts

michael.minelli authored
CompletionScriptCommand.ts 1.78 KiB
import CommanderCommand from '../../CommanderCommand';
import { Argument } from 'commander';
class CompletionScriptCommand extends CommanderCommand {
protected commandName: string = 'script';
protected defineCommand() {
this.command.description('generate script completion')
.addArgument(new Argument('<shell>', 'shell completion format').choices([ 'bash', 'zsh' ]))
.action(this.commandAction.bind(this));
}
private bashCompletionScript() {
console.log(`
#/usr/bin/env bash
###-begin-dojo-completions-###
#
# dojo command completion script for bash
#
# Installation: dojo completion bash
#
function _dojo_completions()
{
latest="\${COMP_WORDS[$COMP_CWORD]}"
words=$(dojo completion get --shell bash \${COMP_WORDS[@]})
COMPREPLY=($(compgen -W "$words" -- $latest))
return 0
}
complete -F _dojo_completions dojo
###-end-dojo-completions-###
`);
}
private zshCompletionScript() {
console.log(`
#compdef dojo
###-begin-dojo-completions-###
#
# dojo command completion script for zsh
#
# Installation: dojo completion zsh
#
_dojo_completions()
{
local reply
local si=$IFS
IFS=$'
' reply=($(dojo completion get --shell zsh \${words[@]}))
IFS=$si
_describe 'values' reply
}
compdef _dojo_completions dojo
###-end-dojo-completions-###
`);
}
protected async commandAction(shell: 'bash' | 'zsh'): Promise<void> {
switch ( shell ) {
case 'bash':
this.bashCompletionScript();
break;
case 'zsh':
this.zshCompletionScript();
break;
default:
console.error('Unsupported shell completion format');
break;
}
}
}
export default new CompletionScriptCommand();