cmd2命令行参数

时间:2013-11-18 19:28:59

标签: python command-line command-line-arguments optparse

我有以下情况:

import sys
from cmd2 import Cmd, make_option, options

class DemoApp(Cmd):
    """Simple command processor example."""

    @options([make_option('-n', '--name', action="store", help="your name"),
         ])
    def do_hello(self, command, opts):
        if opts.name:
            sys.stdout.write('Hello %s\n' % opts.name)
        else:
            sys.stdout.write('Hello Nobody\n')

if __name__ == '__main__':
    DemoApp().cmdloop() 

我想传递一个带空格的字符串作为选项-n的参数。 (在cmd2 promt中)

示例:

- > hello -n'我很棒的名字'

当我这样做时,它被打印出来:

你好'我的

同样也是双引号

所以它不接受那里的空格。有人知道怎么做吗?

2 个答案:

答案 0 :(得分:0)

我发现它发生了。我提交了一个错误: https://bitbucket.org/catherinedevlin/cmd2/issues?status=new&status=open

加上修复bug的补丁。 如果有人遇到同样的问题,如果维护者没有更新代码,你可以使用补丁。

答案 1 :(得分:0)

cmd2的0.7版修复了此问题。 cmd2现在正确地将引用的参数视为单个参数,而不管引号中是否存在空格。您可以使用以下命令从PyPI安装最新版本的cmd2:

pip install -U cmd2
相关问题