python-click

How can I define the order of click sub-commands in “--help”

♀尐吖头ヾ 提交于 2020-08-24 10:39:52
问题 I have code like this: import click @click.group() def entry_point(): pass entry_point.add_command(lidtk.data.download_documents.main) entry_point.add_command(lidtk.data.create_ml_dataset.main) entry_point.add_command(lidtk.classifiers.text_cat.textcat_ngram.cli) which gives the help text: lidtk --help Usage: lidtk [OPTIONS] COMMAND [ARGS]... Options: --help Show this message and exit. Commands: create-dataset Create sharable dataset from downloaded... download Download 1000 documents of each

Click password option only if argument equals something

笑着哭i 提交于 2020-07-08 09:59:26
问题 In click, I'm defining this command: @click.command('time', short_help='Timesheet Generator') @click.argument('time_command', type=click.Choice(['this', 'last'])) @click.argument('data_mode', type=click.Choice(['excel', 'exchange']), default='exchange') @click.option('--password', prompt=True, hide_input=True, confirmation_prompt=False) @pass_context def cli(ctx, time_command, data_mode, password): The issue I have is that I only want the password to prompt if the data_mode argument equals

Click: “Got unexpected extra arguments” when passing string

邮差的信 提交于 2020-07-05 01:29:27
问题 import click @cli.command() @click.argument("namespace", nargs=1) def process(namespace): ..... @cli.command() def run(): for namespace in KEYS.iterkeys(): process(namespace) Running run('some string') produces: Error: Got unexpected extra arguments (o m e s t r i n g) As if Click passes string argument by one character. Printing an argument shows correct result. PS: KEYS dictionary defined and working as expected. 回答1: Figured this out. Instead of just calling a function, I must pass a

Specify options and arguments dynamically

我的梦境 提交于 2020-06-28 09:51:12
问题 I'd like to load arguments and options from a database. I am allowing users to define their own options and arguments. Users can call a remote api on the command line. They specify the URL and parameters to the end point. Here is what the data from database looks like [ { "name": "thename1", "short": "a", "long": "ace" "type": "string", "required": false }, { "name": "thename2", "short": "b", "long": "bravo" "type": "number", "required": true }, { "name": "thename3", "short": "c", "long":

Specify options and arguments dynamically

让人想犯罪 __ 提交于 2020-06-28 09:48:12
问题 I'd like to load arguments and options from a database. I am allowing users to define their own options and arguments. Users can call a remote api on the command line. They specify the URL and parameters to the end point. Here is what the data from database looks like [ { "name": "thename1", "short": "a", "long": "ace" "type": "string", "required": false }, { "name": "thename2", "short": "b", "long": "bravo" "type": "number", "required": true }, { "name": "thename3", "short": "c", "long":

Categorize help output in Python Click

青春壹個敷衍的年華 提交于 2020-06-16 09:49:51
问题 I am trying to figure out how to categorize commands in Click to resemble something close to the structure that kubectl uses in the way it separate commands out. For instance, in a vanilla Click help output we have: Usage: cli.py [OPTIONS] COMMAND [ARGS]... A CLI tool Options: -h, --help Show this message and exit. Commands: command1 This is command1 command2 This is command2 command3 This is command3 command4 This is command4 Instead, what would be ideal for my usage is to have a separation

Python Click: Having the group execute code AFTER a command

孤街浪徒 提交于 2020-05-26 10:49:55
问题 I have a click.group() defined, with about 10 commands in it. I understand how to use a group to run code before the code in the command, but I also want to run some code AFTER each command is run. Is that possible with click? 回答1: You can use @resultcallback decorator @click.group() def cli(): click.echo('Before command') @cli.resultcallback() def process_result(result, **kwargs): click.echo('After command') @cli.command() def command(): click.echo('Command') if __name__ == '__main__': cli()