点击类型转换

时间:2017-02-08 09:35:50

标签: python command-line-interface python-click

我正在使用click来定义一个带有datetime的CLI和一个逗号分隔的参数列表。

import click

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

split_parameter = lambda _, __, s: s.split(",")
check_date = lambda _, __, s: valid_date(s)


@click.command()
@click.argument('symbols', callback=split_parameter)
@click.option('--start_date', callback=check_date)
@click.option('--end_date', callback=check_date)
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
     return None

它有效,但围绕callback s的逻辑有点尴尬。拆分列表并转换为datetime都会中断简单的单击结构。有这样做的pythonic方法吗?

2 个答案:

答案 0 :(得分:3)

您可以将丑陋的lambda _, __, x部分封装在装饰器中:

import click
from datetime import datetime as dt

def click_callback(f):
    return lambda _, __, x: f(x)

def check_date(s):
    try:
        return dt.strptime(s, "%Y-%m-%d")
    except ValueError:
        msg = "Not a valid date: '{0}'.".format(s)
        raise Exception(msg)
    except TypeError:
        return None

@click.command()
@click.argument('symbols', callback=click_callback(lambda s: s.split(',')))
@click.option('--start_date', callback=click_callback(check_date))
@click.option('--end_date', callback=click_callback(check_date))
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
    print(symbols, start_date, end_date, file_name)

if __name__ == '__main__':
    f()

或者像这样(只显示相关部分):

def click_callback(f):
    return {'callback': lambda _, __, x: f(x)}

@click.command()           #  vvvvvvvvvvvvvvvv
@click.argument('symbols',    **click_callback(lambda s: s.split(',')))
@click.option('--start_date', **click_callback(check_date))
@click.option('--end_date',   **click_callback(check_date))
@click.option('--file_name')
def f(symbols, start_date, end_date, file_name):
    print(symbols, start_date, end_date, file_name)

答案 1 :(得分:0)

我希望日期字符串采用'05 -Jul-2018'这样的格式,以下内容对我有用

def check_date(ctx, param, value):
    try:
        _ = dt.strptime(value, "%Y-%m-%d")
        return value
    except ValueError:
        msg = "Not a valid date: '{0}'.".format(value)
        raise Exception(msg)

def getDate():
    return '09-Jul-2018' #you can have any code here

@click.command()
@click.option('--start_date', callback=check_date, default=lambda :getDate())
def f(symbols, start_date):
    print(symbols, start_date)

if __name__ == '__main__':
    f()

您可以找到更多帮助Here