如何在两个@ click.option()之间打印自定义消息? [英] how to print a custom message between two @click.option()?
本文介绍了如何在两个@ click.option()之间打印自定义消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Python Click 软件包编写命令行工具.
I'm writing a command-line tool using Python Click package.
我想在两个@click.option()
之间打印自定义消息.
I want to print a custom message between two @click.option()
.
以下是我要实现的示例代码:
Here is the sample code for what I want to achieve:
import click
@click.command()
@click.option('--first', prompt='enter first input')
print('custom message') # want to print custom message here
@click.option('--second', prompt='enter second input')
def add_user(first, second):
print(first)
print(second)
add_user()
任何帮助我该怎么做?
谢谢.
推荐答案
您可以在第一个参数上使用回调:
You can use a callback on the first argument:
import click
def print_message(ctx, param, args):
print("Hi")
return args
@click.command()
@click.option('--first', prompt='enter first input', callback=print_message)
@click.option('--second', prompt='enter second input')
def add_user(first, second):
print(first)
print(second)
add_user()
$ python3.8 user.py
enter first input: one
Hi
enter second input: two
one
two
这篇关于如何在两个@ click.option()之间打印自定义消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文