Skip to content
Myr

Merge pull request #1830 from pallets/windows-glob

Before adoption: this commit came in with the history the project adopted, so no landing records it.

expand patterns in Windows args

authored by David Lord · committed by GitHub · parents 08583da, ee4feaa · Browse files · 4 files+69 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+69 added−3 removed

  • tests:+10 added−0 removed
  • other:+59 added−3 removed

4 files+69 added−3 removed

modified:src/click/core.py+11 added-3 removed
28 unmodified lines
29from .types import BOOL30from .types import convert_type31from .types import IntRange32from .utils import _detect_program_name33from .utils import _expand_args34from .utils import echo35from .utils import make_default_short_help36from .utils import make_str37from .utils import PacifyFlushWrapper
865 unmodified lines
903904 This method is also available by directly calling the instance of905 a :class:`Command`.906906 .. versionadded:: 3.0907 Added the `standalone_mode` flag to control the standalone mode.908907 :param args: the arguments that should be used for parsing. If not908 provided, ``sys.argv[1:]`` is used.909 :param prog_name: the program name that should be used. By default910 the program name is constructed by taking the file
12 unmodified lines
923 value of this function is the return value924 of :meth:`invoke`.925 :param extra: extra keyword arguments are forwarded to the context926 constructor. See :class:`Context` for more information.927928 .. versionchanged:: 8.0929 When taking arguments from ``sys.argv`` on Windows, glob930 patterns, user dir, and env vars are expanded.931932 .. versionchanged:: 3.0933 Added the ``standalone_mode`` parameter.934 """935 # Verify that the environment is configured correctly, or reject936 # further execution to avoid a broken script.937 _verify_python_env()938939 if args is None:940 args = sys.argv[1:]941942 if os.name == "nt":943 args = _expand_args(args)944 else:945 args = list(args)946947 if prog_name is None:
1675 unmodified lines
modified:src/click/utils.py+46 added
1import os2import sys3import typing as t45from ._compat import _default_text_stderr6from ._compat import _default_text_stdout7from ._compat import _find_binary_writer
474 unmodified lines
482 if name != "__main__":483 py_module = f"{py_module}.{name}"484485 return f"python -m {py_module.lstrip('.')}"486487488def _expand_args(489 args: t.Iterable[str],490 *,491 user: bool = True,492 env: bool = True,493 glob_recursive: bool = True,494) -> t.List[str]:495 """Simulate Unix shell expansion with Python functions.496497 See :func:`glob.glob`, :func:`os.path.expanduser`, and498 :func:`os.path.expandvars`.499500 This intended for use on Windows, where the shell does not do any501 expansion. It may not exactly match what a Unix shell would do.502503 :param args: List of command line arguments to expand.504 :param user: Expand user home directory.505 :param env: Expand environment variables.506 :param glob_recursive: ``**`` matches directories recursively.507508 .. versionadded:: 8.0509510 :meta private:511 """512 from glob import glob513514 out = []515516 for arg in args:517 if user:518 arg = os.path.expanduser(arg)519520 if env:521 arg = os.path.expandvars(arg)522523 matches = glob(arg, recursive=glob_recursive)524525 if not matches:526 out.append(arg)527 else:528 out.extend(matches)529530 return out
modified:tests/test_utils.py+10 added
427 unmodified lines
428 ],429)430def test_detect_program_name(path, main, expected):431 assert click.utils._detect_program_name(path, _main=main) == expected432433434def test_expand_args(monkeypatch):435 user = os.path.expanduser("~")436 assert user in click.utils._expand_args(["~"])437 monkeypatch.setenv("CLICK_TEST", "hello")438 assert "hello" in click.utils._expand_args(["$CLICK_TEST"])439 assert "setup.cfg" in click.utils._expand_args(["*.cfg"])440 assert os.path.join("docs", "conf.py") in click.utils._expand_args(["**/conf.py"])441 assert "*.not-found" in click.utils._expand_args(["*.not-found"])
modified:CHANGES.rst+2 added
190 unmodified lines
191 :issue:`1813`192- Command deprecation notice appears at the start of the help text, as193 well as in the short help. The notice is not in all caps.194 :issue:`1791`195- When taking arguments from ``sys.argv`` on Windows, glob patterns,196 user dir, and env vars are expanded. :issue:`1096`197198199Version 7.1.2200-------------
635 unmodified lines