argparse前端库帮助生成软件使用消息

helpgen的Python项目详细描述


helpgen帮助您轻松生成命令行工具的帮助/使用消息。

示例

下面是一个如何使用库的简短示例:

#!/usr/bin/env python

import sys

from helpgen.decorators import Action
from helpgen.parser import ActionParser


@Action('test')
def action_test():
    """run a test"""

    print("I AM A TEST \o/")


@Action('hello', args=['word'])
def action_hello(word):
    """display a word"""

    print("The word is : %s" % (word,))


def main():
    parser = ActionParser(description="helpgen test script")
    parser.compile()

    try:
        parser.process()
    except:
        return 1
    else:
        return 0

if __name__ == "__main__":
    sys.exit(main())

这段代码将产生以下帮助消息:

  • 一般帮助消息:
$ ./test.py -h
usage: test.py [-h] {test,hello} ...

helpgen test script

positional arguments:
  {test,hello}
    test        run a test
    hello       display a word

optional arguments:
  -h, --help    show this help message and exit
  • 测试子命令的帮助消息:
$ ./test.py test -h
usage: test.py test [-h]

optional arguments:
  -h, --help  show this help message and exit
  • hello子命令的帮助消息:
$ ./test.py hello -h
usage: test.py hello [-h] word

positional arguments:
  word

optional arguments:
  -h, --help  show this help message and exit
  • 而且,它工作正常:
$ ./test.py test
I AM A TEST \o/
$ ./test.py hello "\_o<~ KOIN"
The word is : \_o<~ KOIN

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java日期格式的月和日的固定长度?   无法将java ArrayList<String>转换为字符串。   java当我使用log4j 1.2.15时,编译时出现错误;当更改为1.2.16时,一切正常   java如何使用ArgumentMatchers将任何队列告知Mockito?   如何解决“java”。Java电子邮件API中的lang.ExceptionInInitializerError   java是实例化对象名并设置其值的通用方法   java在多线程环境中,如果一个线程崩溃,如何关闭JVM?   java WebDriverManager在selenium中获取不正确版本的驱动程序   如果JAVA中后减量的优先级高于前增量,那么为什么下面的代码输出为22.0而不是20.0。?   在hibernate中找不到java属性   当我用java运行调度程序时,什么都没有发生   jackson如何在java中从数据库中检索json对象   java安卓:如何在自定义适配器中设置侦听器并获取imagebutton单击的位置   java MySQLIntegrityConstraintViolationException:列“问题id”不能为空错误   JavaSpring自定义注销过滤器在注销前执行一些操作?   java如何返回常量集合   java使用subselect进行一对多分页   java二进制搜索基础。陷入无限循环