[英]How to split string by space and treat special characters as a separate word in Python?
[英]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.