python-click

python-click: formatting help text

白昼怎懂夜的黑 提交于 2019-12-11 06:49:26
问题 This question is about the click package: Long text of help is not being displayed as desired. I tried using /b as well but does not seem to affect much. cmd and powershell both have different results for same code, why? CODE: import click def command_required_option_from_option(require_name, require_map): class CommandOptionRequiredClass(click.Command): def invoke(self, ctx): require = ctx.params[require_name] if require not in require_map: raise click.ClickException( "Unexpected value for -

Arbitrary command line keywords with Python's click library

让人想犯罪 __ 提交于 2019-12-11 04:34:14
问题 I have a command line tool built with Python's click library. I want to extend this tool to use user-defined keywords like the following: $ my-cli --foo 10 --bar 20 Normally I would add the following code to my command @click.option('--foo', type=int, default=0, ...) However in my case there are a few keywords that are user defined. I won't know that the user wants to sepcify foo or bar or something else ahead of time. One solution Currently, my best solution is to use strings and do my own

Make a command available for two groups in python click?

こ雲淡風輕ζ 提交于 2019-12-11 02:50:46
问题 I'm using click to write a cli program in Python, and I need to write something like this: import click @click.group() def root(): """root""" pass @root.group() def cli(): """test""" pass @root.group() def cli2(): """test""" pass @cli.command('test1') @cli2.command('test1') def test1(): """test2""" print 1234 return root() but this will fail with: TypeError: Attempted to convert a callback into a command twice. How can I share the command between multiple groups? 回答1: The group.command()

Click - Dynamic Defaults for Prompts based on other options

落花浮王杯 提交于 2019-12-10 20:19:17
问题 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. What I'm trying to do now is to have dynamic defaults based on another provided click option, e.g. python mymodule --param1 something --param2 somethingelse Now if param2 is empty I want to try to get a dynamic default

Python Click multiple command names

本秂侑毒 提交于 2019-12-10 18:38:45
问题 Is it possible to do something like this with Python Click? @click.command(name=['my-command', 'my-cmd']) def my_command(): pass I want my command lines to be something like: mycli my-command and mycli my-cmd but reference the same function. Do I need to do a class like AliasedGroup? 回答1: AliasedGroup is not what you are after, since it allows a shortest prefix match, and it appears you need actual aliases. But that example does provide hints in a direction that can work. It inherits from

Python Click - only execute subcommand if parent command executed successfully

天涯浪子 提交于 2019-12-10 14:57:12
问题 I'm using Click to build a Python CLI and am running into an issue with how exceptions are handles in Click. I'm not sure about the wording ("subcommand", "parentcommand") here but from my example you'll get the idea I hope. Let's assume this code: @click.group() @click.option("--something") def mycli(something): try: #do something with "something" and set ctx ctx.obj = {} ctx.obj["somevar"] = some_result except: print("Something went wrong") raise #only if everything went fine call mycommand

Python Click: custom error message

大憨熊 提交于 2019-12-10 02:58:56
问题 I use the excellent Python Click library for handling command line options in my tool. Here's a simplified version of my code (full script here): @click.command( context_settings = dict( help_option_names = ['-h', '--help'] ) ) @click.argument('analysis_dir', type = click.Path(exists=True), nargs = -1, required = True, metavar = "<analysis directory>" ) def mytool(analysis_dir): """ Do stuff """ if __name__ == "__main__": mytool() If someone runs the command without any flags, they get the

Pyinstaller on a setuptools package

早过忘川 提交于 2019-12-10 02:11:45
问题 I'm attempting to run PyInstaller on a CLI app I am building in Python using the Click library. I'm having trouble building the project using PyInstaller. PyInstaller has a document in their GitHub wiki titled Recipe Setuptools Entry Point, which gives information about how to use PyInstaller with a setuptools package, which I'm using for this project. However, it seems it cannot find the base module when I run pyinstaller --onefile main.spec . My question is: Is the problem simply an issue

Creating a Click.Option with prompt that shows only if default value is empty

泪湿孤枕 提交于 2019-12-08 05:22:26
问题 Given the following option: @cli.command() @click.option('--param', default=lambda: get_value(), prompt="Enter Param") Normal behavior is for click to show a prompt to enter a value for param and display the default value (and you can just ENTER through it to preserve that). Instead I'd like the param prompt to only show if get_value() returns None or some pre-defined "show" value, but for any Truthy/Other value for get_value() click will not show the prompt for this option and run the

python click subcommand unified error handling

倾然丶 夕夏残阳落幕 提交于 2019-12-06 04:16:04
问题 In the case where there are command groups and every sub-command may raise exceptions, how can I handle them all together in one place? Given the example below: import click @click.group() def cli(): pass @cli.command() def foo(): pass if __name__ == '__main__': cli() Both cli and foo may raise. I know that one possible solution is to place try-except around cli() in the if clause. But that does not work when you distribute a package. In setup.py , you have to specify an entry point (in this