Skip to content

Commit

Permalink
cli: add -i option to enter interactive loop after command line argum…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
pillo79 committed Feb 29, 2024
1 parent 707002a commit 19b8a4b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
19 changes: 17 additions & 2 deletions src/dtsh/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ def __init__(self) -> None:
metavar="FILE",
)

self._parser.add_argument(
grp_session_ctrl = self._parser.add_argument_group("session control")
grp_session_ctrl.add_argument(
"-c",
help="execute commands from CMD instead of the standard input",
action="append",
metavar="CMD",
)
grp_session_ctrl.add_argument(
"-i",
"--interactive",
help="enter interactive loop after command line arguments",
action="store_true",
)

self._argv = self._parser.parse_args()

Expand Down Expand Up @@ -112,6 +119,14 @@ def cli_cmds(self) -> Optional[List[str]]:
return cast(List[str], self._argv.c)
return None

@property
def interactive(self) -> bool:
"""Is the interactive loop requested?"""
if not self._argv.c:
# no commands, must be interactive
return True
return self._argv.interactive


def _load_preference_file(path: str) -> None:
try:
Expand Down Expand Up @@ -159,7 +174,7 @@ def run() -> None:
sys.exit(-22)

if session:
session.run(argv.cli_cmds)
session.run(argv.cli_cmds, argv.interactive)


if __name__ == "__main__":
Expand Down
15 changes: 11 additions & 4 deletions src/dtsh/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def __init__(
self._autocomp.display,
)

def run(self, cli_cmds: Optional[List[str]]) -> None: # pylint: disable=too-many-branches
def run(self, cli_cmds: Optional[List[str]], interactive: bool) -> None: # pylint: disable=too-many-branches
"""Enter command processing loop.
This will:
Expand All @@ -146,12 +146,13 @@ def run(self, cli_cmds: Optional[List[str]]) -> None: # pylint: disable=too-man
Args:
cli_cmds: List of command lines to execute in batch mode.
interactive: True if the session will enter an interactive loop.
"""
# closing() the session when the pager is active breaks the TTY.
# As a work-around, we ignore SIGINT.
signal.signal(signal.SIGINT, self._sig_handler)

if not cli_cmds:
if interactive:
self._vt.clear()
self.preamble_hook()

Expand All @@ -177,8 +178,14 @@ def run(self, cli_cmds: Optional[List[str]]) -> None: # pylint: disable=too-man
# Exit DTSh on EOF.
self.close()
except IndexError:
# Exit DTSh on end of command line args.
self.close(interactive=False)
if not interactive:
# Exit DTSh on end of command line args.
self.close(interactive=False)
else:
# Forget about CLI commands and continue with
# an interactive session.
cli_cmds = None
continue

if cmdline:
if cmdline.strip() in ["q", "quit", "exit"]:
Expand Down

0 comments on commit 19b8a4b

Please sign in to comment.