|
33 | 33 | import copy
|
34 | 34 | import functools
|
35 | 35 | import itertools
|
| 36 | +import logging |
36 | 37 | import operator
|
37 | 38 | import re
|
38 | 39 | import sys
|
39 | 40 | import warnings
|
40 | 41 | from collections import defaultdict
|
41 | 42 | from collections.abc import Callable, Generator, Iterable, Sequence
|
42 |
| -from getopt import GetoptError, getopt |
43 | 43 | from io import BufferedIOBase, BufferedReader, BytesIO
|
44 | 44 | from itertools import chain
|
45 | 45 | from typing import TYPE_CHECKING, NamedTuple, NewType, NoReturn, TextIO, Union
|
@@ -876,67 +876,34 @@ def register(linter: PyLinter) -> None:
|
876 | 876 | linter.register_checker(SimilaritiesChecker(linter))
|
877 | 877 |
|
878 | 878 |
|
879 |
| -def usage(status: int = 0) -> NoReturn: |
880 |
| - """Display command line usage information.""" |
881 |
| - print("finds copy pasted blocks in a set of files") |
882 |
| - print() |
883 |
| - print( |
884 |
| - "Usage: symilar [-d|--duplicates min_duplicated_lines] \ |
885 |
| -[-i|--ignore-comments] [--ignore-docstrings] [--ignore-imports] [--ignore-signatures] file1..." |
886 |
| - ) |
887 |
| - sys.exit(status) |
888 |
| - |
889 |
| - |
890 | 879 | def Run(argv: Sequence[str] | None = None) -> NoReturn:
|
891 | 880 | """Standalone command line access point."""
|
892 |
| - if argv is None: |
893 |
| - argv = sys.argv[1:] |
894 |
| - |
895 |
| - s_opts = "hd:i:" |
896 |
| - l_opts = [ |
897 |
| - "help", |
898 |
| - "duplicates=", |
899 |
| - "ignore-comments", |
900 |
| - "ignore-imports", |
901 |
| - "ignore-docstrings", |
902 |
| - "ignore-signatures", |
903 |
| - ] |
904 |
| - min_lines = DEFAULT_MIN_SIMILARITY_LINE |
905 |
| - ignore_comments = False |
906 |
| - ignore_docstrings = False |
907 |
| - ignore_imports = False |
908 |
| - ignore_signatures = False |
909 |
| - try: |
910 |
| - opts, args = getopt(list(argv), s_opts, l_opts) |
911 |
| - except GetoptError as e: |
912 |
| - print(e) |
913 |
| - usage(2) |
914 |
| - for opt, val in opts: |
915 |
| - if opt in {"-d", "--duplicates"}: |
916 |
| - try: |
917 |
| - min_lines = int(val) |
918 |
| - except ValueError as e: |
919 |
| - print(e) |
920 |
| - usage(2) |
921 |
| - elif opt in {"-h", "--help"}: |
922 |
| - usage() |
923 |
| - elif opt in {"-i", "--ignore-comments"}: |
924 |
| - ignore_comments = True |
925 |
| - elif opt in {"--ignore-docstrings"}: |
926 |
| - ignore_docstrings = True |
927 |
| - elif opt in {"--ignore-imports"}: |
928 |
| - ignore_imports = True |
929 |
| - elif opt in {"--ignore-signatures"}: |
930 |
| - ignore_signatures = True |
931 |
| - if not args: |
932 |
| - usage(1) |
933 |
| - sim = Symilar( |
934 |
| - min_lines, ignore_comments, ignore_docstrings, ignore_imports, ignore_signatures |
| 881 | + logging.error(argv) |
| 882 | + parser = argparse.ArgumentParser( |
| 883 | + prog="symilar", description="Finds copy pasted blocks in a set of files." |
| 884 | + ) |
| 885 | + parser.add_argument("files", nargs="+") |
| 886 | + parser.add_argument( |
| 887 | + "-d", "--duplicates", type=int, default=DEFAULT_MIN_SIMILARITY_LINE |
| 888 | + ) |
| 889 | + parser.add_argument("-i", "--ignore-comments", action="store_true") |
| 890 | + parser.add_argument("--ignore-docstrings", action="store_true") |
| 891 | + parser.add_argument("--ignore-imports", action="store_true") |
| 892 | + parser.add_argument("--ignore-signatures", action="store_true") |
| 893 | + parsed_args = parser.parse_args(args=argv) |
| 894 | + similar_runner = Symilar( |
| 895 | + min_lines=parsed_args.duplicates, |
| 896 | + ignore_comments=parsed_args.ignore_comments, |
| 897 | + ignore_docstrings=parsed_args.ignore_docstrings, |
| 898 | + ignore_imports=parsed_args.ignore_imports, |
| 899 | + ignore_signatures=parsed_args.ignore_signatures, |
935 | 900 | )
|
936 |
| - for filename in args: |
| 901 | + logging.error(parsed_args.files) |
| 902 | + for filename in parsed_args.files: |
937 | 903 | with open(filename, encoding="utf-8") as stream:
|
938 |
| - sim.append_stream(filename, stream) |
939 |
| - sim.run() |
| 904 | + similar_runner.append_stream(filename, stream) |
| 905 | + similar_runner.run() |
| 906 | + # the sys exit must be kept because of the unit tests that rely on it |
940 | 907 | sys.exit(0)
|
941 | 908 |
|
942 | 909 |
|
|
0 commit comments