Skip to content

Commit

Permalink
added version command line argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mristin committed Sep 21, 2018
1 parent 280c78c commit 5358b86
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 9 additions & 2 deletions icontract_lint/main.py
Expand Up @@ -9,6 +9,7 @@
from typing import List, Any, TextIO

import icontract_lint
import pyicontract_lint_meta


class Args:
Expand All @@ -19,17 +20,19 @@ def __init__(self, args: Any) -> None:
assert isinstance(args.paths, list)
assert all(isinstance(pth, str) for pth in args.paths)

self.dont_panic = bool(args.dont_panic)
self.format = str(args.format)
self.version = bool(args.version)
self.paths = [pathlib.Path(pth) for pth in args.paths]
self.dont_panic = bool(args.dont_panic)


def parse_args(sys_argv: List[str]) -> Args:
"""Parse command-line arguments."""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--dont_panic", help="Retrun a zero code even if there were errors.", action='store_true')
parser.add_argument("--format", help="Specify the output format.", default='verbose', choices=['verbose', 'json'])
parser.add_argument("paths", help="Specify paths to check (directories and files).", nargs="+")
parser.add_argument("--version", help="Display the version and return immediately", action='store_true')
parser.add_argument("paths", help="Specify paths to check (directories and files).", nargs="*")

args = parser.parse_args(sys_argv[1:])

Expand All @@ -38,6 +41,10 @@ def parse_args(sys_argv: List[str]) -> Args:

def _main(args: Args, stream: TextIO) -> int:
"""Execute the main routine."""
if args.version:
stream.write("{}\n".format(pyicontract_lint_meta.__version__))
return 0

errors = icontract_lint.check_paths(paths=args.paths)

if args.format == 'verbose':
Expand Down
10 changes: 10 additions & 0 deletions tests/test_main.py
Expand Up @@ -12,6 +12,7 @@
import icontract_lint.main

# pylint: disable=missing-docstring
import pyicontract_lint_meta


class TestParseArgs(unittest.TestCase):
Expand Down Expand Up @@ -125,6 +126,15 @@ def test_dont_panic(self):

self.assertEqual(0, retcode)

def test_version(self):
buf = io.StringIO()
stream = cast(TextIO, buf)
args = icontract_lint.main.parse_args(sys_argv=["some-executable.py", "--version"])

retcode = icontract_lint.main._main(args=args, stream=stream)
self.assertEqual(0, retcode)
self.assertEqual('{}\n'.format(pyicontract_lint_meta.__version__), buf.getvalue())


if __name__ == '__main__':
unittest.main()

0 comments on commit 5358b86

Please sign in to comment.