python-click

Python Click: Having the group execute code AFTER a command

旧城冷巷雨未停 提交于 2020-05-26 10:47:09
问题 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()

python click, make option value optional

不打扰是莪最后的温柔 提交于 2020-04-06 03:29:47
问题 I am working on a small command line tool using Python 2 and click. My tool either needs to write a value, read it, or do not change it. It would be nice if I could do the following: mytool --r0=0xffff ..........Set value r0 to 0xffff mytool --r0 ......................Read value r0 mytool ...............................Don't do anything with r0 Based on the documentation, it doesn't seem possible, but I could have missed it. So is it possible or do I have to find a different approach? 回答1:

Python Click: NoSuchOption exception when manually attaching Option objects to Command instance using

。_饼干妹妹 提交于 2020-03-04 05:03:48
问题 My code sample: import click def std_cb(ctx, param, standardize): if standardize: opt = click.Option(param_decls=['-a'], help='this option only exists when -S is set') else: opt = click.Option(param_decls=['-b'], help='this option only exists when -S is not set') ctx.command.params.append(opt) return standardize @click.command() @click.option('-S', '--standardize/--no-standardize', is_eager=True, is_flag=True, default=False, callback=std_cb) def get_options(standardize, **extra_opts): print

Commands with multiple common options going into one argument using custom decorator

人盡茶涼 提交于 2020-02-20 07:48:10
问题 I would like to make a module that makes it very simple to build click commands that share a lot of options. Those options would be distilled into a single object that is passed into the command. As an illustrative example: from magic import magic_command import click @magic_command('Colored') @click.option('--color') def cmd(magic, color): pass The total command would then have many --magic-... options that go into the magic object passed into cmd . I was able to achieve that using the

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

How to add multiple blank lines to the end of the usage message produced by the Python click module?

折月煮酒 提交于 2020-01-24 21:30:26
问题 I have a question that's somewhat similar to this SO Q&A, however I want to add additional blank lines to an epilog at the end of the output generated by click. I have the following code: EPILOG='\n' + '-' * 20 class SpecialEpilog(click.Group): def format_epilog(self, ctx, formatter): if self.epilog: formatter.write_paragraph() for line in self.epilog.split('\n'): formatter.write_text(line) #------------------ @click.group(cls=SpecialEpilog, epilog=EPILOG, invoke_without_command=True) def cli

Error: Got unexpected extra argument (main-func)

拟墨画扇 提交于 2020-01-22 02:32:40
问题 I'm trying to call a function from a package module inside the command of click group. But, getting the following error: Error: Got unexpected extra argument (main-func) Updated: Let say I have a package that contains a module mod.py . The module has a click command mod_func that 'll take user input and print & return it. The package is initialized with importing the command from the module. Next, I want to import the package inside another module, let's say tool.py , and want to call mod

Instantiate Foo() class on main click group command by running subcomand with Foo() arguments

不打扰是莪最后的温柔 提交于 2020-01-16 05:11:07
问题 I want to run a click subcommand with variadic arguments that are going to be used to instantiate a class Foo(*args) on main() group command in order to create an instance of Foo() to be used by its subcommands so that it aligns with the way click works: $ python foo.py subcommand arg1 arg2 ... argN This question is based on my initial question and @StephenRauch answer. import click class Foo(object): def __init__(self, *args): self.args = args def log(self): print('self.args:', self.args)

Python multi-command CLI with common options

吃可爱长大的小学妹 提交于 2020-01-15 08:52:06
问题 I am adding CLI for my Python application. The CLI should allow to run multiple commands in a time. The commands should have common options and personal options. Example : $ python mycliapp.py --common-option1 value1 --common-option2 value2 cmd1 --cmd1-option cmd2 --cmd2-option somevalue cmd3 The example has two common options used by all commands and each command can have or not the option used by the command only. I have considered Python Click. It has rich functionality, but it does not

A command without name, in Click

半城伤御伤魂 提交于 2020-01-14 10:38:10
问题 I want to have a command line tool with a usage like this: $ program <arg> does something, no command name required $ program cut <arg> $ program eat <arg> The Click code would look like this: @click.group() def main() : pass @main.command() @click.argument('arg') def noname(arg) : # does stuff @main.command() @click.argument('arg') def cut(arg) : # cuts stuff @main.command() @click.argument('arg') def eat(arg) : # eats stuff My problem is that with this code, there is always a required