|
13 | 13 |
|
14 | 14 | Dynamic Controls 2010 |
15 | 15 | """ |
16 | | - |
| 16 | +import re |
17 | 17 | import sys |
18 | 18 | import argparse |
19 | 19 | from datetime import datetime |
20 | 20 | import errno |
21 | | -from typing import Any, Dict, List, Union |
| 21 | +from typing import Any, Dict, List, Union, Sequence, Tuple |
22 | 22 |
|
23 | 23 | import can |
24 | 24 | from . import Bus, BusState, Logger, SizedRotatingLogger |
@@ -134,11 +134,32 @@ def _parse_filters(parsed_args: Any) -> CanFilters: |
134 | 134 | return can_filters |
135 | 135 |
|
136 | 136 |
|
137 | | -def _parse_additonal_config(unknown_args): |
138 | | - return dict( |
139 | | - (arg.split("=", 1)[0].lstrip("--").replace("-", "_"), arg.split("=", 1)[1]) |
140 | | - for arg in unknown_args |
141 | | - ) |
| 137 | +def _parse_additional_config( |
| 138 | + unknown_args: Sequence[str], |
| 139 | +) -> Dict[str, Union[str, int, float, bool]]: |
| 140 | + for arg in unknown_args: |
| 141 | + if not re.match(r"^--[a-zA-Z\-]*?=\S*?$", arg): |
| 142 | + raise ValueError(f"Parsing argument {arg} failed") |
| 143 | + |
| 144 | + def _split_arg(_arg: str) -> Tuple[str, str]: |
| 145 | + left, right = _arg.split("=", 1) |
| 146 | + return left.lstrip("--").replace("-", "_"), right |
| 147 | + |
| 148 | + args: Dict[str, Union[str, int, float, bool]] = {} |
| 149 | + for key, string_val in map(_split_arg, unknown_args): |
| 150 | + if re.match(r"^[-+]?\d+$", string_val): |
| 151 | + # value is integer |
| 152 | + args[key] = int(string_val) |
| 153 | + elif re.match(r"^[-+]?\d*\.\d+$", string_val): |
| 154 | + # value is float |
| 155 | + args[key] = float(string_val) |
| 156 | + elif re.match(r"^(?:True|False)$", string_val): |
| 157 | + # value is bool |
| 158 | + args[key] = string_val == "True" |
| 159 | + else: |
| 160 | + # value is string |
| 161 | + args[key] = string_val |
| 162 | + return args |
142 | 163 |
|
143 | 164 |
|
144 | 165 | def main() -> None: |
@@ -202,7 +223,7 @@ def main() -> None: |
202 | 223 | raise SystemExit(errno.EINVAL) |
203 | 224 |
|
204 | 225 | results, unknown_args = parser.parse_known_args() |
205 | | - additional_config = _parse_additonal_config(unknown_args) |
| 226 | + additional_config = _parse_additional_config(unknown_args) |
206 | 227 | bus = _create_bus(results, can_filters=_parse_filters(results), **additional_config) |
207 | 228 |
|
208 | 229 | if results.active: |
|
0 commit comments