summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-02-15 19:56:29 -0500
committerGitHub <noreply@github.com>2017-02-15 19:56:29 -0500
commit9b8bc42a7a6f04d8e83830ba4f87ffe3aa010eb4 (patch)
tree39d6a289f12147b0f6bca8b58b0113146aacb596 /cmd2.py
parent65aa0436a7bf72ff17f59811c14385219917de67 (diff)
parent38b46e1b5d934ae5c4a4f70faa1e4fb7708f0935 (diff)
downloadcmd2-git-9b8bc42a7a6f04d8e83830ba4f87ffe3aa010eb4.tar.gz
Merge pull request #44 from python-cmd2/cmds_at_invocation
Add option to prevent processing of CLI args.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py33
1 files changed, 25 insertions, 8 deletions
diff --git a/cmd2.py b/cmd2.py
index 239a04fc..8540252c 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -502,6 +502,7 @@ class Cmd(cmd.Cmd):
# Attributes which are NOT dynamically settable at runtime
_STOP_AND_EXIT = True # distinguish end of script file from actual exit
_STOP_SCRIPT_NO_EXIT = -999
+ allow_cli_args = True
blankLinesAllowed = False
colorcodes = {'bold': {True: '\x1b[1m', False: '\x1b[22m'},
'cyan': {True: '\x1b[36m', False: '\x1b[39m'},
@@ -1119,7 +1120,7 @@ class Cmd(cmd.Cmd):
line = line[:-1]
return line
- def _cmdloop(self, intro=None):
+ def _cmdloop(self):
"""Repeatedly issue a prompt, accept input, parse an initial prefix
off the received input, and dispatch to action methods, passing them
the remainder of the line as argument.
@@ -1136,8 +1137,6 @@ class Cmd(cmd.Cmd):
pass
stop = None
try:
- if intro is not None:
- self.stdout.write(str(self.intro) + "\n")
while not stop:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
@@ -1576,7 +1575,7 @@ relative to the already-running script's directory.
self.use_rawinput = False
self.prompt = self.continuation_prompt = ''
self.current_script_dir = os.path.split(targetname)[0]
- stop = self._cmdloop(None)
+ stop = self._cmdloop()
self.stdin.close()
keepstate.restore()
self.lastcmd = ''
@@ -1645,10 +1644,28 @@ Script should contain one command per line, just like command would be typed in
if callopts.test:
self.runTranscriptTests(callargs)
else:
- if not self.run_commands_at_invocation(callargs):
- self.preloop()
- self._cmdloop(self.intro)
- self.postloop()
+ # Always run the preloop first
+ self.preloop()
+
+ # If an intro was supplied in the method call, allow it to override the default
+ if intro is not None:
+ self.intro = intro
+
+ # Print the intro, if there is one, right after the preloop
+ if self.intro is not None:
+ self.stdout.write(str(self.intro) + "\n")
+
+ stop = False
+ # If allowed, process any commands present as arguments on the command-line, if allowed
+ if self.allow_cli_args:
+ stop = self.run_commands_at_invocation(callargs)
+
+ # And then call _cmdloop() if there wasn't something in those causing us to quit
+ if not stop:
+ self._cmdloop()
+
+ # Run the postloop() no matter what
+ self.postloop()
class HistoryItem(str):