2017-02-21 04:51:20 +00:00
|
|
|
extern crate regex;
|
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
use clap::{App, Arg, SubCommand, Shell};
|
|
|
|
use regex::Regex;
|
|
|
|
|
2017-03-16 17:20:55 +00:00
|
|
|
static BASH: &'static str = r#"_myapp() {
|
|
|
|
local i cur prev opts cmds
|
|
|
|
COMPREPLY=()
|
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
cmd=""
|
|
|
|
opts=""
|
|
|
|
|
|
|
|
for i in ${COMP_WORDS[@]}
|
|
|
|
do
|
|
|
|
case "${i}" in
|
|
|
|
myapp)
|
|
|
|
cmd="myapp"
|
|
|
|
;;
|
|
|
|
|
|
|
|
help)
|
|
|
|
cmd+="__help"
|
|
|
|
;;
|
|
|
|
test)
|
|
|
|
cmd+="__test"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
case "${cmd}" in
|
|
|
|
myapp)
|
|
|
|
opts=" -h -V --help --version <file> test help"
|
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
myapp__help)
|
|
|
|
opts=" -h -V --help --version "
|
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
myapp__test)
|
|
|
|
opts=" -h -V --help --version --case "
|
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
--case)
|
|
|
|
COMPREPLY=("<case>")
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -F _myapp -o bashdefault -o default myapp
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
2017-03-16 17:20:55 +00:00
|
|
|
static ZSH: &'static str = r#"#compdef myapp
|
|
|
|
|
2018-01-15 18:19:36 +00:00
|
|
|
autoload -U is-at-least
|
|
|
|
|
2017-03-16 17:20:55 +00:00
|
|
|
_myapp() {
|
|
|
|
typeset -A opt_args
|
2018-01-13 18:56:58 +00:00
|
|
|
typeset -a _arguments_options
|
2017-03-16 17:20:55 +00:00
|
|
|
local ret=1
|
|
|
|
|
2018-01-13 18:56:58 +00:00
|
|
|
if is-at-least 5.2; then
|
|
|
|
_arguments_options=(-s -S -C)
|
|
|
|
else
|
|
|
|
_arguments_options=(-s -C)
|
|
|
|
fi
|
|
|
|
|
2017-03-16 17:20:55 +00:00
|
|
|
local context curcontext="$curcontext" state line
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2017-09-04 11:44:22 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
2018-01-15 19:47:26 +00:00
|
|
|
'::file -- some input file:_files' \
|
2018-01-13 18:06:59 +00:00
|
|
|
":: :_myapp_commands" \
|
2018-01-13 18:49:20 +00:00
|
|
|
"*::: :->myapp" \
|
2017-03-16 17:20:55 +00:00
|
|
|
&& ret=0
|
|
|
|
case $state in
|
|
|
|
(myapp)
|
2018-01-13 18:49:20 +00:00
|
|
|
words=($line[2] "${words[@]}")
|
|
|
|
(( CURRENT += 1 ))
|
|
|
|
curcontext="${curcontext%:*:*}:myapp-command-$line[2]:"
|
|
|
|
case $line[2] in
|
2017-03-16 17:20:55 +00:00
|
|
|
(test)
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2018-01-06 10:57:31 +00:00
|
|
|
'--case=[the case to test]' \
|
2017-09-04 11:44:22 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
2017-03-16 17:20:55 +00:00
|
|
|
&& ret=0
|
|
|
|
;;
|
|
|
|
(help)
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2017-09-04 11:44:22 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
2017-03-16 17:20:55 +00:00
|
|
|
&& ret=0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
(( $+functions[_myapp_commands] )) ||
|
|
|
|
_myapp_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
"test:tests things" \
|
|
|
|
"help:Prints this message or the help of the given subcommand(s)" \
|
|
|
|
)
|
|
|
|
_describe -t commands 'myapp commands' commands "$@"
|
|
|
|
}
|
|
|
|
(( $+functions[_myapp__help_commands] )) ||
|
|
|
|
_myapp__help_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
|
|
|
|
)
|
|
|
|
_describe -t commands 'myapp help commands' commands "$@"
|
|
|
|
}
|
|
|
|
(( $+functions[_myapp__test_commands] )) ||
|
|
|
|
_myapp__test_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
|
|
|
|
)
|
|
|
|
_describe -t commands 'myapp test commands' commands "$@"
|
|
|
|
}
|
|
|
|
|
2017-02-21 04:51:20 +00:00
|
|
|
_myapp "$@""#;
|
|
|
|
|
2017-03-16 17:20:55 +00:00
|
|
|
static FISH: &'static str = r#"function __fish_using_command
|
|
|
|
set cmd (commandline -opc)
|
|
|
|
if [ (count $cmd) -eq (count $argv) ]
|
|
|
|
for i in (seq (count $argv))
|
|
|
|
if [ $cmd[$i] != $argv[$i] ]
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2017-09-04 11:44:22 +00:00
|
|
|
complete -c myapp -n "__fish_using_command myapp" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp" -s V -l version -d 'Prints version information'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp" -f -a "test" -d 'tests things'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp" -f -a "help" -d 'Prints this message or the help of the given subcommand(s)'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp test" -l case -d 'the case to test'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp test" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp test" -s V -l version -d 'Prints version information'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp help" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c myapp -n "__fish_using_command myapp help" -s V -l version -d 'Prints version information'
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
|
|
|
#[cfg(not(target_os="windows"))]
|
2017-03-16 17:20:55 +00:00
|
|
|
static POWERSHELL: &'static str = r#"
|
|
|
|
@('myapp', './myapp') | %{
|
|
|
|
Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
|
|
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
|
|
|
|
$command = '_myapp'
|
|
|
|
$commandAst.CommandElements |
|
|
|
|
Select-Object -Skip 1 |
|
|
|
|
%{
|
|
|
|
switch ($_.ToString()) {
|
|
|
|
|
|
|
|
'test' {
|
|
|
|
$command += '_test'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
'help' {
|
|
|
|
$command += '_help'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$completions = @()
|
|
|
|
|
|
|
|
switch ($command) {
|
|
|
|
|
|
|
|
'_myapp' {
|
|
|
|
$completions = @('test', 'help', '-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
|
|
|
|
'_myapp_test' {
|
|
|
|
$completions = @('-h', '-V', '--case', '--help', '--version')
|
|
|
|
}
|
|
|
|
|
|
|
|
'_myapp_help' {
|
|
|
|
$completions = @('-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$completions |
|
|
|
|
?{ $_ -like "$wordToComplete*" } |
|
|
|
|
Sort-Object |
|
|
|
|
%{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
|
|
|
#[cfg(target_os="windows")]
|
2017-03-16 17:20:55 +00:00
|
|
|
static POWERSHELL: &'static str = r#"
|
|
|
|
@('myapp', './myapp', 'myapp.exe', '.\myapp', '.\myapp.exe', './myapp.exe') | %{
|
|
|
|
Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
|
|
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
$command = '_myapp'
|
|
|
|
$commandAst.CommandElements |
|
|
|
|
Select-Object -Skip 1 |
|
|
|
|
%{
|
|
|
|
switch ($_.ToString()) {
|
|
|
|
'test' {
|
|
|
|
$command += '_test'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
'help' {
|
|
|
|
$command += '_help'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$completions = @()
|
|
|
|
switch ($command) {
|
|
|
|
'_myapp' {
|
|
|
|
$completions = @('test', 'help', '-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
'_myapp_test' {
|
|
|
|
$completions = @('-h', '-V', '--case', '--help', '--version')
|
|
|
|
}
|
|
|
|
'_myapp_help' {
|
|
|
|
$completions = @('-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$completions |
|
|
|
|
?{ $_ -like "$wordToComplete*" } |
|
|
|
|
Sort-Object |
|
|
|
|
%{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
|
|
|
#[cfg(not(target_os="windows"))]
|
2017-12-16 10:09:09 +00:00
|
|
|
static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
|
2017-03-16 17:20:55 +00:00
|
|
|
@('my_app', './my_app') | %{
|
|
|
|
Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
|
|
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
|
|
|
|
$command = '_my_app'
|
|
|
|
$commandAst.CommandElements |
|
|
|
|
Select-Object -Skip 1 |
|
|
|
|
%{
|
|
|
|
switch ($_.ToString()) {
|
|
|
|
|
|
|
|
'test' {
|
|
|
|
$command += '_test'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
'some_cmd' {
|
|
|
|
$command += '_some_cmd'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
'help' {
|
|
|
|
$command += '_help'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$completions = @()
|
|
|
|
|
|
|
|
switch ($command) {
|
|
|
|
|
|
|
|
'_my_app' {
|
|
|
|
$completions = @('test', 'some_cmd', 'help', '-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
|
|
|
|
'_my_app_test' {
|
|
|
|
$completions = @('-h', '-V', '--case', '--help', '--version')
|
|
|
|
}
|
|
|
|
|
|
|
|
'_my_app_some_cmd' {
|
|
|
|
$completions = @('-h', '-V', '--config', '--help', '--version')
|
|
|
|
}
|
|
|
|
|
|
|
|
'_my_app_help' {
|
|
|
|
$completions = @('-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$completions |
|
|
|
|
?{ $_ -like "$wordToComplete*" } |
|
|
|
|
Sort-Object |
|
|
|
|
%{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
|
|
|
#[cfg(target_os="windows")]
|
2017-12-16 10:09:09 +00:00
|
|
|
static POWERSHELL_SPECIAL_CMDS: &'static str = r#"
|
2017-03-16 17:20:55 +00:00
|
|
|
@('my_app', './my_app', 'my_app.exe', '.\my_app', '.\my_app.exe', './my_app.exe') | %{
|
|
|
|
Register-ArgumentCompleter -Native -CommandName $_ -ScriptBlock {
|
|
|
|
param($wordToComplete, $commandAst, $cursorPosition)
|
|
|
|
$command = '_my_app'
|
|
|
|
$commandAst.CommandElements |
|
|
|
|
Select-Object -Skip 1 |
|
|
|
|
%{
|
|
|
|
switch ($_.ToString()) {
|
|
|
|
'test' {
|
|
|
|
$command += '_test'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
'some_cmd' {
|
|
|
|
$command += '_some_cmd'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
'help' {
|
|
|
|
$command += '_help'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$completions = @()
|
|
|
|
switch ($command) {
|
|
|
|
'_my_app' {
|
|
|
|
$completions = @('test', 'some_cmd', 'help', '-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
'_my_app_test' {
|
|
|
|
$completions = @('-h', '-V', '--case', '--help', '--version')
|
|
|
|
}
|
|
|
|
'_my_app_some_cmd' {
|
|
|
|
$completions = @('-h', '-V', '--config', '--help', '--version')
|
|
|
|
}
|
|
|
|
'_my_app_help' {
|
|
|
|
$completions = @('-h', '-V', '--help', '--version')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$completions |
|
|
|
|
?{ $_ -like "$wordToComplete*" } |
|
|
|
|
Sort-Object |
|
|
|
|
%{ New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
static ZSH_SPECIAL_CMDS: &'static str = r#"#compdef my_app
|
2017-03-16 17:20:55 +00:00
|
|
|
|
2018-01-15 18:19:36 +00:00
|
|
|
autoload -U is-at-least
|
|
|
|
|
2017-03-16 17:20:55 +00:00
|
|
|
_my_app() {
|
|
|
|
typeset -A opt_args
|
2018-01-13 18:56:58 +00:00
|
|
|
typeset -a _arguments_options
|
2017-03-16 17:20:55 +00:00
|
|
|
local ret=1
|
|
|
|
|
2018-01-13 18:56:58 +00:00
|
|
|
if is-at-least 5.2; then
|
|
|
|
_arguments_options=(-s -S -C)
|
|
|
|
else
|
|
|
|
_arguments_options=(-s -C)
|
|
|
|
fi
|
|
|
|
|
2017-03-16 17:20:55 +00:00
|
|
|
local context curcontext="$curcontext" state line
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2017-09-04 11:44:22 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
2018-01-15 19:47:26 +00:00
|
|
|
'::file -- some input file:_files' \
|
2018-01-13 18:06:59 +00:00
|
|
|
":: :_my_app_commands" \
|
2018-01-13 18:49:20 +00:00
|
|
|
"*::: :->my_app" \
|
2017-03-16 17:20:55 +00:00
|
|
|
&& ret=0
|
|
|
|
case $state in
|
|
|
|
(my_app)
|
2018-01-13 18:49:20 +00:00
|
|
|
words=($line[2] "${words[@]}")
|
|
|
|
(( CURRENT += 1 ))
|
|
|
|
curcontext="${curcontext%:*:*}:my_app-command-$line[2]:"
|
|
|
|
case $line[2] in
|
2017-03-16 17:20:55 +00:00
|
|
|
(test)
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2018-01-06 10:57:31 +00:00
|
|
|
'--case=[the case to test]' \
|
2017-09-04 11:44:22 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
2017-03-16 17:20:55 +00:00
|
|
|
&& ret=0
|
|
|
|
;;
|
|
|
|
(some_cmd)
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2018-01-06 10:57:31 +00:00
|
|
|
'--config=[the other case to test]' \
|
2017-09-04 11:44:22 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
2017-03-16 17:20:55 +00:00
|
|
|
&& ret=0
|
|
|
|
;;
|
2017-12-16 10:48:33 +00:00
|
|
|
(some-cmd-with-hypens)
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2017-12-16 10:48:33 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
|
|
|
&& ret=0
|
|
|
|
;;
|
2017-03-16 17:20:55 +00:00
|
|
|
(help)
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2017-09-04 11:44:22 +00:00
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
2017-03-16 17:20:55 +00:00
|
|
|
&& ret=0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
(( $+functions[_my_app_commands] )) ||
|
|
|
|
_my_app_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
"test:tests things" \
|
|
|
|
"some_cmd:tests other things" \
|
2017-12-16 10:48:33 +00:00
|
|
|
"some-cmd-with-hypens:" \
|
2017-03-16 17:20:55 +00:00
|
|
|
"help:Prints this message or the help of the given subcommand(s)" \
|
|
|
|
)
|
|
|
|
_describe -t commands 'my_app commands' commands "$@"
|
|
|
|
}
|
|
|
|
(( $+functions[_my_app__help_commands] )) ||
|
|
|
|
_my_app__help_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
|
|
|
|
)
|
|
|
|
_describe -t commands 'my_app help commands' commands "$@"
|
|
|
|
}
|
2017-12-16 10:48:33 +00:00
|
|
|
(( $+functions[_my_app__some-cmd-with-hypens_commands] )) ||
|
|
|
|
_my_app__some-cmd-with-hypens_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
|
|
|
|
)
|
|
|
|
_describe -t commands 'my_app some-cmd-with-hypens commands' commands "$@"
|
|
|
|
}
|
2017-03-16 17:20:55 +00:00
|
|
|
(( $+functions[_my_app__some_cmd_commands] )) ||
|
|
|
|
_my_app__some_cmd_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
|
|
|
|
)
|
|
|
|
_describe -t commands 'my_app some_cmd commands' commands "$@"
|
|
|
|
}
|
|
|
|
(( $+functions[_my_app__test_commands] )) ||
|
|
|
|
_my_app__test_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
|
|
|
|
)
|
|
|
|
_describe -t commands 'my_app test commands' commands "$@"
|
|
|
|
}
|
|
|
|
|
2017-02-21 04:51:20 +00:00
|
|
|
_my_app "$@""#;
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
static FISH_SPECIAL_CMDS: &'static str = r#"function __fish_using_command
|
2017-03-16 17:20:55 +00:00
|
|
|
set cmd (commandline -opc)
|
|
|
|
if [ (count $cmd) -eq (count $argv) ]
|
|
|
|
for i in (seq (count $argv))
|
|
|
|
if [ $cmd[$i] != $argv[$i] ]
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2017-09-04 11:44:22 +00:00
|
|
|
complete -c my_app -n "__fish_using_command my_app" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -s V -l version -d 'Prints version information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -f -a "test" -d 'tests things'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -f -a "some_cmd" -d 'tests other things'
|
2017-12-16 10:48:33 +00:00
|
|
|
complete -c my_app -n "__fish_using_command my_app" -f -a "some-cmd-with-hypens"
|
2017-09-04 11:44:22 +00:00
|
|
|
complete -c my_app -n "__fish_using_command my_app" -f -a "help" -d 'Prints this message or the help of the given subcommand(s)'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app test" -l case -d 'the case to test'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app test" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app test" -s V -l version -d 'Prints version information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app some_cmd" -l config -d 'the other case to test'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app some_cmd" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app some_cmd" -s V -l version -d 'Prints version information'
|
2017-12-16 10:48:33 +00:00
|
|
|
complete -c my_app -n "__fish_using_command my_app some-cmd-with-hypens" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app some-cmd-with-hypens" -s V -l version -d 'Prints version information'
|
2017-09-04 11:44:22 +00:00
|
|
|
complete -c my_app -n "__fish_using_command my_app help" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app help" -s V -l version -d 'Prints version information'
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
static BASH_SPECIAL_CMDS: &'static str = r#"_my_app() {
|
2017-03-16 17:20:55 +00:00
|
|
|
local i cur prev opts cmds
|
|
|
|
COMPREPLY=()
|
|
|
|
cur="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
prev="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
cmd=""
|
|
|
|
opts=""
|
|
|
|
|
|
|
|
for i in ${COMP_WORDS[@]}
|
|
|
|
do
|
|
|
|
case "${i}" in
|
|
|
|
my_app)
|
|
|
|
cmd="my_app"
|
|
|
|
;;
|
|
|
|
|
|
|
|
help)
|
|
|
|
cmd+="__help"
|
|
|
|
;;
|
2017-12-16 10:50:19 +00:00
|
|
|
some-cmd-with-hypens)
|
2017-12-16 10:48:33 +00:00
|
|
|
cmd+="__some__cmd__with__hypens"
|
|
|
|
;;
|
2017-03-16 17:20:55 +00:00
|
|
|
some_cmd)
|
|
|
|
cmd+="__some_cmd"
|
|
|
|
;;
|
|
|
|
test)
|
|
|
|
cmd+="__test"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
case "${cmd}" in
|
|
|
|
my_app)
|
2017-12-16 10:48:33 +00:00
|
|
|
opts=" -h -V --help --version <file> test some_cmd some-cmd-with-hypens help"
|
2017-03-16 17:20:55 +00:00
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
|
|
|
|
my_app__help)
|
|
|
|
opts=" -h -V --help --version "
|
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
2017-12-16 10:48:33 +00:00
|
|
|
my_app__some__cmd__with__hypens)
|
|
|
|
opts=" -h -V --help --version "
|
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
2017-03-16 17:20:55 +00:00
|
|
|
my_app__some_cmd)
|
|
|
|
opts=" -h -V --help --version --config "
|
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
--config)
|
|
|
|
COMPREPLY=("<config>")
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
my_app__test)
|
|
|
|
opts=" -h -V --help --version --case "
|
|
|
|
if [[ ${cur} == -* || ${COMP_CWORD} -eq 2 ]] ; then
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
case "${prev}" in
|
|
|
|
|
|
|
|
--case)
|
|
|
|
COMPREPLY=("<case>")
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
COMPREPLY=()
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
|
|
|
|
return 0
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
complete -F _my_app -o bashdefault -o default my_app
|
2017-02-21 04:51:20 +00:00
|
|
|
"#;
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
static FISH_SPECIAL_HELP: &'static str = r#"function __fish_using_command
|
2017-09-04 11:44:22 +00:00
|
|
|
set cmd (commandline -opc)
|
|
|
|
if [ (count $cmd) -eq (count $argv) ]
|
|
|
|
for i in (seq (count $argv))
|
|
|
|
if [ $cmd[$i] != $argv[$i] ]
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -l single-quotes -d 'Can be \'always\', \'auto\', or \'never\''
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -l double-quotes -d 'Can be "always", "auto", or "never"'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -l backticks -d 'For more information see `echo test`'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -l backslash -d 'Avoid \'\\n\''
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -l brackets -d 'List packages [filter]'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -l expansions -d 'Execute the shell command with $SHELL'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -s h -l help -d 'Prints help information'
|
|
|
|
complete -c my_app -n "__fish_using_command my_app" -s V -l version -d 'Prints version information'
|
|
|
|
"#;
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
static ZSH_SPECIAL_HELP: &'static str = r#"#compdef my_app
|
2017-09-04 11:44:22 +00:00
|
|
|
|
2018-01-15 18:19:36 +00:00
|
|
|
autoload -U is-at-least
|
|
|
|
|
2017-09-04 11:44:22 +00:00
|
|
|
_my_app() {
|
|
|
|
typeset -A opt_args
|
2018-01-13 18:56:58 +00:00
|
|
|
typeset -a _arguments_options
|
2017-09-04 11:44:22 +00:00
|
|
|
local ret=1
|
|
|
|
|
2018-01-13 18:56:58 +00:00
|
|
|
if is-at-least 5.2; then
|
|
|
|
_arguments_options=(-s -S -C)
|
|
|
|
else
|
|
|
|
_arguments_options=(-s -C)
|
|
|
|
fi
|
|
|
|
|
2017-09-04 11:44:22 +00:00
|
|
|
local context curcontext="$curcontext" state line
|
2018-01-13 18:56:58 +00:00
|
|
|
_arguments "${_arguments_options[@]}" \
|
2017-09-04 11:44:22 +00:00
|
|
|
'--single-quotes[Can be '\''always'\'', '\''auto'\'', or '\''never'\'']' \
|
|
|
|
'--double-quotes[Can be "always", "auto", or "never"]' \
|
|
|
|
'--backticks[For more information see `echo test`]' \
|
|
|
|
'--backslash[Avoid '\''\\n'\'']' \
|
|
|
|
'--brackets[List packages \[filter\]]' \
|
|
|
|
'--expansions[Execute the shell command with $SHELL]' \
|
|
|
|
'-h[Prints help information]' \
|
|
|
|
'--help[Prints help information]' \
|
|
|
|
'-V[Prints version information]' \
|
|
|
|
'--version[Prints version information]' \
|
|
|
|
&& ret=0
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
(( $+functions[_my_app_commands] )) ||
|
|
|
|
_my_app_commands() {
|
|
|
|
local commands; commands=(
|
|
|
|
|
|
|
|
)
|
|
|
|
_describe -t commands 'my_app commands' commands "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
_my_app "$@""#;
|
|
|
|
|
2017-02-21 04:51:20 +00:00
|
|
|
fn compare(left: &str, right: &str) -> bool {
|
|
|
|
let b = left == right;
|
|
|
|
if !b {
|
|
|
|
let re = Regex::new(" ").unwrap();
|
|
|
|
println!("");
|
|
|
|
println!("--> left");
|
|
|
|
// println!("{}", left);
|
|
|
|
println!("{}", re.replace_all(left, "\u{2022}"));
|
|
|
|
println!("--> right");
|
|
|
|
println!("{}", re.replace_all(right, "\u{2022}"));
|
|
|
|
// println!("{}", right);
|
|
|
|
println!("--")
|
|
|
|
}
|
|
|
|
b
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_app() -> App<'static, 'static> { build_app_with_name("myapp") }
|
|
|
|
|
|
|
|
fn build_app_with_name(s: &'static str) -> App<'static, 'static> {
|
|
|
|
App::new(s)
|
|
|
|
.about("Tests completions")
|
|
|
|
.arg(Arg::with_name("file").help("some input file"))
|
|
|
|
.subcommand(SubCommand::with_name("test")
|
|
|
|
.about("tests things")
|
|
|
|
.arg(Arg::with_name("case")
|
|
|
|
.long("case")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("the case to test")))
|
|
|
|
}
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
fn build_app_special_commands() -> App<'static, 'static> {
|
2017-12-16 10:48:33 +00:00
|
|
|
build_app_with_name("my_app")
|
|
|
|
.subcommand(SubCommand::with_name("some_cmd")
|
|
|
|
.about("tests other things")
|
|
|
|
.arg(Arg::with_name("config")
|
|
|
|
.long("--config")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("the other case to test")))
|
|
|
|
.subcommand(SubCommand::with_name("some-cmd-with-hypens"))
|
2017-02-21 04:51:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
fn build_app_special_help() -> App<'static, 'static> {
|
2017-09-04 11:44:22 +00:00
|
|
|
App::new("my_app")
|
|
|
|
.arg(Arg::with_name("single-quotes")
|
|
|
|
.long("single-quotes")
|
|
|
|
.help("Can be 'always', 'auto', or 'never'"))
|
|
|
|
.arg(Arg::with_name("double-quotes")
|
|
|
|
.long("double-quotes")
|
|
|
|
.help("Can be \"always\", \"auto\", or \"never\""))
|
|
|
|
.arg(Arg::with_name("backticks")
|
|
|
|
.long("backticks")
|
|
|
|
.help("For more information see `echo test`"))
|
|
|
|
.arg(Arg::with_name("backslash")
|
|
|
|
.long("backslash")
|
|
|
|
.help("Avoid '\\n'"))
|
|
|
|
.arg(Arg::with_name("brackets")
|
|
|
|
.long("brackets")
|
|
|
|
.help("List packages [filter]"))
|
|
|
|
.arg(Arg::with_name("expansions")
|
|
|
|
.long("expansions")
|
|
|
|
.help("Execute the shell command with $SHELL"))
|
|
|
|
}
|
|
|
|
|
2017-02-21 04:51:20 +00:00
|
|
|
#[test]
|
|
|
|
fn bash() {
|
|
|
|
let mut app = build_app();
|
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("myapp", Shell::Bash, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
|
|
|
assert!(compare(&*string, BASH));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn zsh() {
|
|
|
|
let mut app = build_app();
|
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("myapp", Shell::Zsh, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
|
|
|
assert!(compare(&*string, ZSH));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn fish() {
|
|
|
|
let mut app = build_app();
|
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("myapp", Shell::Fish, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
|
|
|
assert!(compare(&*string, FISH));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disabled until I figure out this windows line ending and AppVeyor issues
|
|
|
|
//#[test]
|
|
|
|
// fn powershell() {
|
|
|
|
// let mut app = build_app();
|
|
|
|
// let mut buf = vec![];
|
|
|
|
// app.gen_completions_to("myapp", Shell::PowerShell, &mut buf);
|
|
|
|
// let string = String::from_utf8(buf).unwrap();
|
|
|
|
//
|
|
|
|
// assert!(compare(&*string, POWERSHELL));
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Disabled until I figure out this windows line ending and AppVeyor issues
|
|
|
|
//#[test]
|
2017-12-16 10:09:09 +00:00
|
|
|
// fn powershell_with_special_commands() {
|
|
|
|
// let mut app = build_app_special_commands();
|
2017-02-21 04:51:20 +00:00
|
|
|
// let mut buf = vec![];
|
|
|
|
// app.gen_completions_to("my_app", Shell::PowerShell, &mut buf);
|
|
|
|
// let string = String::from_utf8(buf).unwrap();
|
|
|
|
//
|
2017-12-16 10:09:09 +00:00
|
|
|
// assert!(compare(&*string, POWERSHELL_SPECIAL_CMDS));
|
2017-02-21 04:51:20 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
#[test]
|
2017-12-16 10:09:09 +00:00
|
|
|
fn bash_with_special_commands() {
|
|
|
|
let mut app = build_app_special_commands();
|
2017-02-21 04:51:20 +00:00
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("my_app", Shell::Bash, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
assert!(compare(&*string, BASH_SPECIAL_CMDS));
|
2017-02-21 04:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-16 10:09:09 +00:00
|
|
|
fn fish_with_special_commands() {
|
|
|
|
let mut app = build_app_special_commands();
|
2017-02-21 04:51:20 +00:00
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("my_app", Shell::Fish, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
assert!(compare(&*string, FISH_SPECIAL_CMDS));
|
2017-02-21 04:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-16 10:09:09 +00:00
|
|
|
fn zsh_with_special_commands() {
|
|
|
|
let mut app = build_app_special_commands();
|
2017-02-21 04:51:20 +00:00
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("my_app", Shell::Zsh, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
assert!(compare(&*string, ZSH_SPECIAL_CMDS));
|
2017-02-21 04:51:20 +00:00
|
|
|
}
|
2017-09-04 11:44:22 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-12-16 10:09:09 +00:00
|
|
|
fn fish_with_special_help() {
|
|
|
|
let mut app = build_app_special_help();
|
2017-09-04 11:44:22 +00:00
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("my_app", Shell::Fish, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
assert!(compare(&*string, FISH_SPECIAL_HELP));
|
2017-09-04 11:44:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-12-16 10:09:09 +00:00
|
|
|
fn zsh_with_special_help() {
|
|
|
|
let mut app = build_app_special_help();
|
2017-09-04 11:44:22 +00:00
|
|
|
let mut buf = vec![];
|
|
|
|
app.gen_completions_to("my_app", Shell::Zsh, &mut buf);
|
|
|
|
let string = String::from_utf8(buf).unwrap();
|
|
|
|
|
2017-12-16 10:09:09 +00:00
|
|
|
assert!(compare(&*string, ZSH_SPECIAL_HELP));
|
2017-09-04 11:44:22 +00:00
|
|
|
}
|