本文整理汇总了Python中cmd.cmdloop方法的典型用法代码示例。如果您正苦于以下问题:Python cmd.cmdloop方法的具体用法?Python cmd.cmdloop怎么用?Python cmd.cmdloop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cmd
的用法示例。
在下文中一共展示了cmd.cmdloop方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_input_reset_at_EOF
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def test_input_reset_at_EOF(self):
input = StringIO.StringIO("print test\nprint test2")
output = StringIO.StringIO()
cmd = self.simplecmd2(stdin=input, stdout=output)
cmd.use_rawinput = False
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) test\n"
"(Cmd) test2\n"
"(Cmd) *** Unknown syntax: EOF\n"))
input = StringIO.StringIO("print \n\n")
output = StringIO.StringIO()
cmd.stdin = input
cmd.stdout = output
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) \n"
"(Cmd) \n"
"(Cmd) *** Unknown syntax: EOF\n"))
示例2: test_input_reset_at_EOF
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def test_input_reset_at_EOF(self):
input = io.StringIO("print test\nprint test2")
output = io.StringIO()
cmd = self.simplecmd2(stdin=input, stdout=output)
cmd.use_rawinput = False
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) test\n"
"(Cmd) test2\n"
"(Cmd) *** Unknown syntax: EOF\n"))
input = io.StringIO("print \n\n")
output = io.StringIO()
cmd.stdin = input
cmd.stdout = output
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) \n"
"(Cmd) \n"
"(Cmd) *** Unknown syntax: EOF\n"))
示例3: test_file_with_missing_final_nl
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def test_file_with_missing_final_nl(self):
input = StringIO.StringIO("print test\nprint test2")
output = StringIO.StringIO()
cmd = self.simplecmd(stdin=input, stdout=output)
cmd.use_rawinput = False
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) test\n"
"(Cmd) test2\n"
"(Cmd) "))
示例4: runcmds_plus_hooks
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def runcmds_plus_hooks(self, cmds: List[str]) -> bool:
"""Convenience method to run multiple commands by onecmd_plus_hooks.
This method adds the given cmds to the command queue and processes the
queue until completion or an error causes it to abort. Scripts that are
loaded will have their commands added to the queue. Scripts may even
load other scripts recursively. This means, however, that you should not
use this method if there is a running cmdloop or some other event-loop.
This method is only intended to be used in "one-off" scenarios.
NOTE: You may need this method even if you only have one command. If
that command is a load, then you will need this command to fully process
all the subsequent commands that are loaded from the script file. This
is an improvement over onecmd_plus_hooks, which expects to be used
inside of a command loop which does the processing of loaded commands.
Example: cmd_obj.runcmds_plus_hooks(['load myscript.txt'])
:param cmds: command strings suitable for onecmd_plus_hooks.
:return: True implies the entire application should exit.
"""
stop = False
self.cmdqueue = list(cmds) + self.cmdqueue
try:
while self.cmdqueue and not stop:
line = self.cmdqueue.pop(0)
if self.echo and line != 'eos':
self.poutput('{}{}'.format(self.prompt, line))
stop = self.onecmd_plus_hooks(line)
finally:
# Clear out the command queue and script directory stack, just in
# case we hit an error and they were not completed.
self.cmdqueue = []
self._script_dir = []
# NOTE: placing this return here inside the finally block will
# swallow exceptions. This is consistent with what is done in
# onecmd_plus_hooks and _cmdloop, although it may not be
# necessary/desired here.
return stop
示例5: test_file_with_missing_final_nl
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def test_file_with_missing_final_nl(self):
input = io.StringIO("print test\nprint test2")
output = io.StringIO()
cmd = self.simplecmd(stdin=input, stdout=output)
cmd.use_rawinput = False
cmd.cmdloop()
self.assertMultiLineEqual(output.getvalue(),
("(Cmd) test\n"
"(Cmd) test2\n"
"(Cmd) "))
示例6: main
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def main():
#训练的NLU模型,所以,不需要再提供nlu_config.yml了,在这里
logger.info("Rasa process starting")
nlu_model_path = '../model/default/latest'
model_directory = "../model/dialogue"
agent = Agent.load(model_directory, nlu_model_path)
logger.info("Finished loading agent, starting input channel & server.")
cmd = RasaCmd(agent)
cmd.cmdloop()
示例7: preloop
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def preloop(self):
"""Hook method executed once when the :meth:`~.cmd2.Cmd.cmdloop()`
method is called.
See :meth:`~cmd2.Cmd.register_preloop_hook` for a more robust way
to run hooks before the command loop begins. See
:ref:`features/hooks:Application Lifecycle Hooks` for more information.
"""
pass
示例8: postloop
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def postloop(self):
"""Hook method executed once when the :meth:`~.cmd2.Cmd.cmdloop()`
method is about to return.
See :meth:`~cmd2.Cmd.register_postloop_hook` for a more robust way
to run hooks after the command loop completes. See
:ref:`features/hooks:Application Lifecycle Hooks` for more information.
"""
pass
示例9: _cmdloop
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def _cmdloop(self) -> None:
"""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.
This serves the same role as cmd.cmdloop().
"""
saved_readline_settings = None
try:
# Get sigint protection while we set up readline for cmd2
with self.sigint_protection:
saved_readline_settings = self._set_up_cmd2_readline()
# Run startup commands
stop = self.runcmds_plus_hooks(self._startup_commands)
self._startup_commands.clear()
while not stop:
# Get commands from user
try:
line = self._read_command_line(self.prompt)
except KeyboardInterrupt as ex:
if self.quit_on_sigint:
raise ex
else:
self.poutput('^C')
line = ''
# Run the command along with all associated pre and post hooks
stop = self.onecmd_plus_hooks(line)
finally:
# Get sigint protection while we restore readline settings
with self.sigint_protection:
if saved_readline_settings is not None:
self._restore_readline(saved_readline_settings)
# ----- Alias subcommand functions -----
示例10: pseudo_raw_input
# 需要导入模块: import cmd [as 别名]
# 或者: from cmd import cmdloop [as 别名]
def pseudo_raw_input(self, prompt: str) -> str:
"""Began life as a copy of cmd's cmdloop; like raw_input but
- accounts for changed stdin, stdout
- if input is a pipe (instead of a tty), look at self.echo
to decide whether to print the prompt and the input
"""
if self.use_rawinput:
try:
if sys.stdin.isatty():
# Wrap in try since terminal_lock may not be locked when this function is called from unit tests
try:
# A prompt is about to be drawn. Allow asynchronous changes to the terminal.
self.terminal_lock.release()
except RuntimeError:
pass
# Deal with the vagaries of readline and ANSI escape codes
safe_prompt = rl_make_safe_prompt(prompt)
line = input(safe_prompt)
else:
line = input()
if self.echo:
sys.stdout.write('{}{}\n'.format(self.prompt, line))
except EOFError:
line = 'eof'
finally:
if sys.stdin.isatty():
# The prompt is gone. Do not allow asynchronous changes to the terminal.
self.terminal_lock.acquire()
else:
if self.stdin.isatty():
# on a tty, print the prompt first, then read the line
self.poutput(self.prompt, end='')
self.stdout.flush()
line = self.stdin.readline()
if len(line) == 0:
line = 'eof'
else:
# we are reading from a pipe, read the line to see if there is
# anything there, if so, then decide whether to print the
# prompt or not
line = self.stdin.readline()
if len(line):
# we read something, output the prompt and the something
if self.echo:
self.poutput('{}{}'.format(self.prompt, line))
else:
line = 'eof'
return line.strip()