I am using python argparse with the following argument definition:
parser.add_argument('path', nargs=1, help='File path to process')
But when I enter my command with a wildcard
argument, argparse
globs all the file paths and terminates with an error.
How do I get argparse
not to glob the files?
Optional Arguments To add an optional argument, simply omit the required parameter in add_argument() . args = parser.
By default, argparse will look for a single argument, shown above in the filename example. If you want your parameters to accept a list of items you can specify nargs=n for how many arguments to accept. Note, if you set nargs=1 , it will return as a list not a single value.
parser. add_argument('indir', type=str, help='Input dir for videos') created a positional argument. For positional arguments to a Python function, the order matters. The first value passed from the command line becomes the first positional argument. The second value passed becomes the second positional argument.
The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861.
The shell is expanding the wildcard argument before argparse
gets a chance to see it. Put quotes around the wildcard argument to prevent the shell from expanding it.
You could later perform the wildcard expansion with glob.glob.
How do I get argparse not to glob the files?
You don't.
You get the shell to stop globbing.
However. Let's think for a moment.
You're saying this in your code
parser.add_argument('path', nargs=1, help='File path to process')
But you are actually providing wild-cards when you run it.
One of those two is wrong. Either stop providing wild-cards at run time or fix argparse to allow multiple filenames.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With