Skip to content

Commit

Permalink
Merge pull request #1 from MamoruDS/dev
Browse files Browse the repository at this point in the history
bump version to 0.2.4
  • Loading branch information
MamoruDS authored Jul 19, 2022
2 parents b2eb7e7 + df4e6e1 commit 7117c4c
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 126 deletions.
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ def __init__(self) -> None:


class DB(TypedDict):
pass
...


class OB:
pass
...


T = TypeVar("T", bound=Union[TypedDict, object])
Expand Down
15 changes: 15 additions & 0 deletions tests/items/cmt_params.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from tests import CFG, cmd, get_profile
from typed_cap import Cap
from typing import List, Optional
Expand Down Expand Up @@ -49,3 +50,17 @@ class T(B):
cap = Cap(T)
res = cap.parse(cmd("--message foo,bar"))
assert G(res.val, "message") == ["foo,bar"]


def test_cmt_param_enum_on_val():
class CoinFlip(Enum):
head = 0
tail = 1

class T(B):
# @enum_on_value
flip: CoinFlip

cap = Cap(T)
res = cap.parse(cmd("--flip 1"))
assert G(res.val, "flip") == CoinFlip.tail
42 changes: 40 additions & 2 deletions tests/items/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum, IntEnum
from tests import CFG, cmd, get_profile
from typed_cap import Cap
from typed_cap.types import ArgsParserKeyError
Expand Down Expand Up @@ -165,7 +166,6 @@ class T(B):

cap = Cap(T)
res = cap.parse(cmd("--data=a,b,5,false"))
# FIXME:
assert G(res.val, "data") != (["a", "b"], 5.0, False)


Expand All @@ -176,10 +176,48 @@ class T(B):

cap = Cap(T)
res = cap.parse(cmd("--data=5,false,a,5"))
# FIXME:
assert G(res.val, "data") != (5.0, False, ("a", 5))


def test_option_enum_A():
class CoinFlip(Enum):
Head = 0
Tail = 1

class T(B):
flip: CoinFlip

cap = Cap(T)
res = cap.parse(cmd("--flip head"))
assert G(res.val, "flip") == CoinFlip.Head

def test_option_enum_B():
class CoinFlip(IntEnum):
Head = 0
Tail = 1

class T(B):
flip: CoinFlip

cap = Cap(T)
cap._attributes["enum_on_value"] = True
res = cap.parse(cmd("--flip 0"))
assert G(res.val, "flip") == CoinFlip.Head

def test_option_enum_C():
class CoinFlip(Enum):
Head = 'h'
Tail = 't'

class T(B):
flip: CoinFlip

cap = Cap(T)
cap._attributes["enum_on_value"] = True
res = cap.parse(cmd("--flip t"))
assert G(res.val, "flip") == CoinFlip.Tail


# test for alt bool


Expand Down
2 changes: 1 addition & 1 deletion typed_cap/args_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def args_parser(
elif at == "option":
named_options.append(an)
else:
pass
...

def get_valid_key(k: str) -> Tuple[str, bool, bool]:
key = k
Expand Down
40 changes: 26 additions & 14 deletions typed_cap/cap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from typed_cap.typing import (
VALIDATOR,
AnnoExtra,
ValidVal,
get_optional_candidates,
get_queue_type,
get_type_candidates,
Expand Down Expand Up @@ -305,7 +306,7 @@ def __call__(
name: str,
alias: Optional[VALID_ALIAS_CANDIDATES] = None,
) -> None:
pass
...


class _Helpers(TypedDict):
Expand Down Expand Up @@ -343,6 +344,7 @@ def colorize_text_t_value(val: Any) -> str:


class Cap(Generic[K, T, U]):
_attributes: Dict[str, Any]
_argstype: Type[T]
_args: Dict[str, _ArgOpt]
_about: Optional[str]
Expand All @@ -368,6 +370,7 @@ def __init__(
use_anno_cmt_params: bool = True,
add_helper_help: bool = True,
) -> None:
self._attributes = {}
self._argstype = argstype
self._args = {}
self._about = None
Expand Down Expand Up @@ -397,13 +400,17 @@ def __init__(
if alias is not None:
self._set_alias(name, alias)
#
show_default = params.get("show_default", None)
if show_default is not None:
self._args[name]["show_default"] = show_default
#
delimiter = params.get("delimiter", None)
if delimiter is not None:
self._args[name]["local_delimiter"] = delimiter
#
show_default = params.get("show_default", None)
if show_default is not None:
self._args[name]["show_default"] = show_default
self._attributes["enum_on_value"] = params.get(
"enum_on_value", False
)

self._add_helper_help = add_helper_help

Expand Down Expand Up @@ -517,9 +524,7 @@ def add_argument(
try:
self._set_alias(key, alias)
except CapInvalidAlias as err:
if ignore_invalid_alias:
pass
else:
if not ignore_invalid_alias:
raise err
return self

Expand Down Expand Up @@ -577,7 +582,7 @@ def default_strict(self, value: T) -> Cap:
for arg, val in value.items(): # type: ignore
try:
t = self._args[arg]["type"]
valid, _, _ = VALIDATOR.extract(t, val, cvt=False)
valid, _, _ = VALIDATOR.extract(t, val, cvt=False).unwrap()
if valid:
self._args[arg]["val"] = val
else:
Expand Down Expand Up @@ -606,13 +611,14 @@ def helper(self, helpers: Dict[K, BasicArgOption]) -> Cap:
print(
"[warn] detected call of `Cap.helper` after call of preset helpers"
)
for arg, opt in helpers.items():
for arg, opt in helpers.items(): # type: ignore
try:
try:
opt: Dict[str, str]
alias = opt.pop("alias")
self._set_alias(arg, alias)
except KeyError:
pass
...
self._args[arg] = {**self._args[arg], **opt} # type: ignore[misc]

except KeyError as err:
Expand Down Expand Up @@ -640,10 +646,15 @@ def parse(
self,
argv: List[str] = sys.argv[1:],
args_parser_options: Optional[ArgsParserOptions] = None,
validator: Optional[ValidVal] = None,
) -> Parsed[T]:
self._before_parse()

VALIDATOR.delimiter = self._delimiter
if validator is None:
validator = VALIDATOR

validator.delimiter = self._delimiter
validator.attributes = self._attributes

def _is_flag(t: Type) -> bool:
if t == bool:
Expand Down Expand Up @@ -707,13 +718,14 @@ def _is_flag(t: Type) -> bool:
for v in val:
t = opt["type"]
temp_delimiter = opt["local_delimiter"]
valid, v_got, err = VALIDATOR.extract(
valid, v_got, err = validator.extract(
t,
v,
cvt=True,
temp_delimiter=temp_delimiter,
leave_scope=True,
)
).unwrap()

# TODO: catch extract failed
if valid:
parsed["val"].append([v_got])
Expand Down Expand Up @@ -777,7 +789,7 @@ def _is_flag(t: Type) -> bool:
"default_val"
] = args_obj.__getattribute__(key)
except AttributeError:
pass
...
if (
parsed_map[key]["default_val"] is None
and get_optional_candidates(opt["type"]) is None
Expand Down
21 changes: 21 additions & 0 deletions typed_cap/cmt_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ValidParams(TypedDict, total=False):
alias: VALID_ALIAS_CANDIDATES
show_default: bool
delimiter: RO[str]
enum_on_value: bool


NamedValidParams = Dict[str, ValidParams]
Expand Down Expand Up @@ -96,6 +97,19 @@ def _parse_none_delimiter(
return RO.Some(None)


def _parse_enum_on_value(
name: str, val: _CmtParamVal
) -> Union[bool, NoReturn]:
_parse_flag_generic(
name,
"enum_on_value",
val,
flag_val=True,
allow_val=False,
)
return True


def parse_anno_cmt_params(
args: Dict[str, ArgOption]
) -> Union[NamedValidParams, NoReturn]:
Expand Down Expand Up @@ -136,6 +150,13 @@ def parse_anno_cmt_params(
val,
)

# @enum_on_value
elif key == "enum_on_value":
params["enum_on_value"] = _parse_enum_on_value(
name,
val,
)

named_param[name] = params
return named_param

Expand Down
4 changes: 2 additions & 2 deletions typed_cap/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,11 @@ def __init__(


class CapInvalidValue(_CapInvalidValue):
pass
...


class CapInvalidDefaultValue(_CapInvalidValue):
pass
...


class CapInvalidAlias(Exception):
Expand Down
Loading

0 comments on commit 7117c4c

Please sign in to comment.