Skip to content

Commit

Permalink
Merge pull request #23 from explosion/fix/no-defaullt-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed Mar 18, 2023
2 parents 4d53eda + 3e59ca4 commit 10961dd
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
6 changes: 4 additions & 2 deletions radicli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,14 @@ def _add_args(self, parser: ArgumentParser, args: List[ArgparseArg]) -> None:
if arg.id == self.extra_key:
continue
func_args, func_kwargs = arg.to_argparse()
# Suppress all argument defaults and mark options without defaults
# as required
# Suppress all defaults and mark options without defaults as required
if not self.fill_defaults:
func_kwargs["default"] = DEFAULT_PLACEHOLDER
if arg.arg.option:
func_kwargs["required"] = arg.default is DEFAULT_PLACEHOLDER
if arg.help and arg.default is not DEFAULT_PLACEHOLDER:
# Manually add default to help again (now suppressed)
func_kwargs["help"] = f"{arg.help} (default: {arg.default})"
parser.add_argument(*func_args, **func_kwargs)

def _validate(
Expand Down
4 changes: 3 additions & 1 deletion radicli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import argparse
import sys

from .util import format_arg_help, CliParserError
from .util import format_arg_help, CliParserError, DEFAULT_PLACEHOLDER


class ArgumentParser(argparse.ArgumentParser):
Expand All @@ -13,6 +13,8 @@ def error(self, message: str) -> NoReturn:
# Overriding this internal function so we can have more control over how
# errors are handled and (not) masked internally
def _get_value(self, action: argparse.Action, arg_string: str) -> Any:
if arg_string == DEFAULT_PLACEHOLDER:
return DEFAULT_PLACEHOLDER
type_func = self._registry_get("type", action.type, action.type)
if not callable(type_func):
raise argparse.ArgumentError(action, f"{type_func!r} is not callable")
Expand Down
1 change: 1 addition & 0 deletions radicli/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def get_arg(
if inspect.isclass(param_type) and issubclass(param_type, Enum):
arg.choices = list(param_type.__members__.keys())
arg.type = lambda value: getattr(param_type, value, value)
arg.has_converter = True
return arg
if not origin:
raise UnsupportedTypeError(param, param_type)
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.19
version = 0.0.20
description = Radically lightweight command-line interfaces
url = https://github.com/explosion/radicli
author = Explosion
Expand Down

0 comments on commit 10961dd

Please sign in to comment.