2

我喜欢 cmdln 框架,用于编写类似svn command argument但仅适用于 Python 2 的程序。有什么好的 Python 3 替代方案?

4

4 回答 4

5

Python 标准库中的argparse模块也支持命令,它适用于 Python 2.x 和 3.x。

于 2012-07-14T13:14:11.670 回答
2

如果你喜欢cmdln你仍然可以使用它,我认为:使用该2to3工具cmdln.py和示例p4.pysvn.py似乎可以生成工作代码。2-ness ofcmdln.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=()
于 2012-07-14T13:24:26.853 回答
2

您可能对水泥感兴趣

于 2012-07-14T13:34:18.830 回答
1

Cliff看起来功能齐全,得到积极维护,可在 2.x 和 3.x 上运行。

于 2012-07-14T13:14:17.350 回答