I am trying to pass some dates via argparse to my script, but somehow it always yields the following error:
error: unrecognized arguments: -startdate -enddate -timeofday
My code is the following:
import argparse
parser = argparse.ArgumentParser(description='This program downloads webcam images')
parser.add_argument('‐startdate', type=str, help='startdate given as:dd.mm.YYYY')
parser.add_argument('‐enddate', type=str, help='enddate given as:dd.mm.YYYY')
parser.add_argument('‐timeofday', type=str, help='time of day as:"HH:MM"')
args = parser.parse_args()
print args.startdate
print args.enddate
I already tried to use hyphens instead of dots as separators, as well as "real" strings instead of the numbers. Nothing helped. Any ideas? Thanks!
-
2I think long arguments need two dashes - single-dash is for single-character arguments.ThiefMaster– ThiefMaster2014年06月16日 17:37:45 +00:00Commented Jun 16, 2014 at 17:37
-
1@ThiefMaster: nope, a single dash is just fine. Provided it is an actual ASCII dash..Martijn Pieters– Martijn Pieters2014年06月16日 17:49:34 +00:00Commented Jun 16, 2014 at 17:49
1 Answer 1
You are using the wrong character:
>>> parser = argparse.ArgumentParser(description='This program downloads webcam images')
>>> parser.add_argument('‐startdate', type=str, help='startdate given as:dd.mm.YYYY')
_StoreAction(option_strings=[], dest='\xe2\x80\x90startdate', nargs=None, const=None, default=None, type=<type 'str'>, choices=None, help='startdate given as:dd.mm.YYYY', metavar=None)
Note the dest value there! That's UTF-8 for U+2010 HYPHEN; use an ASCII - minus character instead. Because the string doesn't start with parser.prefix_chars, it isn't even recognised as an optional argument, but rather as positional. As a result, option_strings is also empty. The help output tells you this too:
>>> parser.print_help()
usage: [-h] ‐startdate
This program downloads webcam images
positional arguments:
‐startdate startdate given as:dd.mm.YYYY
optional arguments:
-h, --help show this help message and exit
It is a positional (required) argument here.
Generally speaking, make sure you are not using a word processor to write code; most likely your simple ASCII - dash was replaced by your editor to a fancy ‐ HYPHEN character (note how it is rendered shorter). Such editors will also replace quotes and other punctuation with more styled versions.
If I use a regular ASCII minus character things work just fine:
>>> parser = argparse.ArgumentParser(description='This program downloads webcam images')
>>> parser.add_argument('-startdate', type=str, help='startdate given as:dd.mm.YYYY')
_StoreAction(option_strings=['-startdate'], dest='startdate', nargs=None, const=None, default=None, type=<type 'str'>, choices=None, help='startdate given as:dd.mm.YYYY', metavar=None)
>>> parser.print_help()
usage: [-h] [-startdate STARTDATE]
This program downloads webcam images
optional arguments:
-h, --help show this help message and exit
-startdate STARTDATE startdate given as:dd.mm.YYYY
3 Comments
# -*- coding: utf-8 -*- to source files that are supposed to only contain ASCII should be avoided, because they mask exactly this type of mistake.SyntaxError because of non-ASCII characters in his source, and "fixed" it by adding an encoding declaration. Otherwise he couldn't even have gotten an unrecognized arguments error.