click-contrib / click-default-group Goto Github PK
View Code? Open in Web Editor NEWExtends click.Group to invoke a command without explicit subcommand name.
License: BSD 3-Clause "New" or "Revised" License
Extends click.Group to invoke a command without explicit subcommand name.
License: BSD 3-Clause "New" or "Revised" License
python: 3.9.1
click: 7.1.2
shell: fish-shell
Just saying a quick thanks for this project. It was exactly what I needed, and I found it quickly with Google and the GitHub issue on the main repo. :)
When installing towncrier
in Python 3.11 using pip 23.0.1, I got the warning:
DEPRECATION: click-default-group is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change.
A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
This appears to be an issue with how click-default-group
handles getting itself installed.
Spotted this for my llm
command, which uses click-default-group
to set the default command to llm prompt
.
If you run this:
llm -- '--help means what?'
You get this error:
Usage: llm prompt [OPTIONS] [PROMPT]
Try 'llm prompt --help' for help.
Error: No such option: --help means what?
But if you run the full command instead:
llm prompt -- '--help means what?'
You get the expected answer:
"--help" is a command that can be used in various computer programs and ...
Bug reported here:
This project uses the BSD 3-clause license, which requires that the license be distributed with all copies of the source code:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
The PyPI source tarball does not include such a copy of the license:
> curl -fsSL 'https://files.pythonhosted.org/packages/source/c/click-default-group/click-default-group-1.2.2.tar.gz' | tar -tz
click-default-group-1.2.2/
click-default-group-1.2.2/PKG-INFO
click-default-group-1.2.2/README.md
click-default-group-1.2.2/click_default_group.egg-info/
click-default-group-1.2.2/click_default_group.egg-info/PKG-INFO
click-default-group-1.2.2/click_default_group.egg-info/SOURCES.txt
click-default-group-1.2.2/click_default_group.egg-info/dependency_links.txt
click-default-group-1.2.2/click_default_group.egg-info/requires.txt
click-default-group-1.2.2/click_default_group.egg-info/top_level.txt
click-default-group-1.2.2/click_default_group.py
click-default-group-1.2.2/setup.cfg
click-default-group-1.2.2/setup.py
That makes it difficult/impossible to legally include in third-party/OS package repositories, which require packaged code to be distributed in a license-compliant manner. It would be great if the PyPI tarball could be updated to include the license!
Is there a way I can redirect one of the subcommand --help output to the group command's --help?
For example:
@click.group(cls=DefaultGroup, default='foo', default_if_no_args=True)
def cli():
pass
@cli.command()
def foo():
click.echo('foo')
cli --help
should output foo --help
description.
Is this possible to customize --help in this way?
Let's say that I have a command with a default subcommand, when the default subcommand is implicity executed (i.e. subcommand was not typed), whatever value was used as argument is used as the subcommand name in the help string:
$ command does-not-exist -h
Usage: command does-not-exist [OPTIONS]
even if the subcommand wasn't passed, it should be part of the help string:
$ command does-not-exist -h
Usage: command subcommand [OPTIONS]
I tried to install a package that depends on this inside JupyterLite and got the following error:
Couldn't find a pure Python 3 wheel for 'click-default-group'
Steps to reproduce: navigate to https://jupyterlite.github.io/demo/lab/index.html and run the following:
import micropip
await micropip.install("sqlite-utils")
From pallets/click#430 (comment):
Using
@click.group(cls=DefaultGroup, default='shell', default_if_no_args=True)
breaks the auto-completion integration with prompt-toolkit
from click-repl.
Why is it preferred versus the following anyway (which works, and does not break click-repl?
@click.group(invoke_without_command=True)
@click.option(
'--profile',
help='profile to use',
default=u'default'
)
@click.pass_context
def cli(ctx, profile):
ctx.obj = Config(_app, profile)
if ctx.invoked_subcommand is None:
ctx.invoke(cmd_shell)
Hi I'm looking for a way to use click-default-group
while providing a cli --version
option to get the cli version. Is there a way to get cli --version
to print cli 0.0.1
?
import click
from click_default_group import DefaultGroup
VERSION = 'cli 0.0.1'
@click.group(cls=DefaultGroup, default='foo', default_if_no_args=True)
@click.option('--version', is_flag=True, default=False)
def cli(version):
click.echo(VERSION)
@cli.command()
def foo():
click.echo('foo')
@cli.command()
def bar():
click.echo('bar')
@cli.command('--version')
def cli_version():
click.echo(VERSION)
cli()
$ python cli.py --version
Usage: cli.py [OPTIONS] COMMAND [ARGS]...
Error: Missing command.
Because of the missing type hints, when using cls=DefaultGroup
it is necessary to use # type: ignore
to prevent errors like:
error: Untyped decorator makes function "my_function" untyped [misc]
So I copied the types from click 8.1.7 which makes mypy happy. I'll open the PR shortly.
As title says, a missing command error is shown when only group options are passed to the prompt.
The expected behavior is that group options are handled by the group and then default command is invoked.
import click
from click_default_group import DefaultGroup
@click.group(cls=DefaultGroup, default='foo', default_if_no_args=True)
@click.option('-v', is_flag=True)
def cli(v):
print('cli group exec')
if v:
print('Verbose!')
@cli.command()
@click.option('--config')
def foo(config):
print('foo command exec')
if config:
print(f'config={config}')
$ program foo
--> normal call, works
$ program
--> default cmd is called, works
$ program --config=abc
--> option is forwarded to 'foo' cmd, works
$ program -v --config=abc
--> '-v' option is parsed by group and '--config' is parsed by 'foo' cmd, works
$ program -v
--> doesn't work!
@llchan suggested an API better than ours at pallets/click#430 (comment). click-default-group will accept the idea.
+ /usr/bin/pytest -ra
=========================================================================== test session starts ============================================================================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.10.0, pluggy-0.13.1
benchmark: 3.4.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
Using --randomly-seed=1090834362
rootdir: /home/tkloczko/rpmbuild/BUILD/click-default-group-1.2.2, configfile: setup.cfg
plugins: forked-1.3.0, shutil-1.7.0, virtualenv-1.7.0, expect-1.1.0, flake8-1.0.7, timeout-1.4.2, betamax-0.8.1, freezegun-0.4.2, aspectlib-1.5.2, toolbox-0.5, rerunfailures-9.1.1, requests-mock-1.9.3, cov-2.12.1, flaky-3.7.0, benchmark-3.4.1, xdist-2.3.0, pylama-7.7.1, datadir-1.3.1, regressions-2.2.0, cases-3.6.3, xprocess-0.18.1, black-0.3.12, anyio-3.3.0, asyncio-0.15.1, trio-0.7.0, subtests-0.5.0, isort-2.0.0, hypothesis-6.14.6, mock-3.6.1, profiling-1.7.0, randomly-3.8.0, Faker-8.12.1, nose2pytest-1.0.8, pyfakefs-4.5.1, tornado-0.8.1, twisted-1.13.3
collected 7 items
test.py ...F... [100%]
================================================================================= FAILURES =================================================================================
___________________________________________________________________ test_default_command_with_arguments ____________________________________________________________________
def test_default_command_with_arguments():
assert r.invoke(cli, ['--foo', 'foooo']).output == 'foooo\n'
> assert 'no such option' in r.invoke(cli, ['-x']).output
E assert 'no such option' in "Usage: cli foo [OPTIONS]\nTry 'cli foo --help' for help.\n\nError: No such option: -x\n"
E + where "Usage: cli foo [OPTIONS]\nTry 'cli foo --help' for help.\n\nError: No such option: -x\n" = <Result SystemExit(2)>.output
E + where <Result SystemExit(2)> = <bound method CliRunner.invoke of <click.testing.CliRunner object at 0x7f4b7e04f850>>(cli, ['-x'])
E + where <bound method CliRunner.invoke of <click.testing.CliRunner object at 0x7f4b7e04f850>> = <click.testing.CliRunner object at 0x7f4b7e04f850>.invoke
test.py:33: AssertionError
========================================================================= short test summary info ==========================================================================
FAILED test.py::test_default_command_with_arguments - assert 'no such option' in "Usage: cli foo [OPTIONS]\nTry 'cli foo --help' for help.\n\nError: No such option: -x\n"
======================================================================= 1 failed, 6 passed in 0.56s ========================================================================
pytest-xprocess reminder::Be sure to terminate the started process by running 'pytest --xkill' if you have not explicitly done so in your fixture with 'xprocess.getinfo(<process_name>).terminate()'.
The source tarball distributed on PyPI does not include the tests:
> curl -fsSL 'https://files.pythonhosted.org/packages/source/c/click-default-group/click-default-group-1.2.2.tar.gz' | tar -tz
click-default-group-1.2.2/
click-default-group-1.2.2/PKG-INFO
click-default-group-1.2.2/README.md
click-default-group-1.2.2/click_default_group.egg-info/
click-default-group-1.2.2/click_default_group.egg-info/PKG-INFO
click-default-group-1.2.2/click_default_group.egg-info/SOURCES.txt
click-default-group-1.2.2/click_default_group.egg-info/dependency_links.txt
click-default-group-1.2.2/click_default_group.egg-info/requires.txt
click-default-group-1.2.2/click_default_group.egg-info/top_level.txt
click-default-group-1.2.2/click_default_group.py
click-default-group-1.2.2/setup.cfg
click-default-group-1.2.2/setup.py
It would be nice if it could do so, since that would enable OS-specific package repository hosts/builders to independently run tests & verify functionality/validity of these packages.
Due to the use of custom class, I discovered that I cannot add the default group to a group that is already using it:
from click_help_colors import HelpColorsGroup, HelpColorsCommand
@click.group(
cls=HelpColorsGroup,
help_headers_color='yellow'
)
Is there a workaround for this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.