繁体   English   中英

使用 Python 将带有特殊字符的 CLI arguments 视为字符串单击

[英]Treat CLI arguments with special characters as string with Python Click

在接收 arguments 时,如何将python jcr.py之后的所有内容视为字符串而不是 bash 命令?

示例 1:

input: `python jcr.py ~!@`

throws: `bash: !@: event not found`

desired: `received:->~!@<-`

示例 2:

input: `python jcr.py (`

throws: `bash: syntax error near unexpected token `('`

desired: `received:->(<-`

jcr.py:

import click

@click.command()
@click.argument('cmd', nargs=-1)
def jcr(cmd):
    click.echo('received:->%s<-' % (" ".join(cmd)))

if __name__ == '__main__':
    jcr()

由于这些是特殊字符,您可以在符号周围添加单引号或双引号,并在 python 文件中解析它们

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM