Skip to content

Commit

Permalink
feat: allow custom cli ctx obj types (#1712)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Oct 31, 2023
1 parent 34aa98c commit 83c880e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
27 changes: 27 additions & 0 deletions docs/userguides/clis.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ def cmd(cli_ctx):
cli_ctx.abort(f"Bad account: {account.address}")
```

In Ape, it is easy to extend the CLI context object and use the extended version in your CLIs:

```python
from ape.cli import ApeCliContextObject, ape_cli_context
import click

class MyManager:
"""My custom manager."""

class CustomContext(ApeCliContextObject):
"""Add new managers to your custom context"""
my_manager: MyManager = MyManager()

@property
def signer(self):
"""Utilize existing managers in your custom context."""
return self.account_manager.load("my_account")

@click.command()
@ape_cli_context(obj_type=CustomContext)
def cli(cli_ctx):
# Access your manager.
print(cli_ctx.my_manager)
# Access other Ape managers.
print(cli_ctx.account_manager)
```

## Network Tools

The `@network_option()` allows you to select an ecosystem / network / provider combination.
Expand Down
2 changes: 2 additions & 0 deletions src/ape/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)
from ape.cli.commands import NetworkBoundCommand
from ape.cli.options import (
ApeCliContextObject,
account_option,
ape_cli_context,
contract_option,
Expand All @@ -33,6 +34,7 @@
"Alias",
"AllFilePaths",
"ape_cli_context",
"ApeCliContextObject",
"contract_file_paths_argument",
"contract_option",
"existing_alias_argument",
Expand Down
13 changes: 10 additions & 3 deletions src/ape/cli/options.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Dict, List, NoReturn, Optional, Union
from typing import Callable, Dict, List, NoReturn, Optional, Type, Union

import click
from ethpm_types import ContractType
Expand Down Expand Up @@ -75,7 +75,9 @@ def set_level(ctx, param, value):
}


def ape_cli_context(default_log_level: str = DEFAULT_LOG_LEVEL):
def ape_cli_context(
default_log_level: str = DEFAULT_LOG_LEVEL, obj_type: Type = ApeCliContextObject
):
"""
A ``click`` context object with helpful utilities.
Use in your commands to get access to common utility features,
Expand All @@ -84,11 +86,16 @@ def ape_cli_context(default_log_level: str = DEFAULT_LOG_LEVEL):
Args:
default_log_level (str): The log-level value to pass to
:meth:`~ape.cli.options.verbosity_option`.
obj_type (Type): The context object type. Defaults to
:class:`~ape.cli.options.ApeCliContextObject`. Sub-class
the context to extend its functionality in your CLIs,
such as if you want to add additional manager classes
to the context.
"""

def decorator(f):
f = verbosity_option(logger, default=default_log_level)(f)
f = click.make_pass_decorator(ApeCliContextObject, ensure=True)(f)
f = click.make_pass_decorator(obj_type, ensure=True)(f)
return f

return decorator
Expand Down

0 comments on commit 83c880e

Please sign in to comment.