python-click

Adding common parameters to groups with Click

社会主义新天地 提交于 2020-01-14 09:55:30
问题 I am trying to use the Python library Click, but struggle to get an example working. I defined two groups, one of which ( group2 ) is meant to handle common parameters for this group of commands. What I want to achieve is that those common parameters get processed by the group function ( group2 ) and assigned to the context variable, so they can be used by the actual commands. A use case would be a number of commands that require username and password, while some others don't (not even

python Click Return the helper menu

…衆ロ難τιáo~ 提交于 2020-01-05 10:09:15
问题 I just started using python click module and I would like to have it automatically bring up the '--help' function anytime click throws an error. test.py @click.command() @click.option('--count', default=1, help='Number of greetings.') @click.option('--name', default=Adam, help='The person to great.') def test(name): print name If I was to run the script from the command line as test.py --no_such_thing. Is there a way I could get the --help to come up instead of the normal :Error no Option -

Does click lib provide a way to print the builtin help message?

爷,独闯天下 提交于 2020-01-03 07:50:45
问题 I am using the click lib. In my code , sometimes I want to print help msg , But the only way I know is : python xxx --help But I want to print the help msg in my code using a certain function , for example: click.print_help_msg() Is there a function like this ? 回答1: You can use Command's get_help method import click @click.command() @click.option('--name', help='The person to greet.') def hello(name): """Simple program that greets NAME.""" click.echo('Hello %s!' % name) def print_help_msg

Possible to do multiple nested commands in Click 6

戏子无情 提交于 2020-01-01 06:10:31
问题 I am going to write something very basic so as to explain what I'm looking to make happen. I have written some code to do some interesting WordPress administration. The program will create instances but also create https settings for apache. What I would like it to do and where I'm having a problem: (If you run a help on the wp cli you will see exactly what I want to have happen...but I'm not a developer so I'd like some help) python3 testcommands.py --help Usage: testcommands.py [OPTIONS]

Mutually exclusive option groups in python Click

六月ゝ 毕业季﹏ 提交于 2019-12-30 03:56:47
问题 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". 回答1: 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 =

How to accept an indefinite number of options (and how to query their names/values) using click CLI framework?

╄→尐↘猪︶ㄣ 提交于 2019-12-24 17:06:13
问题 I want to pass an unlimited number of options to a click CLI. I don't know Option names either. I'm getting around this issue by using an option named conf . It accepts a string that is assumed to represent a JSON object. What I've done: @click.command() @click.option('--conf', type=str) def dummy(conf): click.echo('dummy param {}'.format(conf)) How I use it: >python main.py dummy --conf='{"foo": "bar", "fizz": "buzz"}' What I want to do: @click.command() #some magic stuff def dummy(**kwargs)

Where should I implement flask custom commands (cli)

瘦欲@ 提交于 2019-12-24 01:01:40
问题 Creating custom commands in flask needs access to the app, which is generally created in app.py like this: import click from flask import Flask app = Flask(__name__) @app.cli.command("create-user") @click.argument("name") def create_user(name): ... However, in order not to bloat my app.py, I want to put my custom commands in a separate file e.g. commands.py , but this doesn't work because the entrypoint to my project is app.py , so I'll have to import app in commands.py and import my commands

Click wouldn't let me pass multiple files, although it should be possible

若如初见. 提交于 2019-12-23 10:07:19
问题 I'm trying to use click for multiple files. For example: @cli.command("test") @click.argument('input', type=click.File('rb')) def test(input): with click.progressbar(input, label='READING') as bar: for x in bar: pass When I do something like this: script test ~/ololo/* I get: Error: Got unexpected extra arguments ( ... listing all files in folder ...) 回答1: You need to use nargs parameter. If it is set to -1, then an unlimited number of arguments is accepted: http://click.pocoo.org/6/arguments

Call a click command from code

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 07:53:35
问题 I have a function which is wrapped as a command using click. So it looks like this: @click.command() @click.option('-w', '--width', type=int, help="Some helping message", default=0) [... some other options ...] def app(width, [... some other option arguments...]): [... function code...] I have different use cases for this function. Sometimes, calling it through the command line is fine, but sometime I would also like to call directly the function from file_name import app width = 45 app(45, [

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

蓝咒 提交于 2019-12-21 07:32:07
问题 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 on!") if __name__ == "__main__": main() It'll never print "Verbose on!" even when I set the '-v' argument. My thoughts are that the log function needs