I have the following three modules (based on Miguel Grinberg's excellent book):
__init__.py
from flask import Flask
from config import config
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
manage.py
import os
from app import create_app
from app.import_file import Import_File_CLI
from flask.ext.script import Manager, Shell, Command
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
manager.add_command('import_file', Import_File_CLI())
manager.add_option('-v', dest='verbosity', help='how verbose output should be - the more vs, the more verbose', action='count', required = False)
if __name__ == '__main__':
manager.run()
import_file.py
from flask.ext.script import Command, Option
from flask import current_app
import logging
class Import_File_CLI(Command):
"Imports files from the command line"
option_list = (
Option(help="file to import", dest="filename"),
)
def run(self, filename, verbosity=0):
if verbosity == 1:
log_level = logging.WARNING
elif verbosity == 2:
log_level = logging.INFO
elif verbosity > 2:
log_level = logging.DEBUG
else:
log_level = None
I'm going to be doing a lot of file manipulation, and I want to be able to do some testing/debugging from the command line, so there are going to be a number of _CLI modules for different things. I want to be able to control the verbosity of the output I get, so I have the verbosity option defined. What I can't figure out is how to actually pass that option into the Import_File_CLI class. I know I can add an option to the option_list, but then I end up having to add that to every _CLI class I create, when I really want it to be a global option. So how do I access an option I set in the main manager in a subclass?
The documentation sort of has an example of this, but it's in the same module, not a subclassed one. I could pass the option into the factory function, and then set an app-wide default, but that seems less than optimal as well, since I most of the time don't need to set it. Do I need to create a _CLI submanager, possibly?