Skip to content

Commit 55fee27

Browse files
committed
extra-arg accumulates as a list
1 parent f87ef8d commit 55fee27

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

cpp_linter/run.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,16 @@
239239
)
240240
cli_arg_parser.add_argument(
241241
"-x",
242-
"--extra-args",
243-
default="",
242+
"--extra-arg",
243+
default=[],
244+
action="append",
244245
help="""A string of extra arguments passed to clang-tidy for use as
245-
compiler arguments.
246+
compiler arguments. This can be specified more than once for each
247+
additional argument. Recommend using quotes around the value here:
248+
249+
.. code-block:: shell
250+
251+
cpp-linter --extra-arg="-std=c++17" --extra-arg="-Wall"
246252
247253
Defaults to ``'%(default)s'``.
248254
""",
@@ -487,7 +493,7 @@ def run_clang_tidy(
487493
lines_changed_only: int,
488494
database: str,
489495
repo_root: str,
490-
extra_args: str,
496+
extra_args: List[str],
491497
) -> None:
492498
"""Run clang-tidy on a certain file.
493499
@@ -500,7 +506,7 @@ def run_clang_tidy(
500506
diff info.
501507
:param database: The path to the compilation database.
502508
:param repo_root: The path to the repository root folder.
503-
:param extra_args: A string of extra arguments used by clang-tidy as compiler
509+
:param extra_args: A list of extra arguments used by clang-tidy as compiler
504510
arguments.
505511
"""
506512
if checks == "-*": # if all checks are disabled, then clang-tidy is skipped
@@ -524,8 +530,10 @@ def run_clang_tidy(
524530
line_ranges = dict(name=filename, lines=file_obj["line_filter"][ranges])
525531
logger.info("line_filter = %s", json.dumps([line_ranges]))
526532
cmds.append(f"--line-filter={json.dumps([line_ranges])}")
527-
if extra_args:
528-
cmds.append(f"--extra-arg={repr(extra_args)}")
533+
if len(extra_args) == 1 and " " in extra_args[0]:
534+
extra_args = extra_args[0].split()
535+
for extra_arg in extra_args:
536+
cmds.append(f"--extra-arg={extra_arg}")
529537
cmds.append(filename)
530538
# clear yml file's content before running clang-tidy
531539
Path("clang_tidy_output.yml").write_bytes(b"")
@@ -638,7 +646,7 @@ def capture_clang_tools_output(
638646
lines_changed_only: int,
639647
database: str,
640648
repo_root: str,
641-
extra_args: str,
649+
extra_args: List[str],
642650
):
643651
"""Execute and capture all output from clang-tidy and clang-format. This aggregates
644652
results in the :attr:`~cpp_linter.Globals.OUTPUT`.
@@ -652,7 +660,7 @@ def capture_clang_tools_output(
652660
diff info.
653661
:param database: The path to the compilation database.
654662
:param repo_root: The path to the repository root folder.
655-
:param extra_args: A string of extra arguments used by clang-tidy as compiler
663+
:param extra_args: A list of extra arguments used by clang-tidy as compiler
656664
arguments.
657665
"""
658666
# temporary cache of parsed notifications for use in log commands
@@ -987,7 +995,7 @@ def main():
987995
args.lines_changed_only,
988996
args.database,
989997
args.repo_root,
990-
args.extra_args,
998+
args.extra_arg,
991999
)
9921000

9931001
start_log_group("Posting comment(s)")

tests/capture_tools_output/test_database_path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_db_detection(
4848
lines_changed_only=0, # analyze complete file
4949
database=database,
5050
repo_root=rel_root,
51-
extra_args="",
51+
extra_args=[],
5252
)
5353
matched_args = []
5454
for record in caplog.records:

tests/capture_tools_output/test_tools_output.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_format_annotations(
113113
lines_changed_only=lines_changed_only,
114114
database="",
115115
repo_root="",
116-
extra_args="",
116+
extra_args=[],
117117
)
118118
assert "Output from `clang-tidy`" not in cpp_linter.Globals.OUTPUT
119119
caplog.set_level(logging.INFO, logger=log_commander.name)
@@ -163,7 +163,7 @@ def test_tidy_annotations(
163163
lines_changed_only=lines_changed_only,
164164
database="",
165165
repo_root="",
166-
extra_args="",
166+
extra_args=[],
167167
)
168168
assert "Run `clang-format` on the following files" not in cpp_linter.Globals.OUTPUT
169169
caplog.set_level(logging.INFO, logger=log_commander.name)
@@ -197,7 +197,7 @@ def test_diff_comment(lines_changed_only: int):
197197
lines_changed_only=lines_changed_only,
198198
database="",
199199
repo_root="",
200-
extra_args="",
200+
extra_args=[],
201201
)
202202
diff_comments = list_diff_comments(lines_changed_only)
203203
# output = Path(__file__).parent / "diff_comments.json"

tests/test_cli_args.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Args:
3636
files_changed_only: bool = False
3737
thread_comments: bool = False
3838
file_annotations: bool = True
39-
extra_args: str = ""
39+
extra_arg: List[str] = []
4040

4141

4242
def test_defaults():
@@ -63,7 +63,8 @@ def test_defaults():
6363
("files-changed-only", "True", "files_changed_only", True),
6464
("thread-comments", "True", "thread_comments", True),
6565
("file-annotations", "False", "file_annotations", False),
66-
("extra-args", '"-std=c++17"', "extra_args", '"-std=c++17"'),
66+
("extra-arg", "-std=c++17", "extra_arg", ["-std=c++17"]),
67+
("extra-arg", '"-std=c++17 -Wall"', "extra_arg", ['"-std=c++17 -Wall"']),
6768
],
6869
)
6970
def test_arg_parser(

0 commit comments

Comments
 (0)