我可以写以下内容:
import click
@click.command()
@click.option('--things', callback=lambda _,__,x: x.split(',') if x else [])
def fun(things):
print('You gave me these things: {}'.format(things))
if __name__ == '__main__':
fun()
这似乎有效,至少如果我保存它fun.py
可以运行:
$ python fun.py
You gave me these things: []
$ python fun.py --things penguins,knights,"something different"
You gave me these things: ['penguin', 'knights', 'something different']
是否有更惯用的方式来使用 Click 编写此代码,或者仅此而已?