简体   繁体   中英

python argparse input error

I have the following two arguments to be specified:

parser.add_argument("-s", "-server", help="specify the server", required=True)
parser.add_argument("-pw", "-password", help="Provide your Admin Password", required=True)

This works great except that my go-to "test"/dummy password has uncovered a bit of a problem.

My dummy password is 90opl;./

if I run ./test.py -s testserver -pw 90opl;./

I get an error because it's not interpreting 90opl;./ as ' 90opl;,./ '

so the auth fails and then -bash says ./: Is a directory (the end of the pw).

to get around this I must run ./test.py -s testserver -pw '90opl;./' - aka single quotes must be used.

Is there a way around this?? I can't seem to find an answer online. I've tried specifying type=str to no avail.

Sorry if this has an obvious fix but I'm a bit of a python noob.

This is not a Python issue. This is a shell issue.

The Bash shell gives a specific interpretation to the ; character in most contexts, and quoting it is the most sane and readable way to avoid this interpretation.

There is nothing Python or the argparse library can do about this, because bash interprets the command line before Python ever gets to see it.

If you have to type a default dummy password at the command line a zillion times... make it an easier password that doesn't contain any shell meta-characters :-P.

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