Skip to content

Commit

Permalink
Add ArgparseArg.display_type
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed Feb 18, 2023
1 parent 786c0bb commit 94914ff
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 2 additions & 3 deletions radicli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,12 @@ def get_converter(arg_type: Type) -> Optional[Callable[[str], Any]]:
param,
arg_info,
arg_type,
orig_type=param_type,
default=sig_defaults[param],
skip_resolve=converter is not None,
get_converter=get_converter,
)
has_converter = converter is not None or arg.has_converter
display_type = param_type if has_converter else arg_type
arg.help = join_strings(arg.help, f"({format_type(display_type)})")
arg.help = join_strings(arg.help, f"({format_type(arg.display_type)})")
cli_args.append(arg)
return Command(
name=name,
Expand Down
17 changes: 15 additions & 2 deletions radicli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ class ArgparseArg:
id: str
arg: Arg
type: Optional[Union[Type, Callable[[str], Any]]] = None
orig_type: Optional[Union[Type, Callable[[str], Any]]] = None
default: Any = DEFAULT_PLACEHOLDER
# We modify the help to add types so we store it twice to store old and new
help: Optional[str] = None
action: Optional[Union[str, Type[argparse.Action]]] = None
choices: Optional[Union[List[str], List[Enum]]] = None
has_converter: bool = False

@property
def display_type(self) -> Optional[Union[Type, Callable[[str], Any]]]:
default_type = self.type if self.type is not None else self.orig_type
return self.orig_type if self.has_converter else default_type

def to_argparse(self) -> Tuple[List[str], Dict[str, Any]]:
"""Helper method to generate args and kwargs for Parser.add_argument."""
args: List[str] = []
Expand Down Expand Up @@ -112,13 +118,20 @@ def get_arg(
orig_arg: Arg,
param_type: Any,
*,
orig_type: Optional[Union[Type, Callable[[str], Any]]] = None,
default: Optional[Any] = DEFAULT_PLACEHOLDER,
get_converter: Optional[Callable[[Type], Optional[ConverterType]]] = None,
skip_resolve: bool = False,
) -> ArgparseArg:
"""Generate an argument to add to argparse and interpret types if possible."""
arg = ArgparseArg(id=param, arg=orig_arg, type=param_type, help=orig_arg.help)
arg.default = default
arg = ArgparseArg(
id=param,
arg=orig_arg,
type=param_type,
help=orig_arg.help,
default=default,
orig_type=orig_type,
)
if orig_arg.count:
arg.action = "count"
arg.type = None
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[metadata]
version = 0.0.5
version = 0.0.6
description = Radically lightweight command-line interfaces
url = https://github.com/explosion/radicli
author = Explosion
Expand Down

0 comments on commit 94914ff

Please sign in to comment.