Content¶
Command-line interface¶
Module aiotraversal.cmd
makes if easy to write command-line interfaces for you application.
Parser¶
After config.include('aiotraversal.cmd')
, you can use these objects:
config['cmd']['parser']
: ArgumentParser instance;config['cmd']['subparsers']
: subparsers for create commands, the main tool for extend console application;
After configure process finished:
app['cmd']['args']
:Namespace
instance fromconfig['cmd']['parser'].parse_args()
;app['cmd']['run_func']
: function for Function run;
Add commands¶
Something like this:
import asyncio
from aiotraversal import Application
from aiotraversal.cmd import run
def cmd_func(app, loop):
print('cmd_func is called!')
def main():
loop = asyncio.get_event_loop()
app = Application()
with app.configure(loop=loop) as config:
config.include('aiotraversal.cmd') # include
subparsers = config['cmd']['subparsers']
parser_test = subparsers.add_parser('test_command', help="Test") # create subparser
parser_test.set_defaults(func=cmd_func) # add function for start
# ... extend subpaser with `parser_test.add_argument`
run(app, loop) # in this place cmd_func is called
Now, if run your application with argument test_command
(e.g. my_cmd test_command
),
“cmd_func is called!” printed.
Default key func
of subparser, is magic for bind functions to commands.
It is called from the Function run with two arguments: app
and loop
.
ArgumentParser¶
ArgumentParser
is modified for grouping subcommand arguments.
StackOverflow with this solution.
Function run
¶
After configure process, you must run aiotraversal.cmd.run
.
It run app['cmd']['run_func']
, finish application and close loop.
Serve helper¶
For add command serving, include aiotraversal.serve
:
import asyncio
from aiotraversal import Application
from aiotraversal.cmd import run
def main():
loop = asyncio.get_event_loop()
app = Application() # create main application instance
with app.configure(loop=loop) as config:
config.include('aiotraversal.cmd')
config.include('aiotraversal.serve') # include module for serving
# some other includes
run(app, loop=loop) # start application
$ python app.py serve --help
usage: app.py serve [-h] [--listen HOST:PORT] [--static DIR]
optional arguments:
-h, --help show this help message and exit
--listen HOST:PORT host and port for listen (default 'localhost:8080')
--static DIR Serve static files
Objects¶
config['cmd']['parser_serve']
: subparser forserve
command;
Arguments¶
serve
command have some arguments.
--listen
¶
Adderss for listen. Default localhost:8080
.
Host or port may be not specified. E.g.:
--listen 0.0.0.0
equal--listen 0.0.0.0:8080
--listen :8082
equal--listen localhost:8082
--static
¶
Serve static directory.
Specified directory is can be found in GET /static/
.
Warning
Do not use in production! Access to files is synchronous!
Settings¶
If aiotraversal.settings is included, you can use settings for setup default values.
For example:
[serve]
listen = "10.0.0.15:8080"
static = "/srv/static"
Settings¶
Just include Settings.
Settings uses zini.
All keys in section named “app”, set to config['settings']
.
Other sections set to config['settings'][section_name]
.
For example:
[app]
author = "ZZZ"
[serve]
listen = ":6543"
assert app['settings']['author'] == 'ZZZ'
assert app['settings']['serve']['listen'] == ':6543'
Objects¶
config['settings_ini']
: instance of Zini <https://github.com/zzzsochi/zini>;config['settings']['file']
: path to file with settings;
Arguments¶
aiotraversal.cmd module is not required.
--settings
¶
Path to ini-file with setings. This is setup config['settings']['file']
.