-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion.py
46 lines (30 loc) · 1.09 KB
/
conversion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from enum import StrEnum, auto
from typing import Literal
import re
from arcparse import arcparser, dict_option, itemwise, option
class Color(StrEnum):
RED = auto()
GREEN = auto()
def parse_result(arg: str) -> bool:
if arg == "pass":
return True
return False
@arcparser
class Args:
value: int
# automatically populates choices
consent: Literal["yes", "no"]
# also automatically populates choices
color: Color
# also automatically populates choices
mode: int = dict_option({"auto": 0, "manual": 1})
# compiles to a regex using re.compile
regex: re.Pattern
result: bool = option(converter=parse_result)
# argument accepts multiple command-line values if no converter is set, or itemwise is used
# the names field will contain multiple values from a single argument
names: list[str] = option(converter=lambda x: x.split())
# the results field will contain multiple values, each from a separate argument
results: list[bool] = option(converter=itemwise(parse_result))
if __name__ == "__main__":
print(vars(Args.parse()))