0

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!

timgeb
79.2k20 gold badges129 silver badges150 bronze badges
asked Jun 16, 2014 at 17:32
2
  • 2
    I think long arguments need two dashes - single-dash is for single-character arguments. Commented Jun 16, 2014 at 17:37
  • 1
    @ThiefMaster: nope, a single dash is just fine. Provided it is an actual ASCII dash.. Commented Jun 16, 2014 at 17:49

1 Answer 1

7

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
answered Jun 16, 2014 at 17:42
Sign up to request clarification or add additional context in comments.

3 Comments

Also, adding source encoding headers like # -*- coding: utf-8 -*- to source files that are supposed to only contain ASCII should be avoided, because they mask exactly this type of mistake.
True, but this is obviously not the case here - the OP most likely encountered a 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.
@LukasGraf: Right; I forgot this applies to byte strings too. I so rarely use non-ASCII in source files I don't think about the systax error anymore.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.