-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
feud.rename
decorator (#94)
- Loading branch information
Showing
19 changed files
with
517 additions
and
94 deletions.
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
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
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
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 |
---|---|---|
@@ -1,11 +1,162 @@ | ||
Renaming parameters | ||
=================== | ||
Renaming commands/parameters | ||
============================ | ||
|
||
TODO | ||
.. contents:: Table of Contents | ||
:class: this-will-duplicate-information-and-it-is-still-useful-here | ||
:local: | ||
:backlinks: none | ||
:depth: 3 | ||
|
||
In certain cases, it may be desirable or even necessary for the names of the | ||
commands or parameters generated by Feud to be different to the names of the | ||
Python functions (and their parameters) that were used to generate the | ||
commands. | ||
|
||
The :py:func:`.rename` operator can be used in these scenarios to rename commands | ||
or parameters. | ||
|
||
Examples | ||
-------- | ||
|
||
Defining commands or parameters with reserved keywords | ||
****************************************************** | ||
|
||
Suppose we have the following command, ``sum``, | ||
which takes a starting number ``from``, and an ending number ``to``, | ||
and sums all numbers between and including the starting and ending number. | ||
|
||
This might be called in the following way: | ||
|
||
.. code:: bash | ||
$ sum --from 1 --to 10 | ||
With generating code that might look like: | ||
|
||
.. code:: python | ||
# sum.py | ||
import feud | ||
def sum(*, from: int, to: int): | ||
"""Sums the numbers between and including a start and end number. | ||
Parameters | ||
---------- | ||
from: | ||
Starting number. | ||
to: | ||
Ending number. | ||
""" | ||
print(sum(range(from, to + 1))) | ||
if __name__ == "__main__": | ||
feud.run(sum) | ||
There are two problems here: | ||
|
||
1. By naming the function ``sum``, we are shadowing the in-built Python | ||
function ``sum``. This is also an issue as our function actually relies | ||
on the in-built Python ``sum`` function to actually do the addition. | ||
2. ``from`` is also a reserved Python keyword which is used in module imports, | ||
and cannot be used as a function parameter. | ||
|
||
We can use the :py:func:`.rename` decorator to rename both the command and parameter. | ||
|
||
.. code:: python | ||
# sum.py | ||
import feud | ||
@feud.rename("sum", from_="from") | ||
def sum_(*, from_: int, to: int): | ||
"""Sums the numbers between and including a start and end number. | ||
Parameters | ||
---------- | ||
from_: | ||
Starting number. | ||
to: | ||
Ending number. | ||
""" | ||
print(sum(range(from, to + 1))) | ||
if __name__ == "__main__": | ||
feud.run(sum_) | ||
This gives us valid Python code, and also our expected CLI behaviour. | ||
|
||
Defining hyphenated commands or parameters | ||
****************************************** | ||
|
||
Suppose we have a command that should be called in the following way: | ||
|
||
.. code:: bash | ||
$ say-hi --welcome-message "Hello World!" | ||
As Feud uses the parameter names present in the Python function signature as | ||
the parameter names for the generated CLI, this means that defining parameters | ||
with hyphens is *usually* not possible, as Python identifiers cannot have hyphens. | ||
Similarly, a function name cannot have a hyphen: | ||
|
||
.. code:: python | ||
# hyphen.py | ||
import feud | ||
def say-hi(*, welcome-message: str): | ||
print(welcome-message) | ||
if __name__ == "__main__": | ||
feud.run(say-hi) | ||
We can use the :py:func:`.rename` decorator to rename both the command and parameter. | ||
|
||
.. code:: python | ||
# hyphen.py | ||
import feud | ||
@feud.rename("say-hi", welcome_message="welcome-message") | ||
def say_hi(*, welcome_message: str): | ||
print(welcome_message) | ||
if __name__ == "__main__": | ||
feud.run(say_hi) | ||
This gives us valid Python code, and also our expected CLI behaviour. | ||
|
||
Special use case for maintaining group-level configurations | ||
*********************************************************** | ||
|
||
Although :py:func:`.command` accepts a ``name`` argument (passed to Click) that can be | ||
used to rename a command, this can sometimes be undesirable in the case of :doc:`../core/group`. | ||
|
||
In the following example, although ``show_help_defaults`` has been set to | ||
``False`` at the group level (which would usually mean that all commands | ||
defined within the group will not have their parameter defaults shown in | ||
``--help``), this has been overridden by the ``feud.command`` call which | ||
has ``show_help_defaults=True`` by default. | ||
|
||
.. code:: python | ||
class CLI(feud.Group, show_help_defaults=False): | ||
@feud.command(name="my-func") | ||
def my_func(*, opt: int = 1): | ||
pass | ||
Using ``@feud.rename("my-func")`` instead of ``@feud.command(name="my-func")`` | ||
would allow for the group-level configuration to be used, while still renaming | ||
the function. | ||
|
||
---- | ||
|
||
API reference | ||
------------- | ||
|
||
TODO | ||
.. autofunction:: feud.decorators.rename |
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
Oops, something went wrong.