开发者社区> 问答> 正文

对于Python 2和3,有什么好的面向命令的命令行框架

喜欢使用cmdln框架来编写类似的程序,svn command argument但该程序仅在Python 2中有效。有什么好的Python 3替代方案?

展开
收起
祖安文状元 2020-02-23 16:23:07 815 0
1 条回答
写回答
取消 提交回答
  • 如果您cmdln仍然可以使用它,我想:使用上的2to3工具cmdln.py和示例p4.py,svn.py似乎可以生成有效的代码。的二元性cmdln.py似乎仅限于较小的句法问题和命名。截取差异中的随机位:

    -LOOP_ALWAYS, LOOP_NEVER, LOOP_IF_EMPTY = range(3)
    +LOOP_ALWAYS, LOOP_NEVER, LOOP_IF_EMPTY = list(range(3))
    
    -            except CmdlnUserError, ex:
    +            except CmdlnUserError as ex:
    
    -                            line = raw_input(self._prompt_str)
    +                            line = input(self._prompt_str)
    
    -        for marker, preprocessor in preprocessors.items():
    +        for marker, preprocessor in list(preprocessors.items()):
    
    -        func = handler.im_func
    -        if func.func_defaults:
    -            func_defaults = list(func.func_defaults)
    +        func = handler.__func__
    +        if func.__defaults__:
    +            func_defaults = list(func.__defaults__)
    
    -        co_argcount = handler.im_func.func_code.co_argcount
    +        co_argcount = handler.__func__.__code__.co_argcount
    
    -        if DEBUG: print "dedent: indent=%d: %r" % (indent, line)
    +        if DEBUG: print("dedent: indent=%d: %r" % (indent, line))
    
    

    等等。转换后,您应该能够获得预期的输出:

    localhost-2:examples $ python3 p4.py add 
    p4 add: opts={'filetype': None, 'changelist': None} paths=()
    
    2020-02-23 16:23:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载