Skip to content

Commit

Permalink
feat: set --no-interaction when stdin is not a tty (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent committed Sep 2, 2022
1 parent 5d2339c commit aee0dae
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 5 additions & 1 deletion cleo/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,11 @@ def _configure_io(self, io: IO) -> None:
elif io.input.has_parameter_option("--no-ansi", True):
io.decorated(False)

if io.input.has_parameter_option(["--no-interaction", "-n"], True):
if io.input.has_parameter_option(["--no-interaction", "-n"], True) or (
io.input._interactive is None
and io.input.stream
and not io.input.stream.isatty()
):
io.interactive(False)

shell_verbosity = int(os.getenv("SHELL_VERBOSITY", 0))
Expand Down
8 changes: 4 additions & 4 deletions cleo/io/inputs/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, definition: Definition | None = None) -> None:
self._stream: TextIO = None # type: ignore[assignment]
self._options: dict[str, Any] = {}
self._arguments: dict[str, Any] = {}
self._interactive = True
self._interactive: bool | None = None

if definition is None:
self._definition = Definition()
Expand Down Expand Up @@ -56,7 +56,7 @@ def read(self, length: int, default: str = "") -> str:
"""
Reads the given amount of characters from the input stream.
"""
if not self._interactive:
if not self.is_interactive():
return default

return self._stream.read(length)
Expand All @@ -65,7 +65,7 @@ def read_line(self, length: int = -1, default: str = "") -> str:
"""
Reads a line from the input stream.
"""
if not self._interactive:
if not self.is_interactive():
return default

return self._stream.readline(length)
Expand All @@ -83,7 +83,7 @@ def is_closed(self) -> bool:
return self._stream.closed

def is_interactive(self) -> bool:
return self._interactive
return True if self._interactive is None else self._interactive

def interactive(self, interactive: bool = True) -> None:
self._interactive = interactive
Expand Down
2 changes: 1 addition & 1 deletion cleo/testers/application_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def execute(
self,
args: str = "",
inputs: str | None = None,
interactive: bool | None = None,
interactive: bool = True,
verbosity: Verbosity | None = None,
decorated: bool = False,
supports_utf8: bool = True,
Expand Down

0 comments on commit aee0dae

Please sign in to comment.