diff options
| author | 2020-01-25 13:58:13 +0000 | |
|---|---|---|
| committer | 2020-01-25 14:01:11 +0000 | |
| commit | 341b3141048a72b59593d17713bf492d5e115ac2 (patch) | |
| tree | dea044f17f41c3d1714242238625057b8fea39bd /src/utils/parse.py | |
| parent | update echo.py to use command spec language (diff) | |
| signature | ||
change command specs to be compiled at runtime by a decorator
Diffstat (limited to 'src/utils/parse.py')
| -rw-r--r-- | src/utils/parse.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/utils/parse.py b/src/utils/parse.py index b45ca6fe..258b97dc 100644 --- a/src/utils/parse.py +++ b/src/utils/parse.py @@ -155,3 +155,30 @@ def format_token_replace(s: str, vars: typing.Dict[str, str], for i, token in tokens: s = s[:i] + vars[token.replace(sigil, "", 1)] + s[i+len(token):] return s + +class ArgumentSpecType(object): + def __init__(self, name: str, exported: str): + self.name = name + self.exported = exported + +class ArgumentSpec(object): + def __init__(self, optional: bool, types: typing.List[ArgumentSpecType]): + self.optional = optional + self.types = types + +def argument_spec(spec: str) -> typing.List[ArgumentSpec]: + out: typing.List[ArgumentSpec] = [] + for type_names_str in spec.split(" "): + optional = type_names_str[0] == "?" + type_names_str = type_names_str[1:] + + spec_types: typing.List[ArgumentSpecType] = [] + for type_name in type_names_str.split("|"): + exported_name = "" + if "~" in type_name: + exported_name = type_name + type_name = type_name.replace("~", "", 1) + + spec_types.append(ArgumentSpecType(type_name, exported_name)) + out.append(ArgumentSpec(optional, spec_types)) + return out |
