CLI
...
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.
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)
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 or typer.
After you have such class, you need to add entrypoint that points to that class.
setuptools setup.py
from setuptools import setup
setup(
# ...,
entry_points={
'taskiq_cli': [
'demo = my_project.cmd:MyCommand',
]
}
)
setuptools pyproject.toml
[project.entry-points.taskiq_cli]
demo = "my_project.cmd:MyCommand"
poetry
[tool.poetry.plugins.taskiq_cli]
demo = "my_project.cmd:MyCommand"
You can read more about entry points in python documentation. The subcommand name is the same as the name of the entry point you've created.
$ taskiq demo --help
usage: demo [-h] [--test TEST]
optional arguments:
-h, --help show this help message and exit
--test TEST My test parameter.
$ taskiq demo --test aaa
Namespace(test='aaa')