Specify format for input arguments argparse python

前端 未结 3 2056
难免孤独
难免孤独 2020-12-02 11:49

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

3条回答
  •  借酒劲吻你
    2020-12-02 12:23

    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.:

    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)
    

提交回复
热议问题