我喜欢Python的Twisted和Cmd.我想一起使用它们.
我有一些工作,但到目前为止,我还没有想出如何使标签完成工作,因为我没有看到如何在Twisted的LineReceiver中立即接收Tab键盘事件(不按Enter键).
到目前为止,这是我的代码:
#!/usr/bin/env python
from cmd import Cmd
from twisted.internet import reactor
from twisted.internet.stdio import StandardIO
from twisted.protocols.basic import LineReceiver
class CommandProcessor(Cmd):
def do_EOF(self, line):
return True
class LineProcessor(LineReceiver):
from os import linesep as delimiter # makes newline work
def __init__(self):
self.processor = CommandProcessor()
self.setRawMode()
def connectionMade(self):
self.transport.write('>>> ')
def rawDataReceived(self, data):
self.processor.onecmd(data)
self.transport.write('>>> ')
StandardIO(LineProcessor())
reactor.run()
Run Code Online (Sandbox Code Playgroud)
除了标签完成,这有点起作用.我可以输入"help"之类的命令,Cmd模块将打印结果.但是我已经失去了Cmd模块的漂亮的tab-complete功能,因为Twisted一次缓冲一行.我尝试设置LineProcessor.delimiter为空字符串,无济于事.也许我需要找一些其他的Twisted而不是LineReceiver?或者也许有一种更简单的方法可以避免我必须逐个处理每个字符?
我不能单独使用Cmd,因为我想把它变成一个网络应用程序,其中一些命令将导致发送数据,并且从网络接收数据将异步发生(并显示给用户).
因此,无论我们从上面的代码开始还是完全不同的东西,我都想在Python中构建一个友好的,友好的终端应用程序,它响应网络事件和标签完成.我希望我可以使用已经存在的东西而不必自己实施太多.
我正在使用 Python 3 和 cmd 模块构建一个交互式 shell。我已经使用 py.test 编写了简单的单元测试来测试各个函数,例如 do_* 函数。我想创建更全面的测试,通过模拟用户的输入来实际与 shell 本身交互。例如,我如何测试以下模拟会话:
bash$ console-app.py
md:> show options
Available Options:
------------------
HOST The IP address or hostname of the machine to interact with
PORT The TCP port number of the server on the HOST
md:> set HOST localhost
HOST => 'localhost'
md:> set PORT 2222
PORT => '2222'
md:>
Run Code Online (Sandbox Code Playgroud) 有没有一种方法可以使用argparse挂钩的模块作为继承自接口的每个提示的解释器cmd?
我想让我的cmd接口以一种典型line的方式来解释典型的参数,就像解释运行时在bash shell上传递的选项和参数一样,使用可选参数和-位置参数。
我使用cmd模块编写了一个Python 3.5应用程序.我想要实现的最后一件事是正确处理CTRL-C(sigint)信号.我希望它的行为或多或少与Bash的行为方式相同:
基本上:
/test $ bla bla bla|
# user types CTRL-C
/test $ bla bla bla^C
/test $
Run Code Online (Sandbox Code Playgroud)
以下是作为可运行示例的简化代码:
import cmd
import signal
class TestShell(cmd.Cmd):
def __init__(self):
super().__init__()
self.prompt = '$ '
signal.signal(signal.SIGINT, handler=self._ctrl_c_handler)
self._interrupted = False
def _ctrl_c_handler(self, signal, frame):
print('^C')
self._interrupted = True
def precmd(self, line):
if self._interrupted:
self._interrupted = False
return ''
if line == 'EOF':
return 'exit'
return line
def emptyline(self):
pass
def do_exit(self, line):
return True
TestShell().cmdloop()
Run Code Online (Sandbox Code Playgroud)
这几乎可行.当我按下CTRL-C时,在光标处打印^ C,但我仍然需要按回车键.然后,该precmd …
字符,如-,+等不解析由Python的readline的字母数字ASCII字符基于CMD模块以同样的方式.这似乎只是Linux特定的问题,因为它似乎在Mac OS上按预期工作.
示例代码
import cmd
class Test(cmd.Cmd):
def do_abc(self, line):
print line
def complete_abc(self, text, line, begidx, endidx):
return [i for i in ['-xxx', '-yyy', '-zzz'] if i.startswith(text)]
try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")
Test().cmdloop()
Run Code Online (Sandbox Code Playgroud)
Mac OS上的预期行为
(Cmd) abc <TAB>
abc
(Cmd) abc -<TAB>
-xxx -yyy -zzz
(Cmd) abc -x<TAB>
(Cmd) abc -xxx
Run Code Online (Sandbox Code Playgroud)
Linux上的行为不正确
(Cmd) abc <TAB>
abc
(Cmd) …Run Code Online (Sandbox Code Playgroud) 是否有任何方法可以从Python配置CMD模块,即使在交互式shell关闭后仍保留持久历史记录?
当我按下向上和向下键时,我想访问先前在我运行python脚本以及我刚刚在此会话期间输入的脚本时先前输入shell的命令.
如果其任何帮助cmd使用set_completer从readline模块导入