-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(complete): Add custom bin name test
- Loading branch information
Showing
10 changed files
with
387 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
_bin-name() { | ||
local i cur prev opts cmd | ||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
prev="${COMP_WORDS[COMP_CWORD-1]}" | ||
cmd="" | ||
opts="" | ||
|
||
for i in ${COMP_WORDS[@]} | ||
do | ||
case "${cmd},${i}" in | ||
",$1") | ||
cmd="bin__name" | ||
;; | ||
my__app,help) | ||
cmd="my__app__help" | ||
;; | ||
my__app,test) | ||
cmd="my__app__test" | ||
;; | ||
my__app__help,help) | ||
cmd="my__app__help__help" | ||
;; | ||
my__app__help,test) | ||
cmd="my__app__help__test" | ||
;; | ||
*) | ||
;; | ||
esac | ||
done | ||
|
||
case "${cmd}" in | ||
bin__name) | ||
opts="-c -v -h --help 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 | ||
;; | ||
bin__name__help) | ||
opts="test help" | ||
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 | ||
;; | ||
bin__name__help__help) | ||
opts="" | ||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then | ||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) | ||
return 0 | ||
fi | ||
case "${prev}" in | ||
*) | ||
COMPREPLY=() | ||
;; | ||
esac | ||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) | ||
return 0 | ||
;; | ||
bin__name__help__test) | ||
opts="" | ||
if [[ ${cur} == -* || ${COMP_CWORD} -eq 3 ]] ; then | ||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) | ||
return 0 | ||
fi | ||
case "${prev}" in | ||
*) | ||
COMPREPLY=() | ||
;; | ||
esac | ||
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) | ||
return 0 | ||
;; | ||
bin__name__test) | ||
opts="-d -c -h --help" | ||
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 | ||
;; | ||
esac | ||
} | ||
|
||
if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then | ||
complete -F _bin-name -o nosort -o bashdefault -o default bin-name | ||
else | ||
complete -F _bin-name -o bashdefault -o default bin-name | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
use builtin; | ||
use str; | ||
|
||
set edit:completion:arg-completer[bin-name] = {|@words| | ||
fn spaces {|n| | ||
builtin:repeat $n ' ' | str:join '' | ||
} | ||
fn cand {|text desc| | ||
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc | ||
} | ||
var command = 'bin-name' | ||
for word $words[1..-1] { | ||
if (str:has-prefix $word '-') { | ||
break | ||
} | ||
set command = $command';'$word | ||
} | ||
var completions = [ | ||
&'bin-name'= { | ||
cand -c 'c' | ||
cand -v 'v' | ||
cand -h 'Print help' | ||
cand --help 'Print help' | ||
cand test 'Subcommand' | ||
cand help 'Print this message or the help of the given subcommand(s)' | ||
} | ||
&'bin-name;test'= { | ||
cand -d 'd' | ||
cand -c 'c' | ||
cand -h 'Print help' | ||
cand --help 'Print help' | ||
} | ||
&'bin-name;help'= { | ||
cand test 'Subcommand' | ||
cand help 'Print this message or the help of the given subcommand(s)' | ||
} | ||
&'bin-name;help;test'= { | ||
} | ||
&'bin-name;help;help'= { | ||
} | ||
] | ||
$completions[$command] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
complete -c bin-name -n "__fish_use_subcommand" -s c | ||
complete -c bin-name -n "__fish_use_subcommand" -s v | ||
complete -c bin-name -n "__fish_use_subcommand" -s h -l help -d 'Print help' | ||
complete -c bin-name -n "__fish_use_subcommand" -f -a "test" -d 'Subcommand' | ||
complete -c bin-name -n "__fish_use_subcommand" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' | ||
complete -c bin-name -n "__fish_seen_subcommand_from test" -s d | ||
complete -c bin-name -n "__fish_seen_subcommand_from test" -s c | ||
complete -c bin-name -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help' | ||
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "test" -d 'Subcommand' | ||
complete -c bin-name -n "__fish_seen_subcommand_from help; and not __fish_seen_subcommand_from test; and not __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
using namespace System.Management.Automation | ||
using namespace System.Management.Automation.Language | ||
|
||
Register-ArgumentCompleter -Native -CommandName 'bin-name' -ScriptBlock { | ||
param($wordToComplete, $commandAst, $cursorPosition) | ||
|
||
$commandElements = $commandAst.CommandElements | ||
$command = @( | ||
'bin-name' | ||
for ($i = 1; $i -lt $commandElements.Count; $i++) { | ||
$element = $commandElements[$i] | ||
if ($element -isnot [StringConstantExpressionAst] -or | ||
$element.StringConstantType -ne [StringConstantType]::BareWord -or | ||
$element.Value.StartsWith('-') -or | ||
$element.Value -eq $wordToComplete) { | ||
break | ||
} | ||
$element.Value | ||
}) -join ';' | ||
|
||
$completions = @(switch ($command) { | ||
'bin-name' { | ||
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c') | ||
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'v') | ||
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') | ||
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') | ||
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand') | ||
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') | ||
break | ||
} | ||
'bin-name;test' { | ||
[CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'd') | ||
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'c') | ||
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help') | ||
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help') | ||
break | ||
} | ||
'bin-name;help' { | ||
[CompletionResult]::new('test', 'test', [CompletionResultType]::ParameterValue, 'Subcommand') | ||
[CompletionResult]::new('help', 'help', [CompletionResultType]::ParameterValue, 'Print this message or the help of the given subcommand(s)') | ||
break | ||
} | ||
'bin-name;help;test' { | ||
break | ||
} | ||
'bin-name;help;help' { | ||
break | ||
} | ||
}) | ||
|
||
$completions.Where{ $_.CompletionText -like "$wordToComplete*" } | | ||
Sort-Object -Property ListItemText | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#compdef bin-name | ||
|
||
autoload -U is-at-least | ||
|
||
_bin-name() { | ||
typeset -A opt_args | ||
typeset -a _arguments_options | ||
local ret=1 | ||
|
||
if is-at-least 5.2; then | ||
_arguments_options=(-s -S -C) | ||
else | ||
_arguments_options=(-s -C) | ||
fi | ||
|
||
local context curcontext="$curcontext" state line | ||
_arguments "${_arguments_options[@]}" \ | ||
'-c[]' \ | ||
'(-c)-v[]' \ | ||
'-h[Print help]' \ | ||
'--help[Print help]' \ | ||
":: :_bin-name_commands" \ | ||
"*::: :->my-app" \ | ||
&& ret=0 | ||
case $state in | ||
(my-app) | ||
words=($line[1] "${words[@]}") | ||
(( CURRENT += 1 )) | ||
curcontext="${curcontext%:*:*}:bin-name-command-$line[1]:" | ||
case $line[1] in | ||
(test) | ||
_arguments "${_arguments_options[@]}" \ | ||
'*-d[]' \ | ||
'-c[]' \ | ||
'-h[Print help]' \ | ||
'--help[Print help]' \ | ||
&& ret=0 | ||
;; | ||
(help) | ||
_arguments "${_arguments_options[@]}" \ | ||
":: :_bin-name__help_commands" \ | ||
"*::: :->help" \ | ||
&& ret=0 | ||
|
||
case $state in | ||
(help) | ||
words=($line[1] "${words[@]}") | ||
(( CURRENT += 1 )) | ||
curcontext="${curcontext%:*:*}:bin-name-help-command-$line[1]:" | ||
case $line[1] in | ||
(test) | ||
_arguments "${_arguments_options[@]}" \ | ||
&& ret=0 | ||
;; | ||
(help) | ||
_arguments "${_arguments_options[@]}" \ | ||
&& ret=0 | ||
;; | ||
esac | ||
;; | ||
esac | ||
;; | ||
esac | ||
;; | ||
esac | ||
} | ||
|
||
(( $+functions[_bin-name_commands] )) || | ||
_bin-name_commands() { | ||
local commands; commands=( | ||
'test:Subcommand' \ | ||
'help:Print this message or the help of the given subcommand(s)' \ | ||
) | ||
_describe -t commands 'bin-name commands' commands "$@" | ||
} | ||
(( $+functions[_bin-name__help_commands] )) || | ||
_bin-name__help_commands() { | ||
local commands; commands=( | ||
'test:Subcommand' \ | ||
'help:Print this message or the help of the given subcommand(s)' \ | ||
) | ||
_describe -t commands 'bin-name help commands' commands "$@" | ||
} | ||
(( $+functions[_bin-name__help__help_commands] )) || | ||
_bin-name__help__help_commands() { | ||
local commands; commands=() | ||
_describe -t commands 'bin-name help help commands' commands "$@" | ||
} | ||
(( $+functions[_bin-name__help__test_commands] )) || | ||
_bin-name__help__test_commands() { | ||
local commands; commands=() | ||
_describe -t commands 'bin-name help test commands' commands "$@" | ||
} | ||
(( $+functions[_bin-name__test_commands] )) || | ||
_bin-name__test_commands() { | ||
local commands; commands=() | ||
_describe -t commands 'bin-name test commands' commands "$@" | ||
} | ||
|
||
if [ "$funcstack[1]" = "_bin-name" ]; then | ||
_bin-name "$@" | ||
else | ||
compdef _bin-name bin-name | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.