Add ArgumentAction.STORE_BOOL_OPTIONAL, Add BreakChainSignal

This commit is contained in:
2025-07-14 21:59:12 -04:00
parent 9654b9926c
commit 68d7d89d64
15 changed files with 348 additions and 62 deletions

View File

@ -1,6 +1,6 @@
import pytest
from falyx.action import Action, SelectionAction
from falyx.action import Action
from falyx.exceptions import CommandArgumentError
from falyx.parser import ArgumentAction, CommandArgumentParser
@ -217,11 +217,3 @@ async def test_action_with_default_and_value_positional():
with pytest.raises(CommandArgumentError):
await parser.parse_args(["be"])
# @pytest.mark.asyncio
# async def test_selection_action():
# parser = CommandArgumentParser()
# action = SelectionAction("select", selections=["a", "b", "c"])
# parser.add_argument("--select", action=ArgumentAction.ACTION, resolver=action)
# args = await parser.parse_args(["--select"])

View File

@ -8,4 +8,4 @@ def test_argument_action():
assert action != "invalid_action"
assert action.value == "append"
assert str(action) == "append"
assert len(ArgumentAction.choices()) == 8
assert len(ArgumentAction.choices()) == 9

View File

@ -0,0 +1,83 @@
import pytest
from falyx.exceptions import CommandArgumentError
from falyx.parser import ArgumentAction, CommandArgumentParser
@pytest.mark.asyncio
async def test_store_bool_optional_true():
parser = CommandArgumentParser()
parser.add_argument(
"--debug",
action=ArgumentAction.STORE_BOOL_OPTIONAL,
help="Enable debug mode.",
)
args = await parser.parse_args(["--debug"])
assert args["debug"] is True
@pytest.mark.asyncio
async def test_store_bool_optional_false():
parser = CommandArgumentParser()
parser.add_argument(
"--debug",
action=ArgumentAction.STORE_BOOL_OPTIONAL,
help="Enable debug mode.",
)
args = await parser.parse_args(["--no-debug"])
assert args["debug"] is False
@pytest.mark.asyncio
async def test_store_bool_optional_default_none():
parser = CommandArgumentParser()
parser.add_argument(
"--debug",
action=ArgumentAction.STORE_BOOL_OPTIONAL,
help="Enable debug mode.",
)
args = await parser.parse_args([])
assert args["debug"] is None
@pytest.mark.asyncio
async def test_store_bool_optional_flag_order():
parser = CommandArgumentParser()
parser.add_argument(
"--dry-run",
action=ArgumentAction.STORE_BOOL_OPTIONAL,
help="Run without making changes.",
)
args = await parser.parse_args(["--dry-run"])
assert args["dry_run"] is True
args = await parser.parse_args(["--no-dry-run"])
assert args["dry_run"] is False
def test_store_bool_optional_requires_long_flag():
parser = CommandArgumentParser()
with pytest.raises(CommandArgumentError):
parser.add_argument(
"-d", action=ArgumentAction.STORE_BOOL_OPTIONAL, help="Invalid"
)
def test_store_bool_optional_disallows_multiple_flags():
parser = CommandArgumentParser()
with pytest.raises(CommandArgumentError):
parser.add_argument("--debug", "-d", action=ArgumentAction.STORE_BOOL_OPTIONAL)
def test_store_bool_optional_duplicate_dest():
parser = CommandArgumentParser()
parser.add_argument(
"--debug",
action=ArgumentAction.STORE_BOOL_OPTIONAL,
help="Enable debug mode.",
)
with pytest.raises(CommandArgumentError):
parser.add_argument(
"--debug",
action=ArgumentAction.STORE_TRUE,
help="Conflicting debug option.",
)