我如何处理单个或多个字符串的argparse?

问题描述 投票:0回答:1

我使用argparse来检查我的输入是单个时间戳字符串还是多个逗号分隔的时间戳字符串。

例如,输入可以是"xxxx-xx-xx xx:xx:xx.xxx" or "xxxx-xx-xx xx:xx:xx.xxx,xxxx-xx-xx xx:xx:xx.xxx,xxxx-xx-xx xx:xx:xx.xxx,,,,"

顺便说一下:

parser.add_argument("-timestamp", dest="timestamp",required = True, help = "single or multiple timestamp of the format:xxxx-xx-xx xx:xx:xx.xxx, seperated by ',' ", type = is_valid_string(parser, arg))

以下是我的想法,但完全失去了如何确定它是单个字符串还是某个正则表达式类型的多个字符串(xxxx-xx-xx xx:xx:xx.xxx)

def is_valid_string(parser,arg):
    if not isinstance(arg,str):
        parser.error("\n input should be of type(str)")

编辑:

以下解决了我的问题:

 group = parser.add_mutually_exclusive_group(required=True)
 group.add_argument("-t", dest="timestamp", help = "timestamp should be of the format:xxxx-xx-xx xx:xx:xx.xxx", type = lambda x: check_timestamp(parser,x))
 group.add_argument("-T", dest="timestamps", help = "timestamps should be ',' seperated and of the format:xxxx-xx-xx xx:xx:xx.xxx", type = lambda x: is_valid_time_list(parser,x))


def is_valid_string(arg):
    if not isinstance(arg,str):
        raise TypeError("\n input should be of type(str)")

def check_valid_time(parser,arg):
    try:
        datetime.strptime(arg,'%Y-%m-%d %H:%M:%S.%f')
        print (1)
    except ValueError:
        print (2)
        parser.error("timestamp %s is not of valid time format"%(arg))
        return ValueError

def check_timestamp(parser,arg):
    is_valid_string(arg)
    check_valid_time(parser,arg)
    return arg

def is_valid_time_list(parser,arg):
    is_valid_string(arg)
    try:
        time_list = arg.split(',')
        for i in range(len(time_list)):
            print (str(time_list[i]))
            check_valid_time(parser,str(time_list[i]))
    except:
        parser.error("list is invalid input format!")
    return arg

对于以下输入格式:

python prog.py -T "2017-12-23 12:00:00.000,2017-12-23 12:00:000"
regex python-3.x argparse
1个回答
0
投票

原来使用add_mutuall_exclusive_group以及自定义错误处理程序的方式去!

group = parser.add_mutually_exclusive_group(required=True)
 group.add_argument("-t", dest="timestamp", help = "timestamp should be of the format:xxxx-xx-xx xx:xx:xx.xxx", type = lambda x: check_timestamp(parser,x))
 group.add_argument("-T", dest="timestamps", help = "timestamps should be ',' seperated and of the format:xxxx-xx-xx xx:xx:xx.xxx", type = lambda x: is_valid_time_list(parser,x))


def is_valid_string(arg):
    if not isinstance(arg,str):
        raise TypeError("\n input should be of type(str)")

def check_valid_time(parser,arg):
    try:
        datetime.strptime(arg,'%Y-%m-%d %H:%M:%S.%f')
        print (1)
    except ValueError:
        print (2)
        parser.error("timestamp %s is not of valid time format"%(arg))
        return ValueError

def check_timestamp(parser,arg):
    is_valid_string(arg)
    check_valid_time(parser,arg)
    return arg

def is_valid_time_list(parser,arg):
    is_valid_string(arg)
    try:
        time_list = arg.split(',')
        for i in range(len(time_list)):
            print (str(time_list[i]))
            check_valid_time(parser,str(time_list[i]))
    except:
        parser.error("list is invalid input format!")
    return arg
© www.soinside.com 2019 - 2024. All rights reserved.