[New-bugs-announce] [issue23884] New DateType for argparse (like FileType but for dates)

Peter Marsh report at bugs.python.org
Tue Apr 7 19:29:13 CEST 2015


New submission from Peter Marsh:

Hello,

Reasonably frequently I find myself needing to pass a date as a command line argument to a Python script I've written. Currently, argparse does not have a built support for dates - this adds a new class to argparse (much like the existing FileType) that parses arguments to datetime.date instances.

Example:

      >>> parser = argparse.ArgumentParser()
      >>> parser.add_argument('--start', type=argparse.DateType('%d/%m/%Y))
      >>> parser.add_argument('end', type=argparse.DateType())
      >>> parser.parse_args(['--start', '01/02/2015', '2015-01-03'])
      Namespace(end=datetime.date(2015, 1, 3), start=datetime.date(2015, 1, 2))


I think this would be a useful addition to the standard library, a quick Google shows that many people roll their own version of this anyway.

Support for datetime.datetime and perhaps even datetime.timedeltas might be good too, but date/times get a bit more complicated (timezones in general and varying support for the '%z' format string which is required to default to an ISO8601 date time).

Cheers,

Pete

----------
components: Library (Lib)
files: argparse_datetype.patch
keywords: patch
messages: 240220
nosy: petedmarsh
priority: normal
severity: normal
status: open
title: New DateType for argparse (like FileType but for dates)
type: enhancement
versions: Python 3.5
Added file: http://bugs.python.org/file38859/argparse_datetype.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23884>
_______________________________________


More information about the New-bugs-announce mailing list