From a1ed3e3d83ae3bb86fa36235382d4d22a0b784bd Mon Sep 17 00:00:00 2001 From: Jakub Wilk Date: Fri, 13 Jan 2023 23:05:01 +0100 Subject: [PATCH] Add -e. https://github.com/jwilk/anorack/issues/9 --- doc/manpage.rst | 2 ++ lib/cli.py | 8 +++++++- tests/test_cli.py | 8 ++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/manpage.rst b/doc/manpage.rst index 9499cce..e795ecd 100644 --- a/doc/manpage.rst +++ b/doc/manpage.rst @@ -28,6 +28,8 @@ that finds incorrect indefinite articles. Options ------- +-e + Exit with non-zero status if any incorrect articles were found. --ipa Print phonemes using IPA (International Phonetic Alphabet) instead of ASCII phoneme mnemonics. diff --git a/lib/cli.py b/lib/cli.py index 86d526f..5715649 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -79,6 +79,8 @@ def check_word(loc, art, word, *, ipa=False): elif art.lower() != correct_art: correct_art = coerce_case(art, correct_art) print(f'{loc}: {art} {word} -> {correct_art} {word} /{phon}/') + return False + return True def main(): ''' @@ -87,6 +89,7 @@ def main(): signal.signal(signal.SIGPIPE, signal.SIG_DFL) ap = ArgumentParser(description='"a" vs "an" checker') ap.add_argument('--version', action=VersionAction) + ap.add_argument('-e', action='store_true', help='exit with non-zero status if issues were found') ap.add_argument('--ipa', action='store_true', help='use IPA instead of ASCII phoneme mnemonics') ap.add_argument('--traceback', action='store_true', help=argparse.SUPPRESS) ap.add_argument('files', metavar='FILE', nargs='*', default=['-'], @@ -96,6 +99,7 @@ def main(): sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding, line_buffering=sys.stdout.line_buffering) sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding, line_buffering=sys.stdout.line_buffering) init_phonetics() + ok = True rc = 0 for path in options.files: try: @@ -109,7 +113,9 @@ def main(): continue with file: for loc, art, word in parse_file(file): - check_word(loc, art, word, ipa=options.ipa) + ok &= check_word(loc, art, word, ipa=options.ipa) + if rc == 0 and options.e and not ok: + rc = 2 sys.exit(rc) __all__ = ['main'] diff --git a/tests/test_cli.py b/tests/test_cli.py index bc2528b..3e04394 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -176,6 +176,14 @@ def test_bad_io(): assert_is_instance(actual.rc, OSError) assert_equal(actual.rc.errno, errno.ENOENT) +@testcase +def test_e(): + argv = ['anorack', '-e'] + actual = run_main(argv, 'a African') + assert_equal(':', actual.stdout[:8]) + assert_equal('', actual.stderr) + assert_equal(actual.rc, 2) + @testcase def test_changelog(): argv = ['anorack', 'doc/changelog']