Skip to content

Commit c978b20

Browse files
committed
check argument format
1 parent 6afd120 commit c978b20

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

can/logger.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def _parse_filters(parsed_args: Any) -> CanFilters:
135135
def _parse_additonal_config(
136136
unknown_args: Sequence[str],
137137
) -> Dict[str, Union[str, int, float, bool]]:
138+
for arg in unknown_args:
139+
if not re.match(r"^--[a-zA-Z\-]*?=\S*?$", arg):
140+
raise ValueError(f"Parsing argument {arg} failed")
141+
138142
def _split_arg(_arg: str) -> Tuple[str, str]:
139143
left, right = _arg.split("=", 1)
140144
return left.lstrip("--").replace("-", "_"), right

test/test_logger.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import gzip
1111
import os
1212
import sys
13+
14+
import pytest
15+
1316
import can
1417
import can.logger
1518

@@ -110,6 +113,7 @@ def test_parse_additonal_config(self):
110113
"--app-name=CANalyzer",
111114
"--serial=5555",
112115
"--receive-own-messages=True",
116+
"--false-boolean=False",
113117
"--offset=1.5",
114118
]
115119
parsed_args = can.logger._parse_additonal_config(unknown_args)
@@ -126,9 +130,27 @@ def test_parse_additonal_config(self):
126130
and parsed_args["receive_own_messages"] is True
127131
)
128132

133+
assert "false_boolean" in parsed_args
134+
assert (
135+
isinstance(parsed_args["false_boolean"], bool)
136+
and parsed_args["false_boolean"] is False
137+
)
138+
129139
assert "offset" in parsed_args
130140
assert parsed_args["offset"] == 1.5
131141

142+
with pytest.raises(ValueError):
143+
can.logger._parse_additonal_config(["--wrong-format"])
144+
145+
with pytest.raises(ValueError):
146+
can.logger._parse_additonal_config(["-wrongformat=value"])
147+
148+
with pytest.raises(ValueError):
149+
can.logger._parse_additonal_config(["--wrongformat=value1 value2"])
150+
151+
with pytest.raises(ValueError):
152+
can.logger._parse_additonal_config(["wrongformat="])
153+
132154

133155
class TestLoggerCompressedFile(unittest.TestCase):
134156
def setUp(self) -> None:

0 commit comments

Comments
 (0)