Skip to content

Commit f42cdb2

Browse files
committed
Move argparse parsing of CLI args back to cmdloop() from __init__()
This is so unit tests pass
1 parent 77633bc commit f42cdb2

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

cmd2/cmd2.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -472,23 +472,10 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
472472
self._startup_commands.append("load '{}'".format(startup_script))
473473

474474
# Transcript files to run instead of interactive command loop
475-
self._transcript_files = None
475+
self._transcript_files = transcript_files
476476

477-
# Check for command line args
478-
if allow_cli_args:
479-
parser = argparse.ArgumentParser()
480-
parser.add_argument('-t', '--test', action="store_true",
481-
help='Test against transcript(s) in FILE (wildcards OK)')
482-
callopts, callargs = parser.parse_known_args()
483-
484-
# If transcript testing was called for, use other arguments as transcript files
485-
if callopts.test:
486-
self._transcript_files = callargs
487-
# If commands were supplied at invocation, then add them to the command queue
488-
elif callargs:
489-
self._startup_commands.extend(callargs)
490-
elif transcript_files:
491-
self._transcript_files = transcript_files
477+
# Should commands at invocation and -t/--test transcript test running be allowed
478+
self._allow_cli_args = allow_cli_args
492479

493480
# The default key for sorting tab completion matches. This only applies when the matches are not
494481
# already marked as sorted by setting self.matches_sorted to True. Its default value performs a
@@ -4026,6 +4013,20 @@ def cmdloop(self, intro: Optional[str] = None) -> int:
40264013
original_sigint_handler = signal.getsignal(signal.SIGINT)
40274014
signal.signal(signal.SIGINT, self.sigint_handler)
40284015

4016+
# Check for command line args
4017+
if self._allow_cli_args:
4018+
parser = argparse.ArgumentParser()
4019+
parser.add_argument('-t', '--test', action="store_true",
4020+
help='Test against transcript(s) in FILE (wildcards OK)')
4021+
callopts, callargs = parser.parse_known_args()
4022+
4023+
# If transcript testing was called for, use other arguments as transcript files
4024+
if callopts.test:
4025+
self._transcript_files = callargs
4026+
# If commands were supplied at invocation, then add them to the command queue
4027+
elif callargs:
4028+
self._startup_commands.extend(callargs)
4029+
40294030
# Grab terminal lock before the prompt has been drawn by readline
40304031
self.terminal_lock.acquire()
40314032

0 commit comments

Comments
 (0)