forked from taskiq-python/taskiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed CLI logic. (taskiq-python#46)
Signed-off-by: Pavel Kirilin <[email protected]>
- Loading branch information
Showing
24 changed files
with
349 additions
and
134 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from argparse import ArgumentParser | ||
from typing import Sequence | ||
|
||
from taskiq.abc.cmd import TaskiqCMD | ||
|
||
|
||
class MyCommand(TaskiqCMD): | ||
short_help = "Demo command" | ||
|
||
def exec(self, args: Sequence[str]) -> None: | ||
parser = ArgumentParser() | ||
parser.add_argument( | ||
"--test", | ||
dest="test", | ||
default="default", | ||
help="My test parameter.", | ||
) | ||
parsed = parser.parse_args(args) | ||
print(parsed) |
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,69 @@ | ||
--- | ||
order: 4 | ||
--- | ||
|
||
# CLI | ||
|
||
You can easily add new subcommands to taskiq. All default subcommands also use this mechanism, | ||
since it's easy to use. | ||
|
||
At first you need to add a class that implements `taskiq.abc.cmd.TaskiqCMD` abstract class. | ||
|
||
@[code python](../examples/extending/cli.py) | ||
|
||
In the `exec` method, you should parse incoming arguments. But since all CLI arguments to taskiq are shifted you can ignore the `args` parameter. | ||
|
||
Also, you can use your favorite tool to build CLI, like [click](https://click.palletsprojects.com/) or [typer](https://typer.tiangolo.com/). | ||
|
||
After you have such class, you need to add entrypoint that points to that class. | ||
|
||
::: tabs | ||
|
||
@tab setuptools setup.py | ||
|
||
```python | ||
from setuptools import setup | ||
|
||
setup( | ||
# ..., | ||
entry_points={ | ||
'taskiq-cli': [ | ||
'demo = my_project.cmd:MyCommand', | ||
] | ||
} | ||
) | ||
``` | ||
|
||
@tab setuptools pyproject.toml | ||
|
||
```toml | ||
[project.entry-points.taskiq-cli] | ||
demo = "my_project.cmd:MyCommand" | ||
``` | ||
|
||
@tab poetry | ||
|
||
```toml | ||
[tool.poetry.plugins.taskiq-cli] | ||
demo = "my_project.cmd:MyCommand" | ||
``` | ||
|
||
::: | ||
|
||
You can read more about entry points in [python documentation](https://packaging.python.org/en/latest/specifications/entry-points/). | ||
The subcommand name is the same as the name of the entry point you've created. | ||
|
||
|
||
```bash | ||
$ taskiq demo --help | ||
usage: demo [-h] [--test TEST] | ||
|
||
optional arguments: | ||
-h, --help show this help message and exit | ||
--test TEST My test parameter. | ||
``` | ||
|
||
```bash | ||
$ taskiq demo --test aaa | ||
Namespace(test='aaa') | ||
``` |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Sequence | ||
|
||
|
||
class TaskiqCMD(ABC): | ||
"""Base class for new commands.""" | ||
|
||
short_help = "" | ||
|
||
@abstractmethod | ||
def exec(self, args: Sequence[str]) -> None: | ||
""" | ||
Execute the command. | ||
:param args: CLI args. | ||
""" |
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
Oops, something went wrong.