Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Pull value-consumingness into ArgAction method. #149

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/cappa/arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def value_actions(cls) -> typing.Set[ArgAction]:
def is_custom(cls, action: ArgAction | Callable | None):
return action is not None and not isinstance(action, ArgAction)

@classmethod
def is_non_value_consuming(cls, action: ArgAction | Callable | None):
return action in {
ArgAction.store_true,
ArgAction.store_false,
ArgAction.count,
ArgAction.version,
ArgAction.help,
}

@property
def is_bool_action(self):
return self in {self.store_true, self.store_false}
Expand Down Expand Up @@ -150,7 +160,7 @@ def collect(
default_short: bool = False,
default_long: bool = False,
) -> list[Arg]:
args = find_annotations(type_view, cls) or [DEFAULT_ARG]
args = find_annotations(type_view, cls) or [Arg()]

exclusive = len(args) > 1

Expand Down Expand Up @@ -457,7 +467,7 @@ def infer_num_args(
if arg.parse:
return 1

if isinstance(action, ArgAction) and action in no_extra_arg_actions:
if ArgAction.is_non_value_consuming(action):
return 0

if type_view.is_union:
Expand Down Expand Up @@ -597,14 +607,3 @@ def explode_negated_bool_args(args: typing.Sequence[Arg]) -> typing.Iterable[Arg

if not yielded:
yield arg


no_extra_arg_actions = {
ArgAction.store_true,
ArgAction.store_false,
ArgAction.count,
ArgAction.version,
ArgAction.help,
}

DEFAULT_ARG: Arg = Arg()
4 changes: 2 additions & 2 deletions src/cappa/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import typing
from collections.abc import Callable

from cappa.arg import Arg, ArgAction, no_extra_arg_actions
from cappa.arg import Arg, ArgAction
from cappa.command import Command, Subcommand
from cappa.help import generate_arg_groups
from cappa.invoke import fulfill_deps
Expand Down Expand Up @@ -242,7 +242,7 @@ def add_argument(
if arg.default is not Empty:
kwargs["default"] = argparse.SUPPRESS

if num_args is not None and (arg.action and arg.action not in no_extra_arg_actions):
if num_args is not None and not ArgAction.is_non_value_consuming(arg.action):
kwargs["nargs"] = num_args
elif is_positional and not arg.required:
kwargs["nargs"] = "?"
Expand Down
4 changes: 2 additions & 2 deletions src/cappa/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rich.text import Text
from typing_extensions import Self, TypeAlias

from cappa.arg import Arg, ArgAction, Group, no_extra_arg_actions
from cappa.arg import Arg, ArgAction, Group
from cappa.output import Displayable
from cappa.subcommand import Subcommand
from cappa.type_view import Empty
Expand Down Expand Up @@ -220,7 +220,7 @@ def add_short_args(prog: str, arg_groups: list[ArgGroup]) -> str:
def format_arg_name(arg: Arg | Subcommand, delimiter, *, n=0) -> str:
if isinstance(arg, Arg):
is_option = arg.short or arg.long
has_value = arg.action not in no_extra_arg_actions
has_value = not ArgAction.is_non_value_consuming(arg.action)

arg_names = arg.names_str(delimiter, n=n)
if not is_option:
Expand Down
4 changes: 2 additions & 2 deletions src/cappa/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import typing
from collections import deque

from cappa.arg import Arg, ArgAction, Group, no_extra_arg_actions
from cappa.arg import Arg, ArgAction, Group
from cappa.command import Command, Subcommand
from cappa.completion.types import Completion, FileCompletion
from cappa.help import format_subcommand_names
Expand Down Expand Up @@ -478,7 +478,7 @@ def consume_arg(
orig_num_args = arg.num_args if arg.num_args is not None else 1
num_args = orig_num_args

if arg.action in no_extra_arg_actions:
if ArgAction.is_non_value_consuming(arg.action):
orig_num_args = 0
num_args = 0

Expand Down
Loading