使用python创建cli应用程序的框架
cli-app的Python项目详细描述
python cli应用程序
Framework for creating CLI apps using Python
这个库的目的是通过提供表示应用程序及其命令的抽象类来加速构建cli应用程序。它使用argparse
来定义和解析命令行参数。
概念
应用程序
“应用程序”是应用程序的主要入口点。它将一个或多个命令组合在一起
命令
“命令”是应用程序可以执行的单个操作。它由命令行上的位置参数标识。以git
为例。git
将是app提供不同的command,例如add
、commit
、merge
、checkout
等。
示例
git.py
#!/usr/bin/env python3fromcli_appimportAppfromcommands.checkoutimportCheckoutfromcommands.mergeimportMergeclassGit(App):"""Git - fast, scalable, distributed revision control system"""defregister_commands(self):self.add_command('checkout',Checkout)# make the Checkout command available through `git.py checkout …`self.add_command('merge',Merge)# make the Merge command available through `git.py merge …`if__name__=='__main__':app=Git()app.run()
命令/checkout.py
fromcli_appimportCommandclassCheckout(Command):"""Switch branches or restore working tree files"""@staticmethoddefregister_arguments(parser):parser.add_argument('ref',type=str,help='The ref (branch name, tag, commit sha) to checkout')defrun(self):# Do whatever needs to be done to checkout given refprint('Checking out %s'%self.app.args.ref)
命令/merge.py
fromcli_appimportCommandclassMerge(Command):"""Join two or more development histories together"""@staticmethoddefregister_arguments(parser):parser.add_argument('branch',type=str,help='The branch to merge into the currently checked-out branch')defrun(self):# Do whatever needs to be done to merge given branchprint('Merging %s into current branch'%self.app.args.branch)