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 059589c commit 9c9dcf1
Show file tree
Hide file tree
Showing 2 changed files with 26 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 @@ -157,7 +172,7 @@ def run() -> None:
sys.exit(-22)

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


if __name__ == "__main__":
Expand Down
13 changes: 9 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, interactive: bool, cli_cmds: Optional[List[str]]) -> None: # pylint: disable=too-many-branches
"""Enter interactive loop.
This will:
Expand All @@ -148,7 +148,7 @@ def run(self, cli_cmds: Optional[List[str]]) -> None: # pylint: disable=too-man
# 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 @@ -173,8 +173,13 @@ 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:
# Clear arguments and continue with interactive session.
cli_cmds = None
continue

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

0 comments on commit 9c9dcf1

Please sign in to comment.