how to print a custom message between two @click.option()?

只谈情不闲聊 提交于 2020-01-25 07:59:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!