151

I have a Python script that requires some command line inputs and I am using argparse for parsing them. I found the documentation a bit confusing and couldn't find a way to check for a format in the input parameters. What I mean by checking format is explained with this example script:

parser.add_argument('-s', "--startdate", help="The Start Date - format YYYY-MM-DD ", required=True)
parser.add_argument('-e', "--enddate", help="The End Date format YYYY-MM-DD (Inclusive)", required=True)
parser.add_argument('-a', "--accountid", type=int, help='Account ID for the account for which data is required (Default: 570)')
parser.add_argument('-o', "--outputpath", help='Directory where output needs to be stored (Default: ' + os.path.dirname(os.path.abspath(__file__)))

I need to check for option -s and -e that the input by the user is in the format YYYY-MM-DD. Is there an option in argparse that I do not know of which accomplishes this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sohaib
  • 4,556
  • 8
  • 40
  • 68

3 Answers3

289

Per the documentation:

The type keyword argument of add_argument() allows any necessary type-checking and type conversions to be performed ... The argument to type can be any callable that accepts a single string.

You could do something like:

def valid_date(s):
    try:
        return datetime.strptime(s, "%Y-%m-%d")
    except ValueError:
        msg = "not a valid date: {0!r}".format(s)
        raise argparse.ArgumentTypeError(msg)

Then use that as type:

parser.add_argument(
    "-s", 
    "--startdate", 
    help="The Start Date - format YYYY-MM-DD", 
    required=True, 
    type=valid_date
)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
84

Just to add on to the answer above, you can use a lambda function if you want to keep it to a one-liner. For example:

parser.add_argument('--date', type=lambda d: datetime.strptime(d, '%Y%m%d'))

Old thread but the question was still relevant for me at least!

Evan V
  • 1,340
  • 10
  • 9
68

For others who hit this via search engines: in Python 3.7, you can use the standard .fromisoformat class method instead of reinventing the wheel for ISO-8601 compliant dates, e.g.:

import datetime

parser.add_argument('-s', "--startdate",
    help="The Start Date - format YYYY-MM-DD",
    required=True,
    type=datetime.date.fromisoformat)
parser.add_argument('-e', "--enddate",
    help="The End Date format YYYY-MM-DD (Inclusive)",
    required=True,
    type=datetime.date.fromisoformat)
Jason
  • 9,408
  • 5
  • 36
  • 36
Michał Górny
  • 18,713
  • 5
  • 53
  • 76
  • 9
    In addition, [`dateutil`'s](https://github.com/dateutil/dateutil) ISO parser takes a number of additional fmts that are still ISO 8601-2004 compliant. You can use `import dateutil.parser` --> `type=dateutil.parser.isoparse` or `type=dateutil.parser.parse` – Brad Solomon Oct 04 '18 at 03:16
  • @BradSolomon Indeed a good suggestion (hence my +1), but `dateutil` is a third-party package. One would have to judge its benefits against the (small) hassle of installing it extra. YMMV. – András Aszódi Feb 04 '20 at 11:08
  • Very elegant solution! – Paul P Jun 17 '21 at 20:30