|
5 | 5 | from pathlib import Path |
6 | 6 | from typing import List, Optional, Tuple, Union |
7 | 7 |
|
8 | | -from unimport import __description__, __version__ |
| 8 | +import unimport.constants as C |
9 | 9 | from unimport.color import Color |
10 | 10 | from unimport.session import Session |
11 | 11 | from unimport.statement import Import, ImportFrom |
12 | 12 |
|
13 | | -parser = argparse.ArgumentParser( |
14 | | - prog="unimport", |
15 | | - description=__description__, |
16 | | - epilog="Get rid of all unused imports 🥳", |
17 | | -) |
18 | | -exclusive_group = parser.add_mutually_exclusive_group(required=False) |
19 | | -parser.add_argument( |
20 | | - "sources", |
21 | | - default=[Path(".")], |
22 | | - nargs="*", |
23 | | - help="files and folders to find the unused imports.", |
24 | | - action="store", |
25 | | - type=Path, |
26 | | -) |
27 | | -parser.add_argument( |
28 | | - "-c", |
29 | | - "--config", |
30 | | - default=".", |
31 | | - help="read configuration from PATH.", |
32 | | - metavar="PATH", |
33 | | - action="store", |
34 | | - type=Path, |
35 | | -) |
36 | | -parser.add_argument( |
37 | | - "--include", |
38 | | - help="file include pattern.", |
39 | | - metavar="include", |
40 | | - action="store", |
41 | | - default="", |
42 | | - type=str, |
43 | | -) |
44 | | -parser.add_argument( |
45 | | - "--exclude", |
46 | | - help="file exclude pattern.", |
47 | | - metavar="exclude", |
48 | | - action="store", |
49 | | - default="", |
50 | | - type=str, |
51 | | -) |
52 | | -parser.add_argument( |
53 | | - "--include-star-import", |
54 | | - action="store_true", |
55 | | - help="Include star imports during scanning and refactor.", |
56 | | -) |
57 | | -parser.add_argument( |
58 | | - "--show-error", |
59 | | - action="store_true", |
60 | | - help="Show or don't show errors captured during static analysis.", |
61 | | -) |
62 | | -parser.add_argument( |
63 | | - "-d", |
64 | | - "--diff", |
65 | | - action="store_true", |
66 | | - help="Prints a diff of all the changes unimport would make to a file.", |
67 | | -) |
68 | | -exclusive_group.add_argument( |
69 | | - "-r", |
70 | | - "--remove", |
71 | | - action="store_true", |
72 | | - help="remove unused imports automatically.", |
73 | | -) |
74 | | -exclusive_group.add_argument( |
75 | | - "-p", |
76 | | - "--permission", |
77 | | - action="store_true", |
78 | | - help="Refactor permission after see diff.", |
79 | | -) |
80 | | -parser.add_argument( |
81 | | - "--requirements", |
82 | | - action="store_true", |
83 | | - help="Include requirements.txt file, You can use it with all other arguments", |
84 | | -) |
85 | | -parser.add_argument( |
86 | | - "--check", |
87 | | - action="store_true", |
88 | | - help="Prints which file the unused imports are in.", |
89 | | -) |
90 | | -parser.add_argument( |
91 | | - "-v", |
92 | | - "--version", |
93 | | - action="version", |
94 | | - version=f"Unimport {__version__}", |
95 | | - help="Prints version of unimport", |
96 | | -) |
97 | | - |
98 | 13 |
|
99 | 14 | def color_diff(sequence: Tuple[str, ...]) -> str: |
100 | 15 | contents = "\n".join(sequence) |
@@ -149,6 +64,92 @@ def show( |
149 | 64 |
|
150 | 65 |
|
151 | 66 | def main(argv: Optional[List[str]] = None) -> int: |
| 67 | + argv = argv if argv is not None else sys.argv[1:] |
| 68 | + parser = argparse.ArgumentParser( |
| 69 | + prog="unimport", |
| 70 | + description=C.DESCRIPTION, |
| 71 | + epilog="Get rid of all unused imports 🥳", |
| 72 | + ) |
| 73 | + exclusive_group = parser.add_mutually_exclusive_group(required=False) |
| 74 | + parser.add_argument( |
| 75 | + "sources", |
| 76 | + default=[Path(".")], |
| 77 | + nargs="*", |
| 78 | + help="files and folders to find the unused imports.", |
| 79 | + action="store", |
| 80 | + type=Path, |
| 81 | + ) |
| 82 | + parser.add_argument( |
| 83 | + "-c", |
| 84 | + "--config", |
| 85 | + default=".", |
| 86 | + help="read configuration from PATH.", |
| 87 | + metavar="PATH", |
| 88 | + action="store", |
| 89 | + type=Path, |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "--include", |
| 93 | + help="file include pattern.", |
| 94 | + metavar="include", |
| 95 | + action="store", |
| 96 | + default="", |
| 97 | + type=str, |
| 98 | + ) |
| 99 | + parser.add_argument( |
| 100 | + "--exclude", |
| 101 | + help="file exclude pattern.", |
| 102 | + metavar="exclude", |
| 103 | + action="store", |
| 104 | + default="", |
| 105 | + type=str, |
| 106 | + ) |
| 107 | + parser.add_argument( |
| 108 | + "--include-star-import", |
| 109 | + action="store_true", |
| 110 | + help="Include star imports during scanning and refactor.", |
| 111 | + ) |
| 112 | + parser.add_argument( |
| 113 | + "--show-error", |
| 114 | + action="store_true", |
| 115 | + help="Show or don't show errors captured during static analysis.", |
| 116 | + ) |
| 117 | + parser.add_argument( |
| 118 | + "-d", |
| 119 | + "--diff", |
| 120 | + action="store_true", |
| 121 | + help="Prints a diff of all the changes unimport would make to a file.", |
| 122 | + ) |
| 123 | + exclusive_group.add_argument( |
| 124 | + "-r", |
| 125 | + "--remove", |
| 126 | + action="store_true", |
| 127 | + help="remove unused imports automatically.", |
| 128 | + ) |
| 129 | + exclusive_group.add_argument( |
| 130 | + "-p", |
| 131 | + "--permission", |
| 132 | + action="store_true", |
| 133 | + help="Refactor permission after see diff.", |
| 134 | + ) |
| 135 | + parser.add_argument( |
| 136 | + "--requirements", |
| 137 | + action="store_true", |
| 138 | + help="Include requirements.txt file, You can use it with all other arguments", |
| 139 | + ) |
| 140 | + parser.add_argument( |
| 141 | + "--check", |
| 142 | + action="store_true", |
| 143 | + help="Prints which file the unused imports are in.", |
| 144 | + ) |
| 145 | + parser.add_argument( |
| 146 | + "-v", |
| 147 | + "--version", |
| 148 | + action="version", |
| 149 | + version=f"Unimport {C.VERSION}", |
| 150 | + help="Prints version of unimport", |
| 151 | + ) |
| 152 | + |
152 | 153 | namespace = parser.parse_args(argv) |
153 | 154 | namespace.check = namespace.check or not any( |
154 | 155 | [namespace.diff, namespace.remove, namespace.permission] |
@@ -251,4 +252,4 @@ def main(argv: Optional[List[str]] = None) -> int: |
251 | 252 |
|
252 | 253 |
|
253 | 254 | if __name__ == "__main__": |
254 | | - sys.exit(main(sys.argv[1:])) |
| 255 | + exit(main()) |
0 commit comments