diff --git a/docs/man/precli.md b/docs/man/precli.md index d884c827..ca0fe422 100644 --- a/docs/man/precli.md +++ b/docs/man/precli.md @@ -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 ...] ``` diff --git a/precli/cli/main.py b/precli/cli/main.py index c21be528..89a93ba5 100644 --- a/precli/cli/main.py +++ b/precli/cli/main.py @@ -59,7 +59,8 @@ 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", @@ -67,7 +68,7 @@ def setup_arg_parser(): 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", @@ -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) diff --git a/precli/parsers/__init__.py b/precli/parsers/__init__.py index 3745c374..92a15e67 100644 --- a/precli/parsers/__init__.py +++ b/precli/parsers/__init__.py @@ -76,6 +76,25 @@ 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, @@ -83,15 +102,22 @@ def parse( 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 diff --git a/precli/rules/__init__.py b/precli/rules/__init__.py index 74d88a95..a8bd5281 100644 --- a/precli/rules/__init__.py +++ b/precli/rules/__init__.py @@ -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