Specify format for input arguments argparse python

前端 未结 3 2058
难免孤独
难免孤独 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:19

    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!

提交回复
热议问题