Skip to content

Commit

Permalink
Fix: execute command.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Aug 2, 2023
1 parent 572e762 commit 04beb60
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmdcomp/v2/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def generate_v2(shell: ShellType, config: V2Config) -> str:
env = Environment(
loader=FileSystemLoader(Path(__file__).parent / "templates"),
)
env.filters["ident"] = lambda x: re.sub(r"[,.-]", "_", x)
env.filters["ident"] = lambda x: re.sub(r"[\*\.,-]", "_", str(x))
template = env.get_template(f"{shell.value}.sh.jinja")

return template.render(
Expand Down
7 changes: 6 additions & 1 deletion cmdcomp/v2/templates/zsh.sh.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{%- elif argument.type == 'file' -%}
:file:_files{%- if argument.base_path is not none %} -W "{{ argument.base_path }}"{%- endif -%}
{%- elif argument.type == 'command' -%}
:command:_values '{{ arg_name|trim("-") }}' $({{ argument.execute }})
:command:_values '{{ arg_name|ident }}' '"$_{{ arg_name|ident }}_execute_result"'
{%- elif argument.type == 'flag' -%}
{%- endif -%}
{%- endmacro -%}
Expand Down Expand Up @@ -51,6 +51,11 @@
{%- endfor %}
)
{% endif %}
{%- for arg_name, argument in command.arguments.items() -%}
{%- if argument.type == "command" %}
local _{{ arg_name|ident }}_execute_result=$({{ argument.execute }})
{%- endif %}
{%- endfor %}
_arguments -C \
{%- for kwd_name, keyword in command.keyword_arguments.items() %}
{{ candidate(kwd_name, keyword) }}'{{ description(keyword) }}{{ contents(kwd_name, keyword) }}' \
Expand Down
15 changes: 15 additions & 0 deletions samples/v2/config.cmdcomp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ type = "file"
base_path = "$HOME"
description = "change project directory."

[root.subcommands.scripts]
description = "operate scripts."
[root.subcommands.scripts.subcommands.run]
description = "run script."

[root.subcommands.scripts.subcommands.run.arguments.--all]
type = "flag"
alias = "-a"
description = "run all scripts."

[root.subcommands.scripts.subcommands.run.arguments."*"]
type = "command"
description = "run script."
execute = "echo 'script1.sh script2.sh script3.sh'"

[root.subcommands.test]
description = "test command."

Expand Down
14 changes: 14 additions & 0 deletions samples/v2/config.cmdcomp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ root:
type: file
base_path: $HOME
description: "change project directory."
scripts:
description: "operate scripts."
subcommands:
run:
description: "run script."
arguments:
--all:
type: flag
alias: "-a"
description: "run all scripts."
"*":
type: command
description: "run script."
execute: "echo 'script1.sh script2.sh script3.sh'"
test:
description: "test command."
subcommands:
Expand Down
63 changes: 62 additions & 1 deletion samples/v2/output.bash
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ _cliname() {
cur=$(( cur + opts_cur + 1 ))
;;

_cliname,scripts)
cmd="_cliname_scripts"
cur=$(( cur + opts_cur + 1 ))
;;

_cliname_scripts,run)
cmd="_cliname_scripts_run"
cur=$(( cur + opts_cur + 1 ))
;;

_cliname,test)
cmd="_cliname_test"
cur=$(( cur + opts_cur + 1 ))
Expand Down Expand Up @@ -94,7 +104,7 @@ _cliname() {
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
return 0
elif [ $cur -eq $COMP_CWORD ] ; then
opts="list ls cd test"
opts="list ls cd scripts test"
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
return 0
fi
Expand Down Expand Up @@ -172,6 +182,57 @@ _cliname() {
return 0
;;

_cliname_scripts)
cmd_cur=$cur
while [ $cur -lt $COMP_CWORD ] ; do
cur=$(( cur + 1 ))
done

if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
opts=""
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
return 0
elif [ $cur -eq $COMP_CWORD ] ; then
opts="run"
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
return 0
fi

return 0
;;

_cliname_scripts_run)
cmd_cur=$cur
while [ $cur -lt $COMP_CWORD ] ; do
cur=$(( cur + 1 ))
case "${COMP_WORDS[cur-1]}" in
--all|-a)
cmd_cur=$(( cmd_cur + 1 ))
;;

*)
break
;;
esac
done

if [[ ${COMP_WORDS[COMP_CWORD]} == -* ]] ; then
opts="--all -a"
COMPREPLY=( $(compgen -W "${opts}" -- "${COMP_WORDS[COMP_CWORD]}") )
return 0
fi
cur=$COMP_CWORD
if [ $cur -eq $COMP_CWORD ] ; then
COMPREPLY=( $(compgen -W "echo 'script1.sh script2.sh script3.sh'" -- "$cur") )

return 0
else
cmd_cur=$(( cmd_cur + 2 ))
fi

return 0
;;

_cliname_test)
cmd_cur=$cur
while [ $cur -lt $COMP_CWORD ] ; do
Expand Down
30 changes: 30 additions & 0 deletions samples/v2/output.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _cliname() {
__cliname_subcmds=(
{list,ls}'[list project files.]'
cd'[cd project directory.]'
scripts'[operate scripts.]'
test'[test command.]'
)

Expand Down Expand Up @@ -47,6 +48,35 @@ _cliname() {
&& ret=0
;;

scripts)
local -a __scripts_subcmds
__scripts_subcmds=(
run'[run script.]'
)

_arguments -C \
'1: :_values "subcommand" ${__scripts_subcmds[@]}' \
'*:: :->args' \
&& ret=0

cmd_name=$words[1]
case $state in
args)
case $cmd_name in
run)
local ___execute_result=$(echo 'script1.sh script2.sh script3.sh')
_arguments -C \
{--all,-a}'[run all scripts.]' \
'*:command:_values '_' '"$___execute_result"'' \
&& ret=0
;;

esac
;;

esac
;;

test)
local -a __test_subcmds
__test_subcmds=(
Expand Down

0 comments on commit 04beb60

Please sign in to comment.