From 04beb6007662fe954bd5a7c4f52e73edf716e628 Mon Sep 17 00:00:00 2001 From: yassun4dev Date: Wed, 2 Aug 2023 23:12:52 +0900 Subject: [PATCH] Fix: execute command. --- cmdcomp/v2/completion.py | 2 +- cmdcomp/v2/templates/zsh.sh.jinja | 7 +++- samples/v2/config.cmdcomp.toml | 15 ++++++++ samples/v2/config.cmdcomp.yaml | 14 +++++++ samples/v2/output.bash | 63 ++++++++++++++++++++++++++++++- samples/v2/output.zsh | 30 +++++++++++++++ 6 files changed, 128 insertions(+), 3 deletions(-) diff --git a/cmdcomp/v2/completion.py b/cmdcomp/v2/completion.py index 33375b0..6c34445 100644 --- a/cmdcomp/v2/completion.py +++ b/cmdcomp/v2/completion.py @@ -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( diff --git a/cmdcomp/v2/templates/zsh.sh.jinja b/cmdcomp/v2/templates/zsh.sh.jinja index afb209a..3ac2681 100644 --- a/cmdcomp/v2/templates/zsh.sh.jinja +++ b/cmdcomp/v2/templates/zsh.sh.jinja @@ -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 -%} @@ -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) }}' \ diff --git a/samples/v2/config.cmdcomp.toml b/samples/v2/config.cmdcomp.toml index 8f4c531..86cbb91 100644 --- a/samples/v2/config.cmdcomp.toml +++ b/samples/v2/config.cmdcomp.toml @@ -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." diff --git a/samples/v2/config.cmdcomp.yaml b/samples/v2/config.cmdcomp.yaml index f539b61..bbb881b 100644 --- a/samples/v2/config.cmdcomp.yaml +++ b/samples/v2/config.cmdcomp.yaml @@ -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: diff --git a/samples/v2/output.bash b/samples/v2/output.bash index 62f73f8..eb421b8 100644 --- a/samples/v2/output.bash +++ b/samples/v2/output.bash @@ -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 )) @@ -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 @@ -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 diff --git a/samples/v2/output.zsh b/samples/v2/output.zsh index 931e4f0..a74a7f1 100644 --- a/samples/v2/output.zsh +++ b/samples/v2/output.zsh @@ -16,6 +16,7 @@ _cliname() { __cliname_subcmds=( {list,ls}'[list project files.]' cd'[cd project directory.]' + scripts'[operate scripts.]' test'[test command.]' ) @@ -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=(