Skip to content

Commit

Permalink
Fix commands with arguments that start with "-" (#182)
Browse files Browse the repository at this point in the history
* 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 <jaimergp@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 26, 2024
1 parent 1df31c6 commit cd9fbca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions menuinst/_legacy/cwp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/_legacy/test_cwp.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cd9fbca

Please sign in to comment.