Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding option to not print line's number: --no-line-numbers #904

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions bandit/cli/main.py
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions bandit/core/issue.py
Expand Up @@ -3,6 +3,7 @@
#
# SPDX-License-Identifier: Apache-2.0
import linecache
import os

from bandit.core import constants

Expand Down Expand Up @@ -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 == "<stdin>":
text = self.fdata.readline()
Expand All @@ -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):
Expand Down