python-click

Divide click commands into sections in cli documentation

Divide click commands into sections in cli documentation Question: This code: #!/usr/bin env python3 import click def f(*a, **kw): print(a, kw) commands = [click.Command(“cmd1”, callback=f), click.Command(“cmd2”, callback=f)] cli = click.Group(commands={c.name: c for c in commands}) if __name__ == “__main__”: cli() generates this help: # Usage: cli.py [OPTIONS] COMMAND [ARGS]… # Options: # –help Show this …

Total answers: 2

click custom option prompt function

click custom option prompt function Question: I have noticed that prompt using click accepts inputs with trailing spaces ftp_server = click.prompt(“FTP Server”) Is there a way to use a custom return function like this to reject trailing spaces? def custom_prompt(value): if value.strip(): return True else: return False ftp_server = click.prompt(“FTP Server”, custom_prompt) I have already …

Total answers: 1

python click set allowable values for option

python click set allowable values for option Question: I have created a Click command that will copy files from source to destination The command accepts 3 parameters : 1 – Source of files 2 – Destination of files 3 – Transfer mode (local,ftp) import click @click.group() def cli(): pass @cli.command() @click.argument(‘source’) @click.argument(‘destination’) @click.option(‘–mode’, required = …

Total answers: 1

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

Using Boolean Flags in Python Click Library (command line arguments) Question: I’m trying to make a verbose flag for my Python program. Currently, I’m doing this: import click #global variable verboseFlag = False #parse arguments @click.command() @click.option(‘–verbose’, ‘-v’, is_flag=True, help=”Print more output.”) def log(verbose): global verboseFlag verboseFlag = True def main(): log() if verboseFlag: print(“Verbose …

Total answers: 2

A command without name, in Click

A command without name, in Click Question: 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) : # …

Total answers: 4

Click – Dynamic Defaults for Prompts based on other options

Click – Dynamic Defaults for Prompts based on other options Question: I’m using Click to build a CLI interface. Click offers dynamic defaults for prompts, which is great. Also this example gives some insights on how to implement dynamic defaults using a custom click-class and thus provide more flexible options when evaluating the default value. …

Total answers: 2

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

Is it possible to reuse python @click.option decorators for multiple commands? Question: 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 …

Total answers: 4

Click and pylint

Click and pylint Question: Here is a simple example of click usage that causes pylint error: @click.command() @click.option(‘–option’, is_flag=True) def foo(option): click.echo(option) foo() foo receives no arguments so I’m getting E1120 (no-value-for-parameter). So I did this: @click.command() @click.option(‘–option’, is_flag=True) def foo(**kwargs): click.echo(kwargs[“option”]) foo() Is there a better way? Or a way to disable pylint for …

Total answers: 5

nargs=* equivalent for options in Click

nargs=* equivalent for options in Click Question: Is there an equivalent to argparse‘s nargs=’*’ functionality for optional arguments in Click? I am writing a command line script, and one of the options needs to be able to take an unlimited number of arguments, like: foo –users alice bob charlie –bar baz So users would be …

Total answers: 4