I'm trying to use the argparse library in python to read in optional and required arguments. So far I'm doing this:
import argparse
parser = argparse.ArgumentParser(description='Cleanup Script for Folder')
parser.add_argument('PATH_TO_WORKDIR_ROOT',
type=str,
dest='PATH_TO_WORKDIR_ROOT',
action='store',
help='(absolute or) relative path to directory \n to work on, e.g. "..\MyFolder\\"')
parser.add_argument('--PATH_TO_BACKUP_ROOT',
type=str,
dest='PATH_TO_BACKUP_ROOT',
action='store',
help='(absolute or) relative path to Backup-Directory \n default is ".\BACKUP\"')
args = parser.parse_args()
Now I'm testing my code and it gives me a value-error that I don't understand:
$ python argparsetest.py --help
Traceback (most recent call last):
File "argparsetest.py", line 5, in <module>
parser.add_argument('PATH_TO_WORKDIR_ROOT', type=str, dest='PATH_TO_WORKDIR_ ROOT', action='store', help='(absolute or)
relative path to directory \n to wo rk on, e.g. "..\MyFolder\\"')
File "C:\Program
Files\Enthought\Canopy\App\appdata\canopy-1.3.0.1715.win-x86_
64\lib\argparse.py", line 1262, in add_argument
raise ValueError('dest supplied twice for positional argument') ValueError: dest supplied twice for positional argument
There's only one positional argument, isn't there and the destinations are different. I don't really understand the hassle :)
Thanks a lot in advance!
Optional Arguments To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.
Python argparse optional argument The example adds one argument having two options: a short -o and a long --ouput . These are optional arguments. The module is imported. An argument is added with add_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.
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.
Look at the following code in argparse.py:
# =======================
# Adding argument actions
# =======================
def add_argument(self, *args, **kwargs):
"""
add_argument(dest, ..., name=value, ...)
add_argument(option_string, option_string, ..., name=value, ...)
"""
# if no positional args are supplied or only one is supplied and
# it doesn't look like an option string, parse a positional
# argument
chars = self.prefix_chars
if not args or len(args) == 1 and args[0][0] not in chars:
if args and 'dest' in kwargs:
raise ValueError('dest supplied twice for positional argument')
kwargs = self._get_positional_kwargs(*args, **kwargs)
Since you did not precede --
to PATH_TO_WORKDIR_ROOT it thinks that the first argument is dest
therefore raising the Error when you supply dest
again as a named argument.
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