Click and pylint

The @click.command decorator edits your functions parameters, but pylint does not know this, since it does not actually run your code. I don’t think it makes sense to make your code weird just so pylint is happy. Instead, ignore it, or add a comment to disable that warning in the current scope: # pylint: disable=no-value-for-parameter

Click Command Line Interfaces: Make options required if other optional option is unset

This can be done by building a custom class derived from click.Option, and in that class over riding the click.Option.handle_parse_result() method like: Custom Class: import click class NotRequiredIf(click.Option): def __init__(self, *args, **kwargs): self.not_required_if = kwargs.pop(‘not_required_if’) assert self.not_required_if, “‘not_required_if’ parameter required” kwargs[‘help’] = (kwargs.get(‘help’, ”) + ‘ NOTE: This argument is mutually exclusive with %s’ % … Read more

Shared options and flags between commands

I have found a simple solution! I slightly edited the snippet from https://github.com/pallets/click/issues/108 : import click _cmd1_options = [ click.option(‘–cmd1-opt’) ] _cmd2_options = [ click.option(‘–cmd2-opt’) ] def add_options(options): def _add_options(func): for option in reversed(options): func = option(func) return func return _add_options @click.group() def group(**kwargs): pass @group.command() @add_options(_cmd1_options) def cmd1(**kwargs): print(kwargs) @group.command() @add_options(_cmd2_options) def cmd2(**kwargs): print(kwargs) … Read more

Call a click command from code

You can call a click command function from regular code by reconstructing the command line from parameters. Using your example it could look somthing like this: call_click_command(app, width, [… other arguments …]) Code: def call_click_command(cmd, *args, **kwargs): “”” Wrapper to call a click command :param cmd: click cli command function to call :param args: arguments … Read more

nargs=* equivalent for options in Click

One way to approach what you are after is to inherit from click.Option, and customize the parser. Custom Class: import click class OptionEatAll(click.Option): def __init__(self, *args, **kwargs): self.save_other_options = kwargs.pop(‘save_other_options’, True) nargs = kwargs.pop(‘nargs’, -1) assert nargs == -1, ‘nargs, if set, must be -1 not {}’.format(nargs) super(OptionEatAll, self).__init__(*args, **kwargs) self._previous_parser_process = None self._eat_all_parser = … Read more

how to debug python click cli application?

This is not well documented, but you can call your command functions directly, and thus can run the code in a debugger: Sample Code: import click @click.command() @click.option(‘–my_arg’, default=1, help=’a number’) def my_command(my_arg): click.echo(“my_arg=’%d'” % my_arg) if __name__ == ‘__main__’: my_command([‘–my_arg’, ‘3’]) Result: my_arg=’3′

Why does my use of click.argument produce “got an unexpected keyword argument ‘help’?

I run into this again and again because the trace output does not correspond to the parameter causing the error. Notice ArgumentClass in the trace, that’s your hint. ‘help’ is an acceptable parameter to @click.option. The click library prefers, however, that you document your own arguments. The @click.argument help parameter is causing this exception. This … Read more

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

You can build your own decorator that encapsulates the common options: def common_options(function): function = click.option(‘–unique-flag-1’, is_flag=True)(function) function = click.option(‘–bar’, is_flag=True)(function) function = click.option(‘–foo’, is_flag=True)(function) return function @click.command() @common_options def command(): pass

Using Boolean Flags in Python Click Library (command line arguments)

So click is not simply a command line parser. It also dispatches and processes the commands. So in your example, the log() function never returns to main(). The intention of the framework is that the decorated function, ie: log(), will do the needed work. Code: import click @click.command() @click.option(‘–verbose’, ‘-v’, is_flag=True, help=”Print more output.”) def … Read more

Mutually exclusive option groups in python Click

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’] = help + … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)