Python: argparse löst einen Wertfehler aus, wenn Positionsargumente und optionale Argumente kombiniert werdenPython

Python-Programme
Anonymous
 Python: argparse löst einen Wertfehler aus, wenn Positionsargumente und optionale Argumente kombiniert werden

Post by Anonymous »

Ich versuche, die argparse-Bibliothek in Python zu verwenden, um optionale und erforderliche Argumente einzulesen. Bisher mache ich Folgendes:

Code: Select all

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()
Jetzt teste ich meinen Code und erhalte einen Wertfehler, den ich nicht verstehe:

Code: Select all

$ 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
Es gibt nur ein Positionsargument, nicht wahr, und die Ziele sind unterschiedlich? Ich verstehe den Aufwand nicht wirklich.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post