parser.add_argument("-s", "--start-date", dest="start_date",
default=date.today() - timedelta(days = 1),
type=date, help="Date in the format yyyymmdd")
This method gives the error
argument_test.py: error: argument -s/--start-date: invalid date value: 20181215
I assume that argument received is a string and it is incompatible as the object expected is date. So, how do I actually pass the date object? Is there a work around?
I could parse a string and pass it to a date object. But, I'd like to explore other direct options.
You can use a wrapper function that uses datetime.strptime to parse the given date string with the desired format:
parser.add_argument("-s", "--start-date", dest="start_date",
default=date.today() - timedelta(days = 1),
type=lambda d: datetime.strptime(d, '%Y%m%d').date(),
help="Date in the format yyyymmdd")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With