Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: need quote for help subcommand #302

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/cleo/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,32 @@ def _run(self, io: IO) -> int:
io.input.set_stream(stream)
io.input.interactive(interactive)

if name == "help" and isinstance(io.input, ArgvInput):
# If the command is `help` we suppose
# that all without `-` is a command (possible namespaced)
argv = io.input._tokens[:]

if io.input.script_name is not None:
argv.insert(0, io.input.script_name)
else:
# Needs because `ApplicationTester` doesn't add script name
argv.insert(0, self.name)

# filter all words after `help` without `-`
start = argv.index(name)
words = list(filter(lambda arg: "-" not in arg, argv[start + 1 :]))

if words:
index = argv.index(words[0])
argv[index] = " ".join(argv[index:])
del argv[index + 1 :]

stream = io.input.stream
interactive = io.input.is_interactive()
io.set_input(ArgvInput(argv))
io.input.set_stream(stream)
io.input.interactive(interactive)

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

Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/application_help_multiple_word.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Description:
The foo bar command

Usage:
foo bar [options]
afoobar

Options:
-h, --help Display help for the given command. When no command is given display help for the list command.
-q, --quiet Do not output any message.
-V, --version Display this application version.
--ansi Force ANSI output.
--no-ansi Disable ANSI output.
-n, --no-interaction Do not ask any interactive question.
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug.
15 changes: 15 additions & 0 deletions tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ def test_silent_help(app: Application) -> None:
assert tester.io.fetch_output() == ""


def test_help_multiple_word_command(app: Application) -> None:
app.catch_exceptions(False)

foo = FooCommand()
app.add(foo)

tester = ApplicationTester(app)
tester.execute("help foo bar")

assert (
tester.io.fetch_output()
== FIXTURES_PATH.joinpath("application_help_multiple_word.txt").read_text()
)


def test_get_namespaces(app: Application) -> None:
app.add(FooCommand())
app.add(Foo1Command())
Expand Down