Skip to content

Commit

Permalink
Accept multiple files on command line. Fixes issue #935.
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido van Rossum authored and JukkaL committed Dec 3, 2015
1 parent fb09f7d commit 0555178
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
10 changes: 9 additions & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def default_lib_path(data_dir: str, pyversion: Tuple[int, int],


def lookup_program(module: str, lib_path: List[str]) -> str:
# Modules are .py and not .pyi
# Modules are .py or .pyi
path = find_module(module, lib_path)
if path:
return path
Expand Down Expand Up @@ -544,12 +544,20 @@ def log(self, message: str) -> None:
def remove_cwd_prefix_from_path(p: str) -> str:
"""Remove current working directory prefix from p, if present.
Also crawl up until a directory without __init__.py is found.
If the result would be empty, return '.' instead.
"""
cur = os.getcwd()
# Add separator to the end of the path, unless one is already present.
if basename(cur) != '':
cur += os.sep
# Compute root path.
while p and os.path.isfile(os.path.join(p, '__init__.py')):
dir, base = os.path.split(p)
if not base:
break
p = dir
# Remove current directory prefix from the path, if present.
if p.startswith(cur):
p = p[len(cur):]
Expand Down
30 changes: 24 additions & 6 deletions mypy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
module to run as script (or None),
parsed flags)
"""
# TODO: Rewrite using argparse.
options = Options()
help = False
ver = False
Expand Down Expand Up @@ -171,14 +172,30 @@ def process_options(args: List[str]) -> Tuple[List[BuildSource], Options]:
if not args:
usage('Missing target file or module')

if args[1:]:
usage('Extra argument: {}'.format(args[1]))

if options.python_path and options.pyversion[0] == 2:
usage('Python version 2 (or --py2) specified, '
'but --use-python-path will search in sys.path of Python 3')

return [BuildSource(args[0], None, None)], options
return [BuildSource(arg, file_to_mod(arg), None) for arg in args], options


def file_to_mod(arg: str) -> str:
"""Convert a .py filename to a module name.
We crawl up the path until we find a directory without __init__.py.
"""
if not arg.endswith('.py'):
return '__main__' # Special case for entry scripts.
dir, mod = os.path.split(arg)
if mod.endswith('.py'):
mod = mod[:-3]
assert '.' not in mod
while dir and os.path.isfile(os.path.join(dir, '__init__.py')):
dir, base = os.path.split(dir)
if not base:
break
mod = base + '.' + mod
return mod


# Don't generate this from mypy.reports, not all are meant to be public.
Expand All @@ -200,15 +217,16 @@ def is_report(arg: str) -> bool:


def usage(msg: str = None) -> None:
# TODO: Add other supported options (--package, -f/--dirty-stubs, ...)
if msg:
sys.stderr.write('%s\n' % msg)
sys.stderr.write("""\
usage: mypy [option ...] [-c cmd | -m mod | file]
usage: mypy [option ...] [-c cmd | -m mod | file ...]
Try 'mypy -h' for more information.
""")
else:
sys.stderr.write("""\
usage: mypy [option ...] [-m mod | file]
usage: mypy [option ...] [-c cmd | -m mod | file ...]
Optional arguments:
-h, --help print this help message and exit
Expand Down

0 comments on commit 0555178

Please sign in to comment.