-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite Commands and Groups docs. #2838
Open
Rowlando13
wants to merge
4
commits into
pallets:main
Choose a base branch
from
Rowlando13:rewrite_groups_docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
Basic Commands, Groups, Context | ||
================================ | ||
|
||
.. currentmodule:: click | ||
|
||
Commands and Groups are the building blocks for Click applications. :class:`Command` wraps a function to make it into a cli command. :class:`Group` wraps Commands and Groups to make them into applications. :class:`Context` is how groups and commands communicate. | ||
|
||
.. contents:: | ||
:depth: 1 | ||
:local: | ||
|
||
Basic Command Example | ||
---------------------- | ||
A simple command decorator takes no arguments. | ||
|
||
.. click:example:: | ||
@click.command() | ||
@click.option('--count', default=1) | ||
def hello(count): | ||
for x in range(count): | ||
click.echo("Hello!") | ||
|
||
.. click:run:: | ||
invoke(hello, args=['--count', '2',]) | ||
|
||
Renaming Commands | ||
------------------ | ||
By default the command is the function name with underscores replaced by dashes. To change this pass the desired name into the first positional argument. | ||
|
||
.. click:example:: | ||
@click.command('say-hello') | ||
@click.option('--count', default=1) | ||
def hello(count): | ||
for x in range(count): | ||
click.echo("Hello!") | ||
|
||
.. click:run:: | ||
invoke(hello, args=['--count', '2',]) | ||
|
||
Deprecating Commands | ||
--------------------- | ||
To mark a command as deprecated pass in ``deprecated=True`` | ||
|
||
.. click:example:: | ||
@click.command('say-hello', deprecated=True) | ||
@click.option('--count', default=1) | ||
def hello(count): | ||
for x in range(count): | ||
click.echo("Hello!") | ||
|
||
.. click:run:: | ||
invoke(hello, args=['--count', '2',]) | ||
|
||
Basic Group Example | ||
--------------------- | ||
A group wraps command(s). After being wrapped, the commands are nested under that group. You can see that on the help pages and in the execution. By default, invoking the group with no command shows the help page. | ||
|
||
.. click:example:: | ||
@click.group() | ||
def greeting(): | ||
click.echo('Starting greeting ...') | ||
|
||
@greeting.command('say-hello') | ||
@click.option('--count', default=1) | ||
def hello(count): | ||
for x in range(count): | ||
click.echo("Hello!") | ||
|
||
At the top level: | ||
|
||
.. click:run:: | ||
|
||
invoke(greeting) | ||
|
||
At the command level: | ||
|
||
.. click:run:: | ||
|
||
invoke(greeting, args=['say-hello']) | ||
invoke(greeting, args=['say-hello', '--help']) | ||
|
||
As you can see from the above example, the function wrapped by the group decorator executes unless it is interrupted (for example by calling the help). | ||
|
||
Renaming Groups | ||
----------------- | ||
To have a name other than the decorated function name as the group name, pass it in as the first positional argument. | ||
|
||
.. click:example:: | ||
@click.group('greet_someone') | ||
Rowlando13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def greeting(): | ||
click.echo('Starting greeting ...') | ||
|
||
@greeting.command('say-hello') | ||
@click.option('--count', default=1) | ||
def hello(count): | ||
for x in range(count): | ||
click.echo("Hello!") | ||
|
||
.. click:run:: | ||
|
||
invoke(greeting, args=['say-hello']) | ||
|
||
Group Invocation Without Command | ||
-------------------------------- | ||
|
||
By default, a group is not invoked unless a subcommand is passed. In fact, not | ||
Rowlando13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
providing a command automatically passes ``--help`` by default. This behavior | ||
can be changed by passing ``invoke_without_command=True`` to a group. In that | ||
case, the callback is always invoked instead of showing the help page. The | ||
context object also includes information about whether or not the invocation | ||
would go to a subcommand. | ||
|
||
.. click:example:: | ||
|
||
@click.group(invoke_without_command=True) | ||
@click.pass_context | ||
def cli(ctx): | ||
if ctx.invoked_subcommand is None: | ||
click.echo('I was invoked without subcommand') | ||
else: | ||
click.echo(f"I am about to invoke {ctx.invoked_subcommand}") | ||
|
||
@cli.command() | ||
def sync(): | ||
click.echo('The subcommand') | ||
|
||
.. click:run:: | ||
|
||
invoke(cli, prog_name='tool', args=[]) | ||
invoke(cli, prog_name='tool', args=['sync']) | ||
|
||
|
||
|
||
Group Separation | ||
-------------------------- | ||
Within a group, command :ref:`parameters` attached to a command belong only to that command. | ||
|
||
.. click:example:: | ||
@click.group() | ||
def greeting(): | ||
pass | ||
|
||
@greeting.command() | ||
@click.option('--count', default=1) | ||
def hello(count): | ||
for x in range(count): | ||
click.echo("Hello!") | ||
|
||
@greeting.command() | ||
@click.option('--count', default=1) | ||
def goodbye(count): | ||
for x in range(count): | ||
click.echo("Goodbye!") | ||
|
||
.. click:run:: | ||
|
||
invoke(greeting, args=['hello', '--count', '2']) | ||
invoke(greeting, args=['goodbye', '--count', '2']) | ||
invoke(greeting) | ||
|
||
Additionally parameters for a given group belong only to that group and not to the commands under it. What this means is that options and arguments for a specific command have to be specified *after* the command name itself, but *before* any other command names. | ||
|
||
This behavior is observable with the ``--help`` option. Suppose we have a group called ``tool`` containing a command called ``sub``. | ||
|
||
- ``tool --help`` returns the help for the whole program (listing subcommands). | ||
- ``tool sub --help`` returns the help for the ``sub`` subcommand. | ||
- But ``tool.py --help sub`` treats ``--help`` as an argument for the main program. Click then invokes the callback for ``--help``, which prints the help and aborts the program before click can process the subcommand. | ||
|
||
Arbitrary Nesting | ||
------------------ | ||
:class:`Commands <Command>` are attached to a :class:`Group`. Multiple groups can be attached to another group. Groups containing multiple groups can be attached to a group, and so on. To invoke a command nested under multiple groups, all the groups under which it is nested must added. | ||
|
||
.. click:example:: | ||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
# Not @click so that the group is registered now. | ||
@cli.group() | ||
def session(): | ||
click.echo('Starting session') | ||
|
||
@session.command() | ||
def initdb(): | ||
click.echo('Initialized the database') | ||
|
||
@session.command() | ||
def dropdb(): | ||
click.echo('Dropped the database') | ||
|
||
.. click:run:: | ||
|
||
invoke(cli, args=['session', 'initdb']) | ||
|
||
Lazily Attaching Commands | ||
-------------------------- | ||
Most examples so far have attached the commands and to a group immediately, but commands may be registered later. This could be used to split commands into multiple Python modules. Regardless of how they are attached, the commands are invoked identically. | ||
|
||
.. click:example:: | ||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
@click.command() | ||
def initdb(): | ||
click.echo('Initialized the database') | ||
|
||
@click.command() | ||
def dropdb(): | ||
click.echo('Dropped the database') | ||
|
||
cli.add_command(initdb) | ||
cli.add_command(dropdb) | ||
|
||
.. click:run:: | ||
|
||
invoke(cli, args=['initdb']) | ||
invoke(cli, args=['dropdb']) | ||
|
||
Context Object | ||
------------------- | ||
The :class:`Context` object is how commands and groups communicate. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copy and paste from commands doc