python编写命令行框架_battalion-命令行应用程序的Python框架-Kyle Rockman Quickstart

作者:Kyle Rockman

### 作者邮箱:['kyle.rockman@mac.com']

### 首页:UNKNOWN

### 文档:None

### 下载链接 UNKNOWN

battalion

.. image:: https://pypip.in/py_versions/battalion/badge.svg?style=flat :target: https://pypi.python.org/pypi/battalion/

.. image:: https://pypip.in/license/battalion/badge.svg?style=flat :target: https://pypi.python.org/pypi/battalion/

.. image:: https://pypip.in/download/battalion/badge.svg?style=flat :target: https://pypi.python.org/pypi/battalion/

.. image:: https://pypip.in/version/battalion/badge.svg?style=flat :target: https://pypi.python.org/pypi/battalion/

.. image:: https://pypip.in/wheel/battalion/badge.svg?style=flat :target: https://pypi.python.org/pypi/battalion/

.. image:: https://requires.io/github/rocktavious/battalion/requirements.png?branch=master :target: https://requires.io/github/rocktavious/battalion/requirements/?branch=master :alt: Requirements Status

Python framework for commandline applications

Quickstart

Feeling impatient? I like your style.

.. code-block:: bash

$ pip install battalion

In mycli.py script:

.. code-block:: python

from battalion.api import *

class DB(object):

def __init__(self, state):

self.msg = state.msg

def myfunc(self):

return self.msg

@fixture

def db(state):

return DB(state)

class mycli(CLI):

"""

Toplevel program - mycli

"""

class State:

version = '0.0.1'

msg = ""

@command

def normal_function(cli, db, data=None): # NOTE: the "db" arg will be populated from the "db" fixture function

"""Takes in data and outputs new data"""

if data is None:

return db.myfunc()

else:

return data

@command

def greeting(cli, name="Hello"):

"""

Prints "Hello {name}!"

"""

print "Hello {0}!".format(name)

class myhandler(Handler):

"""

Organizational container of commands which can also add state variables

"""

class State:

version = '0.0.2' # Handlers can have their own versioning

cli = 'mycli'

msg = "World"

@command

def validate(cli, data):

return data

@command

def hello(cli, msg=None):

"""

Prints "Hello World!"

"""

name = cli.normal_function(data=msg)

name = cli.myhandler.validate(data=name)

cli.greeting(name=name)

if __name__ == "__main__":

mycli.main()

Then on the commandline

.. code-block:: bash

$ python mycli.py myhandler hello

> Hello World!

$ #or if installed as a console script via setuptools

$ mycli myhandler hello

> Hello World!

$ #also

$ mycli myhandler hello "Josh"

> Hello Josh!

$ mycli --help

> Toplevel program - mycli

>

> Usage:

> mycli [options] [...]

> mycli [options]

>

> Options:

> -h, --help Show this screen.

> --version Show version.

> -d, --debug Show debug messages

> --config= The config filepath [default: ~/.mycli.cfg]

>

> Commands:

> greeting Prints "Hello {name}!"

> myhandler Organizational container of commands which can also add state variables

>

$ mycli myhandler hello --help

> Prints "Hello World!"

>

> Usage:

> hello [options]

> hello

>

> Options:

> --msg= [default: None]

>

Copy from pypi.org

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值