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

Updated argument parser to print a better help message. #23

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 48 additions & 21 deletions src/stacy_analyzer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,66 @@ def __init__(self):
self.isatty = sys.stdout.isatty()

def main(self):
arg_parser = argparse.ArgumentParser(description='Static Analyzer for the Clarity language from Stacks')
subparsers = arg_parser.add_subparsers(dest="command", help="Commands")

lint_parser = subparsers.add_parser("lint", help="Run detectors in a given contract or contracts directory")
lint_parser.add_argument("path", type=str, help="Path")
lint_parser.add_argument("--filter", nargs="+", type=str, help="Comma-separated list of detector names to use")
lint_parser.add_argument("--exclude", nargs="+", type=str,
help="Comma-separated list of detector names to exclude")
list_detectors = subparsers.add_parser("detectors", help="List detectors")
lint_parser.add_argument("-A", nargs="+", type=int,
help="Print A lines of trailing context after findings.")
lint_parser.add_argument("-B", nargs="+", type=int,
help="Print B lines of leading context before findings.")
lint_parser.add_argument("-C", nargs="+", type=int,
help="Print C lines of leading and trailing context after and before findings. Takes precedence over -A and -B")
def comma_separated_list(value):
return value.split(',')

class StacyParser(argparse.ArgumentParser):
def format_help(self):
formatter = self._get_formatter()
formatter.add_text(self.description)
formatter.add_usage(self.usage, self._actions,
self._mutually_exclusive_groups)
formatter.add_text(self.epilog)

# Add subparsers help
subparsers_actions = [
action for action in self._actions
if isinstance(action, argparse._SubParsersAction)]
for subparsers_action in subparsers_actions:
for choice, subparser in subparsers_action.choices.items():
formatter.start_section(f"{choice} subcommand")
formatter.add_text(subparser.description)
formatter.add_arguments(subparser._actions)
formatter.end_section()

return formatter.format_help()

arg_parser = StacyParser(description='Static Analyzer for the Clarity language from Stacks')
subparsers = arg_parser.add_subparsers(dest="command", help="Available commands")

lint_parser = subparsers.add_parser("lint", help="Run detectors on a given contract or contracts directory")
lint_parser.add_argument("path", type=str, help="Path to the contract file or directory")
lint_parser.add_argument("--filter", type=comma_separated_list, metavar="lint1,lint2,...",
help="List of detector names to use")
lint_parser.add_argument("--exclude", type=comma_separated_list, metavar="lint1,lint2,...",
help="List of detector names to exclude")
lint_parser.add_argument("-A", type=int, metavar="x",
help="Print x lines of trailing context after findings")
lint_parser.add_argument("-B", type=int, metavar="x",
help="Print x lines of leading context before findings")
lint_parser.add_argument("-C", type=int, metavar="x",
help="Print x lines of leading and trailing context (overrides -A and -B)")

list_detectors = subparsers.add_parser("detectors", help="List available detectors")

user_args = arg_parser.parse_args()
if user_args.command == "lint":

tc = 0
lc = 0

if user_args.A is not None:
tc = user_args.A[0]
tc = user_args.A

if user_args.B is not None:
lc = user_args.B[0]
lc = user_args.B

if user_args.C is not None:
tc = user_args.C[0]
lc = user_args.C[0]
tc = user_args.C
lc = user_args.C

filters = list(self.DETECTOR_MAP.keys()) if user_args.filter is None else user_args.filter[0].split(',')
excludes = [] if user_args.exclude is None else user_args.exclude[0].split(',')
filters = list(self.DETECTOR_MAP.keys()) if user_args.filter is None else user_args.filter
excludes = [] if user_args.exclude is None else user_args.exclude
detectors = self.get_detectors(filters, excludes)
path = user_args.path
if path.endswith(".clar"):
Expand Down
Loading