diff --git a/cmdcomp/v2/completion.py b/cmdcomp/v2/completion.py index 6c34445..c68fd35 100644 --- a/cmdcomp/v2/completion.py +++ b/cmdcomp/v2/completion.py @@ -14,6 +14,7 @@ def generate_v2(shell: ShellType, config: V2Config) -> str: loader=FileSystemLoader(Path(__file__).parent / "templates"), ) env.filters["ident"] = lambda x: re.sub(r"[\*\.,-]", "_", str(x)) + env.filters["escape"] = lambda x: x.replace("'", "''") 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 e762922..a4f891d 100644 --- a/cmdcomp/v2/templates/zsh.sh.jinja +++ b/cmdcomp/v2/templates/zsh.sh.jinja @@ -27,7 +27,7 @@ {%- endif -%} {%- endmacro -%} -#!/bin/zsh +#compdef {{ app_name }} # # Code generated by cmdcomp "{{ version }}". DO NOT EDIT. # For more information about cmdcomp, please refer to https://github.com/yassun7010/cmdcomp . @@ -54,7 +54,7 @@ local -a _{{ scope|ident }}_subcmds _{{ scope|ident }}_subcmds=( {%- for subcmd_name, subcommand in command.subcommands.items() %} - {{ candidate(subcmd_name, subcommand) }}'{{ description(subcommand) }}' + {{ candidate(subcmd_name, subcommand) }}'{{ description(subcommand)|escape }}' {%- endfor %} ) {% endif %} @@ -65,7 +65,7 @@ {%- endfor %} _arguments -C \ {%- for kwd_name, keyword in command.keyword_arguments.items() %} - {{ candidate(kwd_name, keyword) }}'{{ description(keyword) }}{{ contents(kwd_name, keyword) }}' \ + {{ candidate(kwd_name, keyword) }}'{{ description(keyword)|escape }}{{ contents(kwd_name, keyword) }}' \ {%- endfor %} {%- for pos_name, positional in command.positional_arguments.items() %} '{{ pos_name }}{{ contents(pos_name, positional) }}' \ diff --git a/examples/demo/output.zsh b/examples/demo/output.zsh index 3982105..174bdf0 100644 --- a/examples/demo/output.zsh +++ b/examples/demo/output.zsh @@ -1,4 +1,4 @@ -#!/bin/zsh +#compdef mycli # # Code generated by cmdcomp "$CMDCOMP_VERSION". DO NOT EDIT. # For more information about cmdcomp, please refer to https://github.com/yassun7010/cmdcomp . diff --git a/examples/v2/output.zsh b/examples/v2/output.zsh index 0cdf26e..b6bcb69 100644 --- a/examples/v2/output.zsh +++ b/examples/v2/output.zsh @@ -1,4 +1,4 @@ -#!/bin/zsh +#compdef cliname # # Code generated by cmdcomp "$CMDCOMP_VERSION". DO NOT EDIT. # For more information about cmdcomp, please refer to https://github.com/yassun7010/cmdcomp .