Python framework for commandline applications
Project description
Python framework for commandline applications
Quickstart
Feeling impatient? I like your style.
$ pip install battalion
In mycli.py script:
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
$ 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] <command> [<args>...]
> mycli [options]
>
> Options:
> -h, --help Show this screen.
> --version Show version.
> -d, --debug Show debug messages
> --config=<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 <msg>
>
> Options:
> --msg=<MSG> [default: None]
>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
battalion-0.3.0.tar.gz
(9.0 kB
view hashes)
Built Distribution
Close
Hashes for battalion-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24c6c120d61fe43b0f58d119ba63f3e77d565ebd54114cc496f30f98e997200f |
|
MD5 | f198f94c02564f9686c5e42d5e286479 |
|
BLAKE2b-256 | 3edf90af1e52f0e25d31882929bbaab8635da56f33200c305d7a865287fad88c |