Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing empty string to argparse

I'm using argparse (Python 3.2). A parameter mode is defined simply as:

p.add_argument('--mode', dest='mode')

I want to call the program from the command line in such a way that parameter mode is set to an empty string; or at least to a single space ' ' (I can strip whitespace later).

I tried to use (under Windows) program_name --mode "" and program_name --mode " ", but neither worked.

like image 444
max Avatar asked Nov 26 '12 05:11

max


1 Answers

This seems to work for me under OS-X:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--mode')
p = parser.parse_args()
print(p)

and then:

python test.py --mode=

I don't have a windows machine, so I don't know anything about how those operate...

Of course, the other things you mentioned above would work as well on OS-X. The advantage here is that it shouldn't rely on how the shell splits arguments enclosed in quotations which should make it work in more places I would think.

like image 61
mgilson Avatar answered Oct 16 '22 22:10

mgilson