Treat CLI arguments with special characters as string with Python Click

Viewed 515

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()
1 Answers

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

Related