Python argparse:具有可选和位置参数的互斥参数

时间:2015-12-17 14:58:31

标签: python argparse

我想用argparse库得到这个:

PROG --yesterday | begin-date [end-date]

我尝试将互斥和参数组合起来,但我没有成功。

该程序应该只接受:

PROG --yesterday
PROG 2015-11-12
PROG 2015-11-12 2015-11-15

是否可以使用argparse执行此操作?

感谢 hpaulj 。查看最终结果:

import argparse
from datetime import datetime
import pytz


def argument_date(str_date):
    try:
        return datetime.strptime(str_date, "%Y-%m-%d").replace(tzinfo=pytz.utc)
    except ValueError as e:
        raise argparse.ArgumentTypeError(e)

parser = argparse.ArgumentParser(prog='PROG')
parser.usage = """PROG [-h] [--yesterday | start [end]]"""
parser.add_argument('start', type=argument_date, nargs='?', help='Start date (format YYYY-MM-DD)')
parser.add_argument('end', type=argument_date, nargs='?', help='End date (format YYYY-MM-DD)')
parser.add_argument('--yesterday', action='store_true', help='Only yesterday')

args = parser.parse_args()

if args.yesterday and args.start:
    raise parser.error("--yesterday option is not incompatible with start argument")

if not args.yesterday and not args.start:
    raise parser.error("--yesterday option or start argument should be filled")

if args.end and (args.start >= args.end):
    raise parser.error("end argument should be granter than start")

1 个答案:

答案 0 :(得分:2)

--yesterday是多余的,因为它只是将start_date设置为昨天的快捷方式。相反,让我们昨天"是start_date的允许值。实际上,您可以根据需要概括datetime以允许其他任何参数的缩写。例如:

def argument_date(str_date):
    # Not the most efficient to roundtrip like this, but
    # fits well with your existing code
    now = datetime.datetime.utcnow().date()
    if str_date == "yesterday":
        str_date = str(now - datetime.timedelta(1))
    elif str_date == "today"
        str_date = str(now)

    try:
        return datetime.strptime(str_date, "%Y-%m-%d").replace(tzinfo=pytz.utc)
    except ValueError as e:
        raise argparse.ArgumentTypeError(e)

完成此操作后,您的代码就会变为:

parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('start', type=argument_date, help='Start date (YYYY-MM-DD, yesterday, today)')
parser.add_argument('end', type=argument_date, nargs='?', help='End date (YYYY-MM-DD, yesterday, today)')