Skip to content

Commit 3717a09

Browse files
⏪️ Do terminate in interactive sessions
The change was reverted to keep consistent behavior. It is also better for typing.
1 parent 9e232cc commit 3717a09

2 files changed

Lines changed: 9 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
- Better support for inspecting subcommand arguments (via
2020
`powercli.parser.ParsedCommand.subcommand()`).
2121
- Usage section in help now includes parent commands names.
22-
- Program no longer terminates when using common subcommands/flags like `help`
23-
when using commands in an interactive session (e.g. Python REPL).
2422
- `list` command and `--list` flag now display all nested subcommands.
2523
- `list` command and `--list` flag now display commands with their description.
2624

powercli/common.py

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .static import Static
2626

2727

28-
class HelpCommand(Command[Never, Never]):
28+
class HelpCommand(Command[Any, Any]):
2929
"""A help command."""
3030

3131
def __init__(
@@ -73,7 +73,7 @@ def _subcommand_of_parent(self, name: str) -> Command[Never, Never]:
7373

7474
def parse_args(
7575
self, args: list[str] | None = None
76-
) -> parser.ParsedCommand[Never, Never]:
76+
) -> Never:
7777
"""Parses arguments like {py:meth}`powercli.command.Command.parse_args`.
7878
7979
This function will print a help message and exit.
@@ -87,35 +87,15 @@ def parse_args(
8787
target = target or self.parent
8888
assert target is not None
8989
_print_and_exit(help_message(target), status=self.status, file=self.file)
90-
return _dummy_parsed_command(self)
9190

9291

93-
def _print_and_exit(*args: Any, status: int = 0, **kwargs: Any) -> None:
92+
def _print_and_exit(*args: Any, status: int = 0, **kwargs: Any) -> Never:
9493
"""Prints text and exits with `status`.
9594
9695
This function does not exit if running interactively.
9796
"""
9897
print(*args, **kwargs)
99-
if not _interactive():
100-
sys.exit(status)
101-
102-
103-
def _dummy_parsed_command(
104-
command: Command[Any, Any],
105-
) -> parser.ParsedCommand[Never, Never]:
106-
"""Returns a dummy instance of a parsed command.
107-
108-
This is only used for commands that usually exit the program but not when
109-
running that command in an interactive session (e.g. Python REPL).
110-
"""
111-
return parser.ParsedCommand(
112-
parsed_subcommand=None,
113-
parsed_positionals=[],
114-
parsed_variadic_positional=None,
115-
parsed_flags=[],
116-
raw_args=[],
117-
command=command,
118-
)
98+
sys.exit(status)
11999

120100

121101
class HelpFlag(Flag[Any, Any, None]):
@@ -153,7 +133,7 @@ def __init__(
153133
)
154134

155135

156-
class VersionCommand(Command[Never, Never]):
136+
class VersionCommand(Command[Any, Any]):
157137
"""A version command."""
158138

159139
def __init__(
@@ -176,14 +156,13 @@ def __init__(
176156

177157
def parse_args(
178158
self, args: list[str] | None = None
179-
) -> parser.ParsedCommand[Never, Never]:
159+
) -> Never:
180160
"""Parses arguments like {py:meth}`powercli.command.Command.parse_args`.
181161
182162
This function will print the version and exit.
183163
"""
184164
_pargs = super().parse_args(args)
185165
_print_and_exit(self.version, file=self.file)
186-
return _dummy_parsed_command(self)
187166

188167

189168
class VersionFlag(Flag[Any, Any, None]):
@@ -207,7 +186,7 @@ def __init__(
207186
super().__init__(
208187
long=long,
209188
description=description,
210-
method=Switch[Any, Any, None, None](
189+
method=Switch[Any, Any, Never, None](
211190
on_presence=lambda ctx: _print_and_exit(version, file=ctx.command.file),
212191
on_absence=Static(None),
213192
),
@@ -216,11 +195,6 @@ def __init__(
216195
self.version = version
217196

218197

219-
def _interactive() -> bool:
220-
"""Returns whether the user is running in interactive mode."""
221-
return hasattr(sys, "ps1")
222-
223-
224198
def _list_message(cmd: Command[Any, Any], *, parents: list[str] | None = None) -> str:
225199
"""Creates a string representation that lists every subcommand of `cmd`."""
226200
lines = []
@@ -245,7 +219,7 @@ def _list_message(cmd: Command[Any, Any], *, parents: list[str] | None = None) -
245219
return "\n".join(lines)
246220

247221

248-
class ListCommand(Command[Never, Never]):
222+
class ListCommand(Command[Any, Any]):
249223
"""A list command."""
250224

251225
def __init__(
@@ -259,15 +233,14 @@ def __init__(
259233

260234
def parse_args(
261235
self, args: list[str] | None = None
262-
) -> parser.ParsedCommand[Never, Never]:
236+
) -> Never:
263237
"""Parses arguments like {py:meth}`powercli.command.Command.parse_args`.
264238
265239
This function will print a list of subcommands and exit.
266240
"""
267241
_pargs = super().parse_args(args)
268242
assert self.parent is not None
269243
_print_and_exit(_list_message(self.parent), file=self.file)
270-
return _dummy_parsed_command(self)
271244

272245

273246
class ListFlag(Flag[Any, Any, None]):

0 commit comments

Comments
 (0)