Skip to content

Commit

Permalink
update flags to support strict/port options
Browse files Browse the repository at this point in the history
  • Loading branch information
cav71 committed Nov 14, 2024
1 parent cc4528b commit 89f5cc4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
26 changes: 18 additions & 8 deletions src/luxos/cli/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,36 @@ class type_ipaddress(ArgumentTypeBase):
options = parser.parse_args()
...
assert options.x == ("host", 9999)
assert options.x == ("1.2.3.4", 9999)
shell::
file.py -x host:9999
file.py -x 1.2.3.4:9999
"""

def __init__(self, port=None, strict=True):
super().__init__()
self.strict = strict
self.port = port

def validate(self, txt) -> None | tuple[str, None | int]:
from luxos import ips

if txt is None:
return None
try:
result = ips.parse_expr(txt) or ("", "", None)
if result[1]:
raise argparse.ArgumentTypeError("cannot use a range as expression")
return (result[0], result[2])
except ips.AddressParsingError as exc:
raise argparse.ArgumentTypeError(f"failed to parse {txt=}: {exc.args[0]}")
if txt.count(":") not in {0, 1}:
raise ValueError("too many ':' (none or one)")
if self.strict:
ip, port = ips.splitip(txt) or ("", self.port)
else:
ip, _, port = txt.partition(":")
return ip, int(port) if port else self.port
except (RuntimeError, ValueError) as exc:
raise argparse.ArgumentTypeError(
f"failed to convert to a strict ip address: {exc.args[0]}"
)


def type_range(txt: str) -> Sequence[tuple[str, int | None]]:
Expand Down
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ def port(miner_host_port):
return miner_host_port[1]


@pytest.fixture(scope="function")
def cli_generator():
from luxos.cli.shared import LuxosParserBase

class MyParser(LuxosParserBase):
def __init__(self):
super().__init__([], exit_on_error=False)

def add_argument(self, *args, **kwargs):
super().add_argument(*args, **kwargs)
return self

return lambda: MyParser()


def pytest_addoption(parser):
parser.addoption(
"--manual",
Expand Down
17 changes: 7 additions & 10 deletions tests/test_cli_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@ def test_type_range(resolver):


def test_type_hhmm():
assert flags.type_hhmm("12:34").default == datetime.time(12, 34)
assert flags.type_hhmm().validate("12:34") == datetime.time(12, 34)

pytest.raises(RuntimeError, flags.type_hhmm, "12")
pytest.raises(argparse.ArgumentTypeError, flags.type_hhmm(), "12")
with pytest.raises(argparse.ArgumentTypeError) as e:
flags.type_hhmm().validate("12")
assert e.value.args[-1] == "failed conversion into HH:MM for '12'"


def test_type_ipaddress():
assert flags.type_ipaddress("hello").default == ("hello", None)
assert flags.type_ipaddress("hello:123").default == ("hello", 123)

pytest.raises(RuntimeError, flags.type_ipaddress, "12:dwedwe")
pytest.raises(argparse.ArgumentTypeError, flags.type_ipaddress(), "12:dwedwe")
with pytest.raises(argparse.ArgumentTypeError) as e:
flags.type_hhmm().validate("hello")
assert e.value.args[-1] == "failed conversion into HH:MM for 'hello'"

0 comments on commit 89f5cc4

Please sign in to comment.