【问题标题】:Slash and python cmd斜线和 python cmd
【发布时间】:2012-06-13 23:34:40
【问题描述】:

我正在尝试使用 cmd 模块实现 python cmd。我想自动完成文件,所以我实现了一些方法,但是,我看到“complete_put(self, text, line, begidx, endidx):" 中的文本参数删除了所有的 '/' 字符。任何人都知道为什么,我该如何避免这种行为?谢谢:)

【问题讨论】:

  • 欢迎来到 SO!你能添加更多细节吗?向我们展示您的源代码!你试过什么?你的用例是什么?什么有效?什么没有?谢谢!
  • 这是我尝试过的:pastebin.com/Zwie5uRW 我在 complete_put 方法中添加了一个跟踪,以查看文本是什么。如果行是“put /”,则文本是“”。我已经在 python 2 上试过了,顺便说一句。
  • 听起来你很清楚你期望的行为是什么,如果你能把它翻译成short, self-contained, correct example,它会让你的问题更容易回答。
  • 不知道 cmets 中的邮政编码如何,所以我创建了一个答案。

标签: python cmd


【解决方案1】:

我解决了。只需要修改 set_completer_delims 属性。

【讨论】:

  • 现在不存在了(或者我只是找不到它?)解决方案是在identchars属性中添加斜线。
  • @S0lll0s set_completer_delims 位于 readline 模块中。我刚刚做了:delims = readline.get_completer_delims()然后readline.set_completer_delims(delims.replace('/', ''))。这将为这种情况适当地配置 readline 模块。它工作得很好(Python 3.4)。
【解决方案2】:

这是我使用的代码,它基于互联网上的几个示例。

import os
import cmd
import readline
class Shell(cmd.Cmd, object):
    def __init__(self):
        cmd.Cmd.__init__(self)

    def __complete_path(self, path=None):
        return ['/bin', '/boot', '/etc']

    def do_put(self,args):
        print args

    def complete_put(self, text, line, begidx, endidx):
        print text
        if not text:
            return self.__complete_path()
        return self.__complete_path(text)

【讨论】:

  • 试试 Shell().cmdloop() 并使用 'put /' 。它应该是'put /bin /boot /etc'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-20
  • 2022-07-13
  • 2015-11-12
  • 1970-01-01
相关资源
最近更新 更多