diff --git a/pre_commit_hooks/file_contents_sorter.py b/pre_commit_hooks/file_contents_sorter.py index c5691f0..5435188 100644 --- a/pre_commit_hooks/file_contents_sorter.py +++ b/pre_commit_hooks/file_contents_sorter.py @@ -49,21 +49,26 @@ def sort_file_contents( def main(argv: Sequence[str] | None = None) -> int: - parser = argparse.ArgumentParser() + parser = argparse.ArgumentParser(exit_on_error=False) parser.add_argument('filenames', nargs='+', help='Files to sort') - parser.add_argument( + group = parser.add_mutually_exclusive_group() + group.add_argument( '--ignore-case', action='store_const', const=bytes.lower, default=None, help='fold lower case to upper case characters', ) - parser.add_argument( + group.add_argument( '--unique', action='store_true', help='ensure each line is unique', ) - args = parser.parse_args(argv) + try: + args = parser.parse_args(argv) + except argparse.ArgumentError as e: + print(f'{e}') + return FAIL retv = PASS diff --git a/tests/file_contents_sorter_test.py b/tests/file_contents_sorter_test.py index 5e79e40..119d8a2 100644 --- a/tests/file_contents_sorter_test.py +++ b/tests/file_contents_sorter_test.py @@ -68,12 +68,6 @@ from pre_commit_hooks.file_contents_sorter import PASS ( b'fee\nFie\nFoe\nfum\n', ['--unique', '--ignore-case'], - PASS, - b'fee\nFie\nFoe\nfum\n', - ), - ( - b'fee\nfee\nFie\nFoe\nfum\n', - ['--unique', '--ignore-case'], FAIL, b'fee\nFie\nFoe\nfum\n', ),