问题
I'm writing a command-line tool using Python Click package.
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()
Any help how can I do this?
Thanks in advance.
回答1:
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
来源:https://stackoverflow.com/questions/59738600/how-to-print-a-custom-message-between-two-click-option