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