Python 是否可以将docopt--help选项重定向到less?

Python 是否可以将docopt--help选项重定向到less?,python,arguments,docopt,Python,Arguments,Docopt,通常,man提供的长文档不会直接打印在屏幕上,而是重定向到less(例如man ls) 使用python中的docopt模块是否可以做到这一点?没有官方的方法,但您可以这样做: """ Usage: docopt_hack.py """ import docopt, sys, pydoc def extras(help, version, options, doc): if help and any((o.name in ('-h', '--help')) and o.val

通常,man提供的长文档不会直接打印在屏幕上,而是重定向到less(例如man ls)

使用python中的docopt模块是否可以做到这一点?

没有官方的方法,但您可以这样做:

"""
Usage:
    docopt_hack.py
"""

import docopt, sys, pydoc

def extras(help, version, options, doc):
    if help and any((o.name in ('-h', '--help')) and o.value for o in options):
        pydoc.pager(doc.strip("\n"))
        sys.exit()
    if version and any(o.name == '--version' and o.value for o in options):
        print(version)
        sys.exit()

docopt.extras = extras

# Do your normal call here, but make sure it is after the previous lines
docopt.docopt(__doc__, version="0.1")

我们要做的是重写
extras
函数,该函数在普通docopt()中处理帮助的打印。然后,我们使用pydoc将输入推送到寻呼机()。请注意,使用pydoc是一种不安全的快捷方式,因为该方法没有文档记录,可以删除。这同样适用于
extra
。YMMV.

没有足够的代表投票,但你的答案和解释正是我想要的。非常感谢@利亚德,别担心,你很快就会有机会的。玩得高兴