Skip to content

Commit

Permalink
parse args with argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
graingert committed Jun 23, 2020
1 parent 11f1adf commit d6a9231
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions pre_commit_hooks/safety_check.py
@@ -1,19 +1,47 @@
from __future__ import print_function

import argparse
import sys

from safety.cli import check


def _parser():
parser = argparse.ArgumentParser()
parser.add_argument(
"--full-report",
default="--full-report",
const="--full-report",
action="store_const",
)
parser.add_argument(
"--short-report",
dest="full_report",
const="--short-report",
action="store_const",
)
parser.add_argument("--ignore", "-i", action="append")
parser.add_argument("files", nargs="+")
return parser


def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parsed_args, rest = _parser().parse_known_args(argv)
ignore = [
arg
for vs in parsed_args.ignore or []
for code in vs.split(",")
for arg in ("--ignore", code.strip())
]
files = [arg for f in parsed_args.files for arg in ("--file", f)]
args = [parsed_args.full_report] + ignore + files + rest
try:
check.main(['--full-report'] + sum(([arg] if arg.startswith('-') else ['--file', arg] for arg in argv), []))
return 0
check.main(args)
except SystemExit as error:
if error.code == 0:
return 0
return 1
return 1


if __name__ == '__main__':
if __name__ == "__main__":
sys.exit(main())

0 comments on commit d6a9231

Please sign in to comment.