Skip to content

Commit 1eff509

Browse files
nathanstockingNathan Stocking
and
Nathan Stocking
authored
Add string options for severity and confidence (PyCQA#702)
Adds two new command line arguments which allow the user to specify severity level and confidence level with a key-value pair rather than repeating a flag. This makes it easier to specify those values if using an alternate interface which invokes Bandit's CLI. The previous repeatable flags have been retained and existing workflows will not be affected. New arguments: * --severity-level: Takes a string "all", "low", "medium", or "high" to set the level. This has the same effect as the existing -l/--level option. If both options are specified, an error will be printed. * --confidence-level: Takes a string "all", "low", "medium", or "high" to set the level. This has the same effect as the existing -i/--confidence option. If both options are specified, an error will be printed. * Help text for these parameters clarifies why 'all' and 'low' aren't the same although they will almost certainly produce the same set of results. Co-authored-by: Nathan Stocking <[email protected]>
1 parent 6765a57 commit 1eff509

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

bandit/cli/main.py

+43-2
Original file line numberDiff line numberDiff line change
@@ -180,16 +180,34 @@ def main():
180180
action='store', default=None, type=str,
181181
help='comma-separated list of test IDs to skip'
182182
)
183-
parser.add_argument(
183+
severity_group = parser.add_mutually_exclusive_group(required=False)
184+
severity_group.add_argument(
184185
'-l', '--level', dest='severity', action='count',
185186
default=1, help='report only issues of a given severity level or '
186187
'higher (-l for LOW, -ll for MEDIUM, -lll for HIGH)'
187188
)
188-
parser.add_argument(
189+
severity_group.add_argument(
190+
'--severity-level', dest='severity_string', action='store',
191+
help='report only issues of a given severity level or higher.'
192+
' "all" and "low" are likely to produce the same results, but it'
193+
' is possible for rules to be undefined which will'
194+
' not be listed in "low".',
195+
choices=['all', 'low', 'medium', 'high']
196+
)
197+
confidence_group = parser.add_mutually_exclusive_group(required=False)
198+
confidence_group.add_argument(
189199
'-i', '--confidence', dest='confidence', action='count',
190200
default=1, help='report only issues of a given confidence level or '
191201
'higher (-i for LOW, -ii for MEDIUM, -iii for HIGH)'
192202
)
203+
confidence_group.add_argument(
204+
'--confidence-level', dest='confidence_string', action='store',
205+
help='report only issues of a given confidence level or higher.'
206+
' "all" and "low" are likely to produce the same results, but it'
207+
' is possible for rules to be undefined which will'
208+
' not be listed in "low".',
209+
choices=["all", "low", "medium", "high"]
210+
)
193211
output_format = 'screen' if sys.stdout.isatty() else 'txt'
194212
parser.add_argument(
195213
'-f', '--format', dest='output_format', action='store',
@@ -302,6 +320,29 @@ def main():
302320
if args.output_format != 'custom' and args.msg_template is not None:
303321
parser.error("--msg-template can only be used with --format=custom")
304322

323+
# Check if confidence or severity level have been specified with strings
324+
if args.severity_string is not None:
325+
if args.severity_string == "all":
326+
args.severity = 1
327+
elif args.severity_string == "low":
328+
args.severity = 2
329+
elif args.severity_string == "medium":
330+
args.severity = 3
331+
elif args.severity_string == "high":
332+
args.severity = 4
333+
# Other strings will be blocked by argparse
334+
335+
if args.confidence_string is not None:
336+
if args.confidence_string == "all":
337+
args.confidence = 1
338+
elif args.confidence_string == "low":
339+
args.confidence = 2
340+
elif args.confidence_string == "medium":
341+
args.confidence = 3
342+
elif args.confidence_string == "high":
343+
args.confidence = 4
344+
# Other strings will be blocked by argparse
345+
305346
try:
306347
b_conf = b_config.BanditConfig(config_file=args.config_file)
307348
except utils.ConfigError as e:

0 commit comments

Comments
 (0)