Skip to content

Commit 94914ff

Browse files
committed
Add ArgparseArg.display_type
1 parent 786c0bb commit 94914ff

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

radicli/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,12 @@ def get_converter(arg_type: Type) -> Optional[Callable[[str], Any]]:
167167
param,
168168
arg_info,
169169
arg_type,
170+
orig_type=param_type,
170171
default=sig_defaults[param],
171172
skip_resolve=converter is not None,
172173
get_converter=get_converter,
173174
)
174-
has_converter = converter is not None or arg.has_converter
175-
display_type = param_type if has_converter else arg_type
176-
arg.help = join_strings(arg.help, f"({format_type(display_type)})")
175+
arg.help = join_strings(arg.help, f"({format_type(arg.display_type)})")
177176
cli_args.append(arg)
178177
return Command(
179178
name=name,

radicli/util.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,19 @@ class ArgparseArg:
7676
id: str
7777
arg: Arg
7878
type: Optional[Union[Type, Callable[[str], Any]]] = None
79+
orig_type: Optional[Union[Type, Callable[[str], Any]]] = None
7980
default: Any = DEFAULT_PLACEHOLDER
8081
# We modify the help to add types so we store it twice to store old and new
8182
help: Optional[str] = None
8283
action: Optional[Union[str, Type[argparse.Action]]] = None
8384
choices: Optional[Union[List[str], List[Enum]]] = None
8485
has_converter: bool = False
8586

87+
@property
88+
def display_type(self) -> Optional[Union[Type, Callable[[str], Any]]]:
89+
default_type = self.type if self.type is not None else self.orig_type
90+
return self.orig_type if self.has_converter else default_type
91+
8692
def to_argparse(self) -> Tuple[List[str], Dict[str, Any]]:
8793
"""Helper method to generate args and kwargs for Parser.add_argument."""
8894
args: List[str] = []
@@ -112,13 +118,20 @@ def get_arg(
112118
orig_arg: Arg,
113119
param_type: Any,
114120
*,
121+
orig_type: Optional[Union[Type, Callable[[str], Any]]] = None,
115122
default: Optional[Any] = DEFAULT_PLACEHOLDER,
116123
get_converter: Optional[Callable[[Type], Optional[ConverterType]]] = None,
117124
skip_resolve: bool = False,
118125
) -> ArgparseArg:
119126
"""Generate an argument to add to argparse and interpret types if possible."""
120-
arg = ArgparseArg(id=param, arg=orig_arg, type=param_type, help=orig_arg.help)
121-
arg.default = default
127+
arg = ArgparseArg(
128+
id=param,
129+
arg=orig_arg,
130+
type=param_type,
131+
help=orig_arg.help,
132+
default=default,
133+
orig_type=orig_type,
134+
)
122135
if orig_arg.count:
123136
arg.action = "count"
124137
arg.type = None

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[metadata]
2-
version = 0.0.5
2+
version = 0.0.6
33
description = Radically lightweight command-line interfaces
44
url = https://github.com/explosion/radicli
55
author = Explosion

0 commit comments

Comments
 (0)