Skip to content

Commit 9766474

Browse files
authored
Update the syntax in reduce.py (#6843)
1 parent a01c9c2 commit 9766474

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

tools/reduce.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
1+
"""
2+
usage: reduce.py [-h] -exe EXE -exe_args EXE_ARGS -expected EXPECTED -file FILE [-segfault]
3+
4+
options:
5+
-h, --help show this help message and exit
6+
-exe EXE, --exe EXE cppcheck executable
7+
-exe_args EXE_ARGS, --exe_args EXE_ARGS
8+
cppcheck executable commands
9+
-expected EXPECTED, --expected EXPECTED
10+
expected text output
11+
-file FILE, --file FILE
12+
source file
13+
-segfault, --segfault
14+
"""
15+
116
#!/usr/bin/env python3
217
import subprocess
318
import sys
419
import time
20+
import argparse
521

622

723
class Reduce:
824
def __init__(self, cmd, expected, file, segfault=None):
9-
if cmd is None:
25+
if not "".join(cmd):
1026
raise RuntimeError('Abort: No --cmd')
1127

12-
if not segfault and expected is None:
28+
if not segfault and not expected:
1329
raise RuntimeError('Abort: No --expected')
1430

15-
if file is None:
31+
if not file:
1632
raise RuntimeError('Abort: No --file')
1733

1834
# need to add '--error-exitcode=0' so detected issues will not be interpreted as a crash
1935
if segfault and '--error-exitcode=0' not in cmd:
2036
print("Adding '--error-exitcode=0' to --cmd")
21-
self.__cmd = cmd + ' --error-exitcode=0'
37+
self.__cmd = cmd + ['--error-exitcode=0']
2238
else:
2339
self.__cmd = cmd
2440
self.__expected = expected
@@ -30,7 +46,7 @@ def __init__(self, cmd, expected, file, segfault=None):
3046
self.__elapsed_time = None
3147

3248
def print_info(self):
33-
print('CMD=' + self.__cmd)
49+
print('CMD=', " ".join(self.__cmd))
3450
if self.__segfault:
3551
print('EXPECTED=SEGFAULT')
3652
else:
@@ -46,7 +62,7 @@ def runtool(self, filedata=None):
4662
timeout = None
4763
if self.__elapsed_time:
4864
timeout = self.__elapsed_time * 2
49-
p = subprocess.Popen(self.__cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
65+
p = subprocess.Popen(self.__cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
5066
try:
5167
comm = self.__communicate(p, timeout=timeout)
5268
except TimeoutExpired:
@@ -268,31 +284,27 @@ def main():
268284
# TODO: add --hang option to detect code which impacts the analysis time
269285
def show_syntax():
270286
print('Syntax:')
271-
print(' reduce.py --cmd=<full command> --expected=<expected text output> --file=<source file> [--segfault]')
287+
print(' reduce.py --exe <cppcheck executable> --exe_args <full command> --expected <expected text output> --file <source file> [--segfault]')
272288
print('')
273289
print("Example. source file = foo/bar.c")
274290
print(
275-
" reduce.py --cmd='./cppcheck --enable=style foo/bar.c' --expected=\"Variable 'x' is reassigned\" --file=foo/bar.c")
291+
' reduce.py --exe ./cppcheck --exe_args " --enable=style" --expected "Variable \'x\' is reassigned" --file foo/bar.c')
276292
sys.exit(1)
277293

278294
if len(sys.argv) == 1:
279295
show_syntax()
280-
281-
arg_cmd = None
282-
arg_expected = None
283-
arg_file = None
284-
arg_segfault = False
285-
286-
for arg in sys.argv[1:]:
287-
if arg.startswith('--cmd='):
288-
arg_cmd = arg[arg.find('=') + 1:]
289-
elif arg.startswith('--expected='):
290-
arg_expected = arg[arg.find('=') + 1:]
291-
elif arg.startswith('--file='):
292-
arg_file = arg[arg.find('=') + 1:]
293-
elif arg == '--segfault':
294-
arg_segfault = True
295-
296+
parser = argparse.ArgumentParser()
297+
parser.add_argument('-exe', '--exe', required=True, help="cppcheck executable")
298+
parser.add_argument('-exe_args', '--exe_args', required=False, default="", help="cppcheck executable commands")
299+
parser.add_argument('-expected', '--expected', required=True, help="expected text output")
300+
parser.add_argument('-file', '--file', required=True, help="source file")
301+
parser.add_argument('-segfault', '--segfault', required=False, action='store_true')
302+
args = parser.parse_args()
303+
304+
arg_file = args.file
305+
arg_cmd = [args.exe] + args.exe_args.split() + [arg_file]
306+
arg_expected = args.expected
307+
arg_segfault = args.segfault
296308
try:
297309
reduce = Reduce(arg_cmd, arg_expected, arg_file, arg_segfault)
298310
except RuntimeError as e:

0 commit comments

Comments
 (0)