Skip to content

Commit

Permalink
Allow rule ranges on command line (#535)
Browse files Browse the repository at this point in the history
Allow the user to specify a rule list range, such as
   --enable=PY002-PY005

Also, the enabled and disabled CLI arguments are now made mutually
exclusive.

Closes: #531

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb authored Jul 22, 2024
1 parent eed6230 commit 529e931
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/man/precli.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## SYNOPSIS

```
precli [-h] [-d] [-r] [--enable ENABLE] [--disable DISABLE] [--json] [--plain]
precli [-h] [-d] [-r] [--enable ENABLE | --disable DISABLE] [--json] [--plain]
[--markdown] [--gist] [-o OUTPUT] [--no-color] [-q] [--version]
[targets ...]
```
Expand Down
9 changes: 5 additions & 4 deletions precli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ def setup_arg_parser():
action="store_true",
help="find and process files in subdirectories",
)
parser.add_argument(
enable_grp = parser.add_mutually_exclusive_group()
enable_grp.add_argument(
"--enable",
dest="enable",
action="store",
default=None,
type=str,
help="comma-separated list of rule IDs or names to enable",
)
parser.add_argument(
enable_grp.add_argument(
"--disable",
dest="disable",
action="store",
Expand Down Expand Up @@ -329,8 +330,8 @@ def main():
# Setup the command line arguments
args = setup_arg_parser()

enabled = args.enable.split(",") if args.enable else []
disabled = args.disable.split(",") if args.disable else []
enabled = args.enable.split(",") if args.enable else None
disabled = args.disable.split(",") if args.disable else None

# Compile a list of the targets
artifacts = discover_files(args.targets, args.recursive)
Expand Down
42 changes: 34 additions & 8 deletions precli/parsers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,48 @@ def rule_prefix(self) -> str:
def get_file_encoding(self, file_path: str) -> str:
"""The prefix for the rule ID"""

@staticmethod
def _expand_rule_list(rule_list: list[str]) -> list[str]:
"""Expand rule range if exists in the rule list"""
expanded_rules = []
for rule in rule_list:
if "-" in rule:
(rule_start, rule_end) = rule.split("-", maxsplit=1)
if rule_start[:-3] == rule_end[:-3]:
try:
start = int(rule_start[-3:])
end = int(rule_end[-3:])
for i in range(start, end + 1):
expanded_rules.append(f"{rule_start[:-3]}{i:03d}")
except ValueError:
pass
else:
expanded_rules.append(rule)
return expanded_rules

def parse(
self,
artifact: Artifact,
enabled: list[str] = None,
disabled: list[str] = None,
) -> list[Result]:
"""File extension of files this parser can handle."""
for rule in self.rules.values():
if enabled is not None and (
enabled == ["all"]
or rule.id in enabled
or rule.name in enabled
):
rule.enabled = True
if enabled is not None:
enabled = Parser._expand_rule_list(enabled)
if disabled is not None:
disabled = Parser._expand_rule_list(disabled)

if disabled is not None and (
for rule in self.rules.values():
if enabled is not None:
if (
enabled == ["all"]
or rule.id in enabled
or rule.name in enabled
):
rule.enabled = True
else:
rule.enabled = False
elif disabled is not None and (
disabled == ["all"]
or rule.id in disabled
or rule.name in disabled
Expand Down
2 changes: 1 addition & 1 deletion precli/rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def enabled(self) -> bool:
return self._enabled

@enabled.setter
def enabled(self, enabled):
def enabled(self, enabled: bool):
"""Set whether the rule is enabled"""
self._enabled = enabled

Expand Down

0 comments on commit 529e931

Please sign in to comment.