expand patterns in Windows args
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 08583da · 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
Provenance
- Commit
ee4feaa- Tree
c6a320f- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
08583da
4 files changed+69 added−3 removed
modified:src/click/core.py+11 added-3 removed
28 unmodified lines29 from .types import BOOL30 from .types import convert_type31 from .types import IntRange32 from .utils import _detect_program_name33+from .utils import _expand_args34 from .utils import echo35 from .utils import make_default_short_help36 from .utils import make_str37 from .utils import PacifyFlushWrapper865 unmodified lines903 904 This method is also available by directly calling the instance of905 a :class:`Command`.906 906- .. versionadded:: 3.0907- Added the `standalone_mode` flag to control the standalone mode.908-907 :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 file12 unmodified lines923 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.927+928+ .. versionchanged:: 8.0929+ When taking arguments from ``sys.argv`` on Windows, glob930+ patterns, user dir, and env vars are expanded.931+932+ .. 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()938 939 if args is None:940 args = sys.argv[1:]941+942+ if os.name == "nt":943+ args = _expand_args(args)944 else:945 args = list(args)946 947 if prog_name is None:1675 unmodified linesmodified:src/click/utils.py+46 added
1 import os2 import sys3+import typing as t4 5 from ._compat import _default_text_stderr6 from ._compat import _default_text_stdout7 from ._compat import _find_binary_writer474 unmodified lines482 if name != "__main__":483 py_module = f"{py_module}.{name}"484 485 return f"python -m {py_module.lstrip('.')}"486+487+488+def _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.496+497+ See :func:`glob.glob`, :func:`os.path.expanduser`, and498+ :func:`os.path.expandvars`.499+500+ 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.502+503+ :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.507+508+ .. versionadded:: 8.0509+510+ :meta private:511+ """512+ from glob import glob513+514+ out = []515+516+ for arg in args:517+ if user:518+ arg = os.path.expanduser(arg)519+520+ if env:521+ arg = os.path.expandvars(arg)522+523+ matches = glob(arg, recursive=glob_recursive)524+525+ if not matches:526+ out.append(arg)527+ else:528+ out.extend(matches)529+530+ return outmodified:tests/test_utils.py+10 added
427 unmodified lines428 ],429 )430 def test_detect_program_name(path, main, expected):431 assert click.utils._detect_program_name(path, _main=main) == expected432+433+434+def 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 lines191 :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`197 198 199 Version 7.1.2200 -------------635 unmodified lines