From cd9fbca8ea5cf5b4998c2f23a7fbe970748e8b48 Mon Sep 17 00:00:00 2001 From: JTignor-Raltron <114696222+JTignor-Raltron@users.noreply.github.com> Date: Fri, 26 Jan 2024 11:04:54 -0500 Subject: [PATCH] Fix commands with arguments that start with "-" (#182) * Fix commands with arguments that start with "-" Parameters that start with "-" are interpreted as a new argument. argparse.REMAINDER forces it to interpret all everything left in the command as a list of strings instead of new arguments. Fixes issue #177 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add test * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update tests/_legacy/test_cwp.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: jaimergp --- menuinst/_legacy/cwp.py | 8 +++++--- tests/_legacy/test_cwp.py | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/_legacy/test_cwp.py diff --git a/menuinst/_legacy/cwp.py b/menuinst/_legacy/cwp.py index df8086b3..93d56282 100644 --- a/menuinst/_legacy/cwp.py +++ b/menuinst/_legacy/cwp.py @@ -11,15 +11,17 @@ from menuinst.knownfolders import FOLDERID, get_folder_path -def main(): +def main(argv=None): # call as: python cwp.py [--no-console] PREFIX ARGs... parser = argparse.ArgumentParser() parser.add_argument( "--no-console", action="store_true", help="Create subprocess with CREATE_NO_WINDOW flag." ) parser.add_argument("prefix", help="Prefix to be 'activated' before calling `args`.") - parser.add_argument("args", nargs="*", help="Command (and arguments) to be executed.") - parsed_args = parser.parse_args() + parser.add_argument( + "args", nargs=argparse.REMAINDER, help="Command (and arguments) to be executed." + ) + parsed_args = parser.parse_args(argv) no_console = parsed_args.no_console prefix = parsed_args.prefix diff --git a/tests/_legacy/test_cwp.py b/tests/_legacy/test_cwp.py new file mode 100644 index 00000000..1222a17f --- /dev/null +++ b/tests/_legacy/test_cwp.py @@ -0,0 +1,21 @@ +import sys +from subprocess import check_output + +import pytest + +cwp = pytest.importorskip("menuinst._legacy.cwp", reason="Windows only") + + +def test_cwp(): + out = check_output( + [ + sys.executable, + cwp.__file__, + sys.prefix, + "python", + "-c", + "import sys; print(sys.prefix)", + ], + text=True, + ) + assert out.strip() == sys.prefix