python-click

Is it possible to reuse python @click.option decorators for multiple commands?

做~自己de王妃 提交于 2019-12-03 07:27:07
I have two Python CLI tools which share a set of common click.options. At the moment, the common options are duplicated: @click.command() @click.option('--foo', is_flag=True) @click.option('--bar', is_flag=True) @click.option('--unique-flag-1', is_flag=True) def command_one(): pass @click.command() @click.option('--foo', is_flag=True) @click.option('--bar', is_flag=True) @click.option('--unique-flag-2', is_flag=True) def command_two(): pass Is it possible to extract the common options in to a single decorator that can be applied to each function? You can build your own decorator that

Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment

﹥>﹥吖頭↗ 提交于 2019-12-03 01:18:28
I downloaded Quokka Python/Flask CMS to a CentOS7 server. Everything works fine with command sudo python3 manage.py runserver --host 0.0.0.0 --port 80 Then I create a file /etc/init.d/quokkacms. The file contains following code start() { echo -n "Starting quokkacms: " python3 /var/www/quokka/manage.py runserver --host 0.0.0.0 --port 80 touch /var/lock/subsys/quokkacms return 0 } stop() { echo -n "Shutting down quokkacms: " rm -f /var/lock/subsys/quokkacms return 0 } case "$1" in start) start ;; stop) stop ;; status) ;; restart) stop start ;; *) echo "Usage: quokkacms {start|stop|status|restart

Click: how do I apply an action to all commands and subcommands but allow a command to opt-out?

给你一囗甜甜゛ 提交于 2019-12-02 03:54:46
I have a case where I'd like to automatically run a common function, check_upgrade() , for most of my click commands and sub-commands, but there are a few cases where I don't want to run it. I was thinking I could have a decorator that one can add (e.g. @bypass_upgrade_check ) for commands where check_upgrade() should not run. I was hoping for something like (thanks Stephen Rauch for the initial idea): def do_upgrade(): print "Performing upgrade" def bypass_upgrade_check(func): setattr(func, "do_upgrade_check", False) return func @click.group() @click.pass_context def common(ctx): sub_cmd =

Python Click Library Rename Argument

纵然是瞬间 提交于 2019-12-01 19:19:05
I am using the Click library but I can't seem to find a behavior similar to dest from argparse . For example, I have @click.option('--format', type=click.Choice(['t', 'j'])) def plug(format): pass Notice that I am using a flag with --format that gets translated into a built-in Python construct format which is not ideal. Is there a way to change the argument passed into the click function for options? While Click doesn't have dest -equivalent of argparse , it has certain argument-naming behavior which can be exploited. Specifically, for parameters with multiple possible names, it will prefer

Python Click Library Rename Argument

淺唱寂寞╮ 提交于 2019-12-01 17:38:41
问题 I am using the Click library but I can't seem to find a behavior similar to dest from argparse . For example, I have @click.option('--format', type=click.Choice(['t', 'j'])) def plug(format): pass Notice that I am using a flag with --format that gets translated into a built-in Python construct format which is not ideal. Is there a way to change the argument passed into the click function for options? 回答1: While Click doesn't have dest -equivalent of argparse , it has certain argument-naming

Mutually exclusive option groups in python Click

断了今生、忘了曾经 提交于 2019-11-30 17:28:36
How can I create a mutually exclusive option group in Click? I want to either accept the flag "--all" or take an option with a parameter like "--color red". I ran into this same use case recently; this is what I came up with. For each option, you can give a list of conflicting options. from click import command, option, Option, UsageError class MutuallyExclusiveOption(Option): def __init__(self, *args, **kwargs): self.mutually_exclusive = set(kwargs.pop('mutually_exclusive', [])) help = kwargs.get('help', '') if self.mutually_exclusive: ex_str = ', '.join(self.mutually_exclusive) kwargs['help'

Call another click command from a click command

这一生的挚爱 提交于 2019-11-30 17:23:59
I want to use some useful functions as commands. For that I am testing the click library. I defined my three original functions then decorated as click.command : import click import os, sys @click.command() @click.argument('content', required=False) @click.option('--to_stdout', default=True) def add_name(content, to_stdout=False): if not content: content = ''.join(sys.stdin.readlines()) result = content + "\n\tadded name" if to_stdout is True: sys.stdout.writelines(result) return result @click.command() @click.argument('content', required=False) @click.option('--to_stdout', default=True) def

Get params sent to a subcommand of a click.group()

ε祈祈猫儿з 提交于 2019-11-30 15:17:08
If I have a click.group() with multiple sub-commands, is there a way that I can get the command line arguments passed to those sub-commands within the group itself? I know you can go from the group downwards via the context , and I know that I can use a callback function that will execute before the command, but I didn't know if there was a better way to do this than use a callback . An example: @click.group() def cli(): pass @cli.command() @click.argument('task') @click.argument('task_id') def sync(task, task_id): click.echo('Synching: {}'.format(task)) In this example, is there any way to

How can I split my Click commands, each with a set of sub-commands, into multiple files?

守給你的承諾、 提交于 2019-11-30 10:25:20
问题 I have one large click application that I've developed, but navigating through the different commands/subcommands is getting rough. How do I organize my commands into separate files? Is it possible to organize commands and their subcommands into separate classes? Here's an example of how I would like to separate it: init import click @click.group() @click.version_option() def cli(): pass #Entry Point command_cloudflare.py @cli.group() @click.pass_context def cloudflare(ctx): pass @cloudflare

Call another click command from a click command

被刻印的时光 ゝ 提交于 2019-11-30 00:54:20
问题 I want to use some useful functions as commands. For that I am testing the click library. I defined my three original functions then decorated as click.command : import click import os, sys @click.command() @click.argument('content', required=False) @click.option('--to_stdout', default=True) def add_name(content, to_stdout=False): if not content: content = ''.join(sys.stdin.readlines()) result = content + "\n\tadded name" if to_stdout is True: sys.stdout.writelines(result) return result