Skip to content

Commit 2652a99

Browse files
authored
Merge pull request #96 from sdispater/fix-argv-input-reconstruction
Fix argv input reconstruction for namespace commands
2 parents a56c99d + 09bab75 commit 2652a99

File tree

2 files changed

+48
-7
lines changed

2 files changed

+48
-7
lines changed

cleo/application.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,10 @@ def _run(self, io: IO) -> int:
388388
# If the command is namespaced we rearrange
389389
# the input to parse it as a single argument
390390
if isinstance(io.input, ArgvInput):
391-
argv = sys.argv[:]
391+
argv = io._input._tokens[:]
392+
393+
if io.input.script_name is not None:
394+
argv.insert(0, io.input.script_name)
392395

393396
namespace = name.split(" ")[0]
394397
index = None
@@ -401,7 +404,7 @@ def _run(self, io: IO) -> int:
401404
if index is not None:
402405
del argv[index + 1 : index + 1 + (len(name.split(" ")) - 1)]
403406

404-
io.set_input(ArgvInput(argv, definition=io.input._definition))
407+
io.set_input(ArgvInput(argv))
405408

406409
exit_code = self._run_command(command, io)
407410
self._running_command = None

tests/testers/test_application_tester.py

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,56 @@
22

33
from cleo.application import Application
44
from cleo.commands.command import Command
5+
from cleo.helpers import argument
6+
from cleo.helpers import option
57
from cleo.testers.application_tester import ApplicationTester
68

79

810
class FooCommand(Command):
911
"""
1012
Foo command
13+
"""
14+
15+
name = "foo"
16+
17+
description = "Foo command"
18+
19+
arguments = [argument("foo")]
20+
21+
options = [option("--bar")]
22+
23+
def handle(self):
24+
self.line(self.argument("foo"))
25+
26+
if self.option("bar"):
27+
self.line("--bar activated")
28+
1129

12-
foo
13-
{foo : Foo argument}
30+
class FooBarCommand(Command):
1431
"""
32+
Foo Bar command
33+
"""
34+
35+
name = "foo bar"
36+
37+
description = "Foo Bar command"
38+
39+
arguments = [argument("foo")]
40+
41+
options = [option("--baz")]
1542

1643
def handle(self):
1744
self.line(self.argument("foo"))
1845

46+
if self.option("baz"):
47+
self.line("--baz activated")
48+
1949

2050
@pytest.fixture()
2151
def app():
2252
app = Application()
2353
app.add(FooCommand())
54+
app.add(FooBarCommand())
2455

2556
return app
2657

@@ -30,7 +61,14 @@ def tester(app):
3061
return ApplicationTester(app)
3162

3263

33-
def test_execute(tester):
34-
assert 0 == tester.execute("foo bar")
64+
def test_execute(tester: ApplicationTester):
65+
assert 0 == tester.execute("foo baz --bar")
66+
assert 0 == tester.status_code
67+
assert "baz\n--bar activated\n" == tester.io.fetch_output()
68+
69+
70+
def test_execute_namespace_command(tester: ApplicationTester):
71+
tester.application.catch_exceptions(False)
72+
assert 0 == tester.execute("foo bar baz --baz")
3573
assert 0 == tester.status_code
36-
assert "bar\n" == tester.io.fetch_output()
74+
assert "baz\n--baz activated\n" == tester.io.fetch_output()

0 commit comments

Comments
 (0)