-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Move Extending-click page from reST to MyST #3106
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
Merged
+132
−138
Merged
Changes from all commits
Commits
Show all changes
2 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 hidden or 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,132 @@ | ||
| # Extending Click | ||
|
|
||
| ```{currentmodule} click | ||
| ``` | ||
|
|
||
| In addition to common functionality that is implemented in the library itself, there are countless patterns that can be | ||
| implemented by extending Click. This page should give some insight into what can be accomplished. | ||
|
|
||
| ```{contents} | ||
| :depth: 2 | ||
| :local: true | ||
| ``` | ||
|
|
||
| (custom-groups)= | ||
|
|
||
| ## Custom Groups | ||
|
|
||
| You can customize the behavior of a group beyond the arguments it accepts by subclassing {class}`click.Group`. | ||
|
|
||
| The most common methods to override are {meth}`~click.Group.get_command` and {meth}`~click.Group.list_commands`. | ||
|
|
||
| The following example implements a basic plugin system that loads commands from Python files in a folder. The command is | ||
| lazily loaded to avoid slow startup. | ||
|
|
||
| ```python | ||
| import importlib.util | ||
| import os | ||
| import click | ||
|
|
||
| class PluginGroup(click.Group): | ||
| def __init__(self, name=None, plugin_folder="commands", **kwargs): | ||
| super().__init__(name=name, **kwargs) | ||
| self.plugin_folder = plugin_folder | ||
|
|
||
| def list_commands(self, ctx): | ||
| rv = [] | ||
|
|
||
| for filename in os.listdir(self.plugin_folder): | ||
| if filename.endswith(".py"): | ||
| rv.append(filename[:-3]) | ||
|
|
||
| rv.sort() | ||
| return rv | ||
|
|
||
| def get_command(self, ctx, name): | ||
| path = os.path.join(self.plugin_folder, f"{name}.py") | ||
| spec = importlib.util.spec_from_file_location(name, path) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module.cli | ||
|
|
||
| cli = PluginGroup( | ||
| plugin_folder=os.path.join(os.path.dirname(__file__), "commands") | ||
| ) | ||
|
|
||
| if __name__ == "__main__": | ||
| cli() | ||
| ``` | ||
|
|
||
| Custom classes can also be used with decorators: | ||
|
|
||
| ```python | ||
| @click.group( | ||
| cls=PluginGroup, | ||
| plugin_folder=os.path.join(os.path.dirname(__file__), "commands") | ||
| ) | ||
| def cli(): | ||
| pass | ||
| ``` | ||
|
|
||
| (aliases)= | ||
|
|
||
| ## Command Aliases | ||
|
|
||
| Many tools support aliases for commands. For example, you can configure `git` to accept `git ci` as alias for | ||
| `git commit`. Other tools also support auto-discovery for aliases by automatically shortening them. | ||
|
|
||
| It's possible to customize {class}`Group` to provide this functionality. As explained in {ref}`custom-groups`, a group | ||
| provides two methods: {meth}`~Group.list_commands` and {meth}`~Group.get_command`. In this particular case, you only | ||
| need to override the latter as you generally don't want to enumerate the aliases on the help page in order to avoid | ||
| confusion. | ||
|
|
||
| The following example implements a subclass of {class}`Group` that accepts a prefix for a command. If there was a | ||
| command called `push`, it would accept `pus` as an alias (so long as it was unique): | ||
|
|
||
| ```{eval-rst} | ||
| .. click:example:: | ||
|
|
||
| class AliasedGroup(click.Group): | ||
| def get_command(self, ctx, cmd_name): | ||
| rv = super().get_command(ctx, cmd_name) | ||
|
|
||
| if rv is not None: | ||
| return rv | ||
|
|
||
| matches = [ | ||
| x for x in self.list_commands(ctx) | ||
| if x.startswith(cmd_name) | ||
| ] | ||
|
|
||
| if not matches: | ||
| return None | ||
|
|
||
| if len(matches) == 1: | ||
| return click.Group.get_command(self, ctx, matches[0]) | ||
|
|
||
| ctx.fail(f"Too many matches: {', '.join(sorted(matches))}") | ||
|
|
||
| def resolve_command(self, ctx, args): | ||
| # always return the full command name | ||
| _, cmd, args = super().resolve_command(ctx, args) | ||
| return cmd.name, cmd, args | ||
| ``` | ||
|
|
||
| It can be used like this: | ||
|
|
||
| ```python | ||
|
|
||
| @click.group(cls=AliasedGroup) | ||
| def cli(): | ||
| pass | ||
|
|
||
| @cli.command | ||
| def push(): | ||
| pass | ||
|
|
||
| @cli.command | ||
| def pop(): | ||
| pass | ||
| ``` | ||
|
|
||
| See the [alias example](https://github.com/pallets/click/tree/main/examples/aliases) in Click's repository for another example. | ||
This file was deleted.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Switched to python because click:example:: code is broken with the above "It can be used like this:" text.