Skip to content

Commit

Permalink
feat: allow argument overrides (#1723)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 3, 2023
1 parent bea735d commit 848bad2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/ape/cli/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,30 @@ def _alias_callback(ctx, param, value):
return value


def existing_alias_argument(account_type: _ACCOUNT_TYPE_FILTER = None):
def existing_alias_argument(account_type: _ACCOUNT_TYPE_FILTER = None, **kwargs):
"""
A ``click.argument`` for an existing account alias.
Args:
account_type (Type[:class:`~ape.api.accounts.AccountAPI`], optional):
If given, limits the type of account the user may choose from.
**kwargs: click.argument overrides.
"""

return click.argument("alias", type=Alias(account_type=account_type))
type_ = kwargs.pop("type", Alias(account_type=account_type))
return click.argument("alias", type=type_, **kwargs)


def non_existing_alias_argument():
def non_existing_alias_argument(**kwargs):
"""
A ``click.argument`` for an account alias that does not yet exist in ape.
Args:
**kwargs: click.argument overrides.
"""

return click.argument("alias", callback=_alias_callback)
callback = kwargs.pop("callback", _alias_callback)
return click.argument("alias", callback=callback, **kwargs)


def _create_contracts_paths(ctx, param, value):
Expand Down
52 changes: 52 additions & 0 deletions tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
PromptChoice,
account_option,
contract_file_paths_argument,
existing_alias_argument,
get_user_selected_account,
network_option,
non_existing_alias_argument,
verbosity_option,
)
from ape.exceptions import AccountsError
Expand Down Expand Up @@ -346,3 +348,53 @@ def cmd(file_paths):

result = runner.invoke(cmd, ["path0", "path1"])
assert "Contract 'path0' not found" in result.output


def test_existing_alias_option(runner):
@click.command()
@existing_alias_argument()
def cmd(alias):
click.echo(alias)

result = runner.invoke(cmd, ["TEST::0"])
assert "TEST::0" in result.output


def test_existing_alias_option_custom_callback(runner):
magic_value = "THIS IS A TEST"

def custom_callback(*args, **kwargs):
return magic_value

@click.command()
@existing_alias_argument(callback=custom_callback)
def cmd(alias):
click.echo(alias)

result = runner.invoke(cmd, ["TEST::0"])
assert magic_value in result.output


def test_non_existing_alias_option(runner):
@click.command()
@non_existing_alias_argument()
def cmd(alias):
click.echo(alias)

result = runner.invoke(cmd, ["non-exists"])
assert "non-exists" in result.output


def test_non_existing_alias_option_custom_callback(runner):
magic_value = "THIS IS A TEST"

def custom_callback(*args, **kwargs):
return magic_value

@click.command()
@non_existing_alias_argument(callback=custom_callback)
def cmd(alias):
click.echo(alias)

result = runner.invoke(cmd, ["non-exists"])
assert magic_value in result.output

0 comments on commit 848bad2

Please sign in to comment.