Rafael T
Rafael T

Reputation: 15679

python argparse shell special chars

I have a script where one has to enter a password. This works for most passwords, except for the "good" ones, where I get strange results.

#! /usr/local/bin/python
from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument("-t", "--test")

print(parser.parse_args())

and call it with

./test.py -t test$$test

will print

Namespace(test='test5365test')

The shell is treating the password as a special charakter.

My question is, if there is a way to disable that inside my code and not enforcing users to properly escape the character?

Upvotes: 2

Views: 4604

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881605

By the time your code gets the args, the shell has already processed them. So you must protect them on the shell command line with single quotes or escaping.

For example, instead of

./test.py -t test$$test

you should use

./test.py -t 'test$$test'

Upvotes: 7

Related Questions