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

时间:2019-03-21 01:49:48

标签: regex python-3.x argparse

我正在使用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"

1 个答案:

答案 0 :(得分: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