Skip to content

Commit 7ee894f

Browse files
dtatarkindanfimov
andauthored
feat: add ability to specify the default WorkerArgs when parsing from the command line using the WorkerArgs.from_cli() method. (#534)
Co-authored-by: Dmitrii Anfimov <anfimov@tochka.com>
1 parent 5825b9a commit 7ee894f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

taskiq/cli/worker/args.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
1+
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser, Namespace
22
from collections.abc import Sequence
33
from dataclasses import dataclass, field
4+
from typing import Any
45

56
from taskiq.acks import AcknowledgeType
67
from taskiq.cli.common_args import LogLevel
@@ -58,11 +59,13 @@ class WorkerArgs:
5859
def from_cli(
5960
cls,
6061
args: Sequence[str] | None = None,
62+
defaults: dict[str, Any] | None = None,
6163
) -> "WorkerArgs":
6264
"""
6365
Construct TaskiqArgs instanc from CLI arguments.
6466
6567
:param args: list of args as for cli.
68+
:param defaults: default worker arguments.
6669
:return: TaskiqArgs instance.
6770
"""
6871
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
@@ -267,7 +270,10 @@ def from_cli(
267270
help="Maximum number of processes in process pool.",
268271
)
269272

270-
namespace = parser.parse_args(args)
273+
namespace = parser.parse_args(
274+
args,
275+
namespace=None if defaults is None else Namespace(**defaults),
276+
)
271277
# If there are any patterns specified, remove default.
272278
# This is an argparse limitation.
273279
if len(namespace.tasks_pattern) > 1:

0 commit comments

Comments
 (0)