python: argparse throwing value error when combining positional and optional argument

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 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!


ANSWERS:


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.



 MORE:


 ? Rolling windows in Multi-index Pandas Dataframe
 ? re.sub() does not keep blanks and new lines
 ? Python:Matplotlib update scatter graph in real time
 ? python http handler broken pipe exception
 ? Is it possible to import variables from __init__.py in views.py?
 ? Extract data from STATIC HTML FILE using python 3.5
 ? How to solve in Ionic app “Your APK's version code needs to be higher than 20000”
 ? Javascript/Cordova - Streaming data from NTRIP Caster to Bluetooth device
 ? Cordova InAppBrowser and FaceBook App Event Logger
 ? Cordova app build failed - configuring root project 'android' error message