diff --git a/bandit/cli/main.py b/bandit/cli/main.py index 47588859..2e5273c2 100644 --- a/bandit/cli/main.py +++ b/bandit/cli/main.py @@ -367,6 +367,15 @@ def main(): default=False, help="exit with 0, " "even with results found", ) + parser.add_argument( + "-no", + "--no-line-numbers", + dest="no_line_numbers", + action="store", + default=False, + type=str, + help="flag for not showing code line's", + ) python_ver = sys.version.replace("\n", "") parser.add_argument( "--version", @@ -451,6 +460,9 @@ def main(): args.confidence = 4 # Other strings will be blocked by argparse + if args.no_line_numbers is not None: + os.environ["BANDIT_NO_LINES"] = str(args.no_line_numbers) + try: b_conf = b_config.BanditConfig(config_file=args.config_file) except utils.ConfigError as e: @@ -593,6 +605,13 @@ def main(): "path of a baseline report", ) + args.no_line_numbers = _log_option_source( + parser.get_default("no_line_numbers"), + args.baseline, + ini_options.get("no-line-numbers"), + "do not print code's lines.", + ) + if not args.targets: parser.print_usage() sys.exit(2) diff --git a/bandit/core/issue.py b/bandit/core/issue.py index 875e5e41..210dd8e2 100644 --- a/bandit/core/issue.py +++ b/bandit/core/issue.py @@ -3,6 +3,7 @@ # # SPDX-License-Identifier: Apache-2.0 import linecache +import os from bandit.core import constants @@ -181,7 +182,11 @@ def get_code(self, max_lines=3, tabbed=False): for line_num in range(1, lmin): self.fdata.readline() - tmplt = "%i\t%s" if tabbed else "%i %s" + no_lines = os.getenv("BANDIT_NO_LINES") + if no_lines == "True" or no_lines == "true" or no_lines == "TRUE": + tmplt = "\t%s" if tabbed else " %s" + else: + tmplt = "%i\t%s" if tabbed else "%i %s" for line in range(lmin, lmax): if self.fname == "": text = self.fdata.readline() @@ -193,7 +198,11 @@ def get_code(self, max_lines=3, tabbed=False): if not len(text): break - lines.append(tmplt % (line, text)) + if no_lines == "True" or no_lines == "true" or no_lines == "TRUE": + lines.append(tmplt % (text)) + else: + lines.append(tmplt % (line, text)) + return "".join(lines) def as_dict(self, with_code=True, max_lines=3):