diff --git a/NodeApp/src/commander/completion/subcommands/CompletionScriptCommand.ts b/NodeApp/src/commander/completion/subcommands/CompletionScriptCommand.ts new file mode 100644 index 0000000000000000000000000000000000000000..45c5f3e52a167b0eb76c370e5010d75959f2e56e --- /dev/null +++ b/NodeApp/src/commander/completion/subcommands/CompletionScriptCommand.ts @@ -0,0 +1,75 @@ +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(); \ No newline at end of file