简体   繁体   中英

Treat CLI arguments with special characters as string with Python Click

When taking in the arguments how do you treat everything after the python jcr.py as a string as opposed to a bash command?

Example 1:

input: `python jcr.py ~!@`

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

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

Example 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()

Since these are special characters you can add single or double quotes around the symbols and parse these in your python file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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