python-click

Creating a shell command line application with Python and Click

耗尽温柔 提交于 2019-12-20 12:36:44
问题 I'm using click (http://click.pocoo.org/3/) to create a command line application, but I don't know how to create a shell for this application. Suppose I'm writing a program called test and I have commands called subtest1 and subtest2 I was able to make it work from terminal like: $ test subtest1 $ test subtest2 But what I was thinking about is a shell, so I could do: $ test >> subtest1 >> subtest2 Is this possible with click? 回答1: This is not impossible with click, but there's no built-in

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

白昼怎懂夜的黑 提交于 2019-12-20 03:51:25
问题 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,

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

半城伤御伤魂 提交于 2019-12-18 16:51:44
问题 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')

How do I pass variables to other methods using Python's click (Command Line Interface Creation Kit) package

那年仲夏 提交于 2019-12-18 12:18:31
问题 I know it's new, but I like the look of click a lot and would love to use it, but I can't work out how to pass variables from the main method to other methods. Am I using it incorrectly, or is this functionality just not available yet? Seems pretty fundamental, so I'm sure it will be in there, but this things only been out a little while so maybe not. import click @click.option('--username', default='', help='Username') @click.option('--password', default='', help='Password') @click.group()

How do I pass variables to other methods using Python's click (Command Line Interface Creation Kit) package

帅比萌擦擦* 提交于 2019-12-18 12:18:11
问题 I know it's new, but I like the look of click a lot and would love to use it, but I can't work out how to pass variables from the main method to other methods. Am I using it incorrectly, or is this functionality just not available yet? Seems pretty fundamental, so I'm sure it will be in there, but this things only been out a little while so maybe not. import click @click.option('--username', default='', help='Username') @click.option('--password', default='', help='Password') @click.group()

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

自古美人都是妖i 提交于 2019-12-18 01:12:07
问题 When writing a command-line interface (CLI) with the Python click library, is it possible to define e.g. three options where the second and third one are only required if the first (optional) one was left unset? My use case is a log-in system which allows me to authenticate either via an authentication token (option 1), or, alternatively, via username (option 2) and password (option 3). If the token was given, there is no need to check for username and password being defined or prompting them

Use python click command with a class with variadic arguments

房东的猫 提交于 2019-12-13 01:28:10
问题 I have a class that gets initialized with a previously unknown number of arguments and I want it to be done on cli using python click package. My issue is that I can't manage to initialize it and run a click command: $ python mycode.py arg1 arg2 ... argN click_command because with variadic arguments option nargs=-1 click don't recognize the click_command as a command. How can I input N args and then run a command using click? Setting a defined number of arguments, like nargs=5 , solves the

python click app is failing with indication of “missing argument” but cannot locate the issue

流过昼夜 提交于 2019-12-13 00:32:06
问题 I am creating an app with Click in python 3.6. I seem to have an error that I cannot find. I have posted my code below. The problem is that when I type: python clickapp.py --help all is good. But when I type python clickapp.py simplerequest --help I get an error indicating that I am missing the argument "directory", but there is no traceback to indicate the source of the error. The exact output is: Usage: clickapp.py [OPTIONS] STARTDATE ENDDATE DIRECTORY COMMAND [ARGS]... Error: Missing

Is it possible to add a global argument for all subcommands in Click based interfaces?

好久不见. 提交于 2019-12-12 08:48:53
问题 I am using Click under a virtualenv and use the entry_point directive in setuptools to map the root to a function called dispatch. My tool exposes two subcommands serve and config , I am using an option on the top level group to ensure that the user always passes a --path directive. However the usage turns out as follows: mycommand --path=/tmp serve both the serve and config sub commands need to ensure that the user always passes a path in and ideally I would like to present the cli as:

How do I create an exception to a group action in click

五迷三道 提交于 2019-12-11 08:39:48
问题 I have a case where I'd like to run a common function ( check_upgrade() ) for most of my click commands, but there are a few cases where I don't want to run it. Rather than relying on developers to remember to add a decorator or function call to explicitly run this check, I'd prefer to instead run it by default and then 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: class State(object): def _