@@ -99,6 +99,7 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
99
99
module to run as script (or None),
100
100
parsed flags)
101
101
"""
102
+ # TODO: Rewrite using argparse.
102
103
options = Options ()
103
104
help = False
104
105
ver = False
@@ -171,14 +172,30 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
171
172
if not args :
172
173
usage ('Missing target file or module' )
173
174
174
- if args [1 :]:
175
- usage ('Extra argument: {}' .format (args [1 ]))
176
-
177
175
if options .python_path and options .pyversion [0 ] == 2 :
178
176
usage ('Python version 2 (or --py2) specified, '
179
177
'but --use-python-path will search in sys.path of Python 3' )
180
178
181
- return [BuildSource (args [0 ], None , None )], options
179
+ return [BuildSource (arg , file_to_mod (arg ), None ) for arg in args ], options
180
+
181
+
182
+ def file_to_mod (arg : str ) -> str :
183
+ """Convert a .py filename to a module name.
184
+
185
+ We crawl up the path until we find a directory without __init__.py.
186
+ """
187
+ if not arg .endswith ('.py' ):
188
+ return '__main__' # Special case for entry scripts.
189
+ dir , mod = os .path .split (arg )
190
+ if mod .endswith ('.py' ):
191
+ mod = mod [:- 3 ]
192
+ assert '.' not in mod
193
+ while dir and os .path .isfile (os .path .join (dir , '__init__.py' )):
194
+ dir , base = os .path .split (dir )
195
+ if not base :
196
+ break
197
+ mod = base + '.' + mod
198
+ return mod
182
199
183
200
184
201
# Don't generate this from mypy.reports, not all are meant to be public.
@@ -200,15 +217,16 @@ def is_report(arg: str) -> bool:
200
217
201
218
202
219
def usage (msg : str = None ) -> None :
220
+ # TODO: Add other supported options (--package, -f/--dirty-stubs, ...)
203
221
if msg :
204
222
sys .stderr .write ('%s\n ' % msg )
205
223
sys .stderr .write ("""\
206
- usage: mypy [option ...] [-c cmd | -m mod | file]
224
+ usage: mypy [option ...] [-c cmd | -m mod | file ... ]
207
225
Try 'mypy -h' for more information.
208
226
""" )
209
227
else :
210
228
sys .stderr .write ("""\
211
- usage: mypy [option ...] [-m mod | file]
229
+ usage: mypy [option ...] [-c cmd | - m mod | file ... ]
212
230
213
231
Optional arguments:
214
232
-h, --help print this help message and exit
0 commit comments