Skip to content

Commit c70e89a

Browse files
committed
Add reusable --parameter decorator
1 parent 037b5a3 commit c70e89a

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

lean/click.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,23 @@ def ensure_options(options: List[str]) -> None:
404404
You are missing the following option{"s" if len(missing_options) > 1 else ""}:
405405
{''.join(help_formatter.buffer)}
406406
""".strip())
407+
408+
def parameter_option(func: FC) -> FC:
409+
"""Decorator that adds the --parameter option to Click commands.
410+
411+
This decorator can be used to add support for passing parameters via command line.
412+
413+
Example usage:
414+
@parameter_option
415+
def backtest(...):
416+
...
417+
"""
418+
func = option(
419+
"--parameter",
420+
type=(str, str),
421+
multiple=True,
422+
help="Key-value pairs to pass as backtest parameters. "
423+
"Values can be string, int, or float.\n"
424+
"Example: --parameter symbol AAPL --parameter period 10 --parameter threshold 0.05"
425+
)(func)
426+
return func

lean/commands/backtest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from typing import List, Optional, Tuple
1616
from click import command, option, argument, Choice
1717

18-
from lean.click import LeanCommand, PathParameter
18+
from lean.click import LeanCommand, PathParameter, parameter_option
1919
from lean.constants import DEFAULT_ENGINE_IMAGE, LEAN_ROOT_PATH
2020
from lean.container import container, Logger
2121
from lean.models.utils import DebuggingMethod
@@ -282,12 +282,7 @@ def _migrate_csharp_csproj(project_dir: Path) -> None:
282282
is_flag=True,
283283
default=False,
284284
help="Use the local LEAN engine image instead of pulling the latest version")
285-
@option("--parameter",
286-
type=(str, str),
287-
multiple=True,
288-
help="Key-value pairs to pass as backtest parameters. "
289-
"Values can be string, int, or float.\n"
290-
"Example: --parameter symbol AAPL --parameter period 10 --parameter threshold 0.05")
285+
@parameter_option
291286
def backtest(project: Path,
292287
output: Optional[Path],
293288
detach: bool,

lean/commands/cloud/backtest.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from typing import List, Optional, Tuple
1515
from click import command, argument, option
16-
from lean.click import LeanCommand
16+
from lean.click import LeanCommand, parameter_option
1717
from lean.container import container
1818

1919
@command(cls=LeanCommand)
@@ -27,12 +27,7 @@
2727
is_flag=True,
2828
default=False,
2929
help="Automatically open the results in the browser when the backtest is finished")
30-
@option("--parameter",
31-
type=(str, str),
32-
multiple=True,
33-
help="Key-value pairs to pass as backtest parameters. "
34-
"Values can be string, int, or float.\n"
35-
"Example: --parameter symbol AAPL --parameter period 10 --parameter threshold 0.05")
30+
@parameter_option
3631
def backtest(project: str, name: Optional[str], push: bool, open_browser: bool, parameter: List[Tuple[str, str]]) -> None:
3732
"""Backtest a project in the cloud.
3833

0 commit comments

Comments
 (0)