指定输入参数argparse python的格式

问题:指定输入参数argparse python的格式

我有一个需要一些命令行输入的python脚本,并且我正在使用argparse进行解析。我发现文档有点混乱,找不到在输入参数中检查格式的方法。这个示例脚本解释了我检查格式的意思:

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__)))

我需要检查选项,-s并且-e用户输入的格式为YYYY-MM-DD。我不知道argparse中有一个选项可以完成此任务。

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.


回答 0

根据文档

type关键字参数add_argument()允许执行任何必要的类型检查和类型转换… type=可以接受带有单个字符串参数并返回转换后值的任何可调用对象

您可以执行以下操作:

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

然后将其用作type

parser.add_argument("-s", 
                    "--startdate", 
                    help="The Start Date - format YYYY-MM-DD", 
                    required=True, 
                    type=valid_date)

Per the documentation:

The type keyword argument of add_argument() allows any necessary type-checking and type conversions to be performed … type= can take any callable that takes a single string argument and returns the converted value

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}'.".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)

回答 1

只是为了补充上面的答案,如果您想将lambda函数保持为单一格式,则可以使用该函数。例如:

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

旧线程,但问题至少仍然与我有关!

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!


回答 2

对于其他通过搜索引擎实现此目标的人:在Python 3.7中,您可以使用标准的.fromisoformatclass方法,而不是为ISO-8601兼容日期重新发明轮子,例如:

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)

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)