Reputation: 783
I am attempting to pass in a string as input argument to a Python program, from the command line i.e. $python parser_prog.py <pos1> <pos2> --opt1 --opt2
and interpreting these using argparse. Of course if contains any metacharacters these are first interpreted by the shell, so it needs to be quoted.
This seems to work, strings are passed through literally, preserving the \*?! characters:
$ python parser_prog.py 'str\1*?' 'str2!'
However, when I attempt to pass through a '-' (hyphen) character, I cannot seem to mask it. It is interpreted as an invalid option.
$ python parser_prog.py 'str\1*?' '-str2!'
I have tried single and double quotes, is there a way to make sure Python interprets this as a raw string? (I'm not in the interpreter yet, this is on the shell command line, so I can't use pythonic expressions such as r'str1'
)
Thank you for any hints!
Upvotes: 0
Views: 1201
Reputation: 602725
As you said yourself, Python only sees the strings after being processed by the shell. The command-line arguments '-f'
and -f
look identical to the called program, and there is no way to dsitinguish them. That said, I think that argparse
supports a --
argument to denote the end of the options, and everything after this is treated as a positional argument.
Upvotes: 2