Application¶
Application with bindings for commands
Quick demo:
>>> import sample
>>> from pyapp.app import CliApplication, add_argument
>>> app = CliApplication(sample)
>>> @add_argument('--verbose', target='verbose', action='store_true')
>>> @app.register_handler()
>>> def hello(opts):
... if opts.verbose:
... print("Being verbose!")
... print("Hello")
>>> if __name__ == '__main__':
... app.dispatch()
This example provides an application with a command hello that takes an
optional verbose flag. The framework also provides help, configures and loads
settings (using pyapp.conf
), an interface to the checks framework
and configures the Python logging framework.
There are however a few more things that are required to get this going. The
CliApplication
class expects a certain structure of your
application to allow for it’s (customisable) defaults to be applied.
Your application should have the following structure:
my_app/__init__.py # Include a __version__ variable
__main__.py # This is where the quick demo is located
default_settings.py # The default settings file
CliApplication¶
-
class
pyapp.app.
CliApplication
(root_module, name=None, description=None, version=None, application_settings=None, application_checks=None, env_settings_key=None, env_loglevel_key=None, default_handler=None)[source]¶ Parameters: - root_module – The root module for this application (used for discovery of other modules)
- name – Name of your application; defaults to sys.argv[0]
- description – A description of your application for –help.
- version – Specify a specific version; defaults to getattr(root_module, ‘__version__’)
- application_settings – The default settings for this application; defaults to root_module.default_settings
- application_checks – Location of application checks file; defaults to root_module.checks if it exists.
-
register_handler
(handler=None, cli_name=None)¶ Decorator for registering handlers.
The description for help is taken from the handlers doc string.
Parameters: - handler – Handler function
- cli_name – Optional name to use for CLI; defaults to the function name.
Return type: HandlerProxy