mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-06-19 00:17:51 +02:00
add special options to bash completion
This commit is contained in:
@ -8,15 +8,23 @@ from .lib import all_parser_info
|
||||
bash_template = Template('''\
|
||||
_jc()
|
||||
{
|
||||
local cur prev words cword jc_commands jc_parsers jc_options
|
||||
local cur prev words cword jc_commands jc_parsers jc_options jc_special_options
|
||||
|
||||
jc_commands=(${bash_commands})
|
||||
jc_parsers=(${bash_arguments})
|
||||
jc_options=(${bash_options})
|
||||
jc_special_options=(${bash_special_opts})
|
||||
|
||||
COMPREPLY=()
|
||||
_get_comp_words_by_ref cur prev words cword
|
||||
|
||||
# if special options are found anywhere in the line, then no more completions
|
||||
for i in "$${words[@]}"; do
|
||||
if [[ " $${jc_special_options[*]} " =~ " $${i} " ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
|
||||
# if magic command is found anywhere in the line, use called command's autocompletion
|
||||
for i in "$${words[@]}"; do
|
||||
if [[ " $${jc_commands[*]} " =~ " $${i} " ]]; then
|
||||
@ -35,7 +43,7 @@ _jc()
|
||||
done
|
||||
|
||||
# default completion
|
||||
COMPREPLY=( $$( compgen -W "$${jc_options[*]} $${jc_parsers[*]} $${jc_commands[*]}" \\
|
||||
COMPREPLY=( $$( compgen -W "$${jc_options[*]} $${jc_special_options[*]} $${jc_parsers[*]} $${jc_commands[*]}" \\
|
||||
-- "$${cur}" ) )
|
||||
} &&
|
||||
complete -F _jc jc
|
||||
@ -46,23 +54,30 @@ zsh_template = Template('''\
|
||||
#compdef jc
|
||||
|
||||
_jc() {
|
||||
|
||||
# if magic command is found anywhere in the line, use called command's autocompletion
|
||||
|
||||
# if a parser arg is found anywhere in the line, only show options
|
||||
|
||||
# default completion
|
||||
# autogenerate completions based on jc --help output
|
||||
_arguments --
|
||||
# _arguments --
|
||||
|
||||
# add commands supported by magic syntax
|
||||
local -a commands
|
||||
commands=(
|
||||
# e.g. 'arp:run arp with magic syntax.'
|
||||
${zsh_commands}
|
||||
)
|
||||
# local -a commands
|
||||
# commands=(
|
||||
# # e.g. 'arp:run arp with magic syntax.'
|
||||
# ${zsh_commands}
|
||||
# )
|
||||
|
||||
_describe -t commands 'commands' commands
|
||||
return 0
|
||||
# _describe -t commands 'commands' commands
|
||||
# return 0
|
||||
}
|
||||
|
||||
_jc
|
||||
''')
|
||||
|
||||
special_options = ['--about', '-a', '--version', '-v', '--bash-comp', '-B', '--zsh-comp', '-Z']
|
||||
|
||||
def get_commands():
|
||||
command_list = []
|
||||
@ -101,9 +116,14 @@ def gen_zsh_command_descriptions(command_list):
|
||||
|
||||
def bash_completion():
|
||||
args = ' '.join(get_arguments())
|
||||
options = ' '.join(get_options())
|
||||
opts_no_special = get_options()
|
||||
for option in special_options:
|
||||
opts_no_special.remove(option)
|
||||
options = ' '.join(opts_no_special)
|
||||
s_options = ' '.join(special_options)
|
||||
commands = ' '.join(get_commands())
|
||||
return bash_template.substitute(bash_arguments=args,
|
||||
bash_special_opts=s_options,
|
||||
bash_options=options,
|
||||
bash_commands=commands)
|
||||
|
||||
|
Reference in New Issue
Block a user