项目作者: jsphpl

项目描述 :
Framework for creating CLI apps using Python
高级语言: Python
项目地址: git://github.com/jsphpl/python-cli-app.git
创建时间: 2018-11-03T14:29:17Z
项目社区:https://github.com/jsphpl/python-cli-app

开源协议:MIT License

下载


Python CLI App

Framework for creating CLI apps using Python

The purpose of this library is to speed up bulding CLI applications by providing abstract classes representing an app and its commands. It uses argparse to define and parse command line arguments.

Concepts

App

An “App” is the main entry point of an application. It groups together one or more commands

Command

A “Command” is a single operation that the application can perform. It is identified by a positional argument on the command line. Let’s take git as an example. git would be the app that provides different commands, such as add, commit, merge, checkout, etc.

Example

git.py

  1. #!/usr/bin/env python3
  2. from cli_app import App
  3. from commands.checkout import Checkout
  4. from commands.merge import Merge
  5. class Git(App):
  6. """Git - fast, scalable, distributed revision control system"""
  7. def register_commands(self):
  8. self.add_command('checkout', Checkout) # make the Checkout command available through `git.py checkout …`
  9. self.add_command('merge', Merge) # make the Merge command available through `git.py merge …`
  10. if __name__ == '__main__':
  11. app = Git()
  12. app.run()

commands/checkout.py

  1. from cli_app import Command
  2. class Checkout(Command):
  3. """Switch branches or restore working tree files"""
  4. @staticmethod
  5. def register_arguments(parser):
  6. parser.add_argument('ref', type=str, help='The ref (branch name, tag, commit sha) to checkout')
  7. def run(self):
  8. # Do whatever needs to be done to checkout given ref
  9. print('Checking out %s' % self.app.args.ref)

commands/merge.py

  1. from cli_app import Command
  2. class Merge(Command):
  3. """Join two or more development histories together"""
  4. @staticmethod
  5. def register_arguments(parser):
  6. parser.add_argument('branch', type=str, help='The branch to merge into the currently checked-out branch')
  7. def run(self):
  8. # Do whatever needs to be done to merge given branch
  9. print('Merging %s into current branch' % self.app.args.branch)