add ability to add application specific options
This commit is contained in:
parent
6f1cec035d
commit
fad7d2d56c
3 changed files with 33 additions and 18 deletions
|
|
@ -31,9 +31,8 @@ class Videoob(ConsoleApplication):
|
||||||
COPYRIGHT = 'Copyright(C) 2010 Christophe Benz, Romain Bignon'
|
COPYRIGHT = 'Copyright(C) 2010 Christophe Benz, Romain Bignon'
|
||||||
CONFIG = {}
|
CONFIG = {}
|
||||||
|
|
||||||
def __init__(self):
|
def add_application_options(self, group):
|
||||||
ConsoleApplication.__init__(self)
|
group.add_option('--nsfw', action='store_true', help='enable non-suitable for work videos')
|
||||||
self._parser.add_option('--nsfw', action='store_true', help='enable non-suitable for work videos')
|
|
||||||
|
|
||||||
def main(self, argv):
|
def main(self, argv):
|
||||||
self.load_modules(ICapVideoProvider)
|
self.load_modules(ICapVideoProvider)
|
||||||
|
|
|
||||||
|
|
@ -44,16 +44,13 @@ class BaseApplication(object):
|
||||||
# Copyright
|
# Copyright
|
||||||
COPYRIGHT = None
|
COPYRIGHT = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, option_parser=None):
|
||||||
self.weboob = self.create_weboob()
|
self.weboob = self.create_weboob()
|
||||||
self.config = None
|
self.config = None
|
||||||
version = None
|
if option_parser is None:
|
||||||
if self.VERSION:
|
self._parser = OptionParser(self.SYNOPSIS, version=self._get_optparse_version())
|
||||||
if self.COPYRIGHT:
|
else:
|
||||||
version = '%s v%s (%s)' % (self.APPNAME, self.VERSION, self.COPYRIGHT)
|
self._parser = option_parser
|
||||||
else:
|
|
||||||
version = '%s v%s' % (self.APPNAME, self.VERSION)
|
|
||||||
self._parser = OptionParser(self.SYNOPSIS, version=version)
|
|
||||||
self._parser.add_option('-b', '--backends', help='what backend(s) to enable (comma separated)')
|
self._parser.add_option('-b', '--backends', help='what backend(s) to enable (comma separated)')
|
||||||
logging_options = OptionGroup(self._parser, 'Logging Options')
|
logging_options = OptionGroup(self._parser, 'Logging Options')
|
||||||
logging_options.add_option('-d', '--debug', action='store_true', help='display debug messages')
|
logging_options.add_option('-d', '--debug', action='store_true', help='display debug messages')
|
||||||
|
|
@ -131,6 +128,15 @@ class BaseApplication(object):
|
||||||
"""
|
"""
|
||||||
return set()
|
return set()
|
||||||
|
|
||||||
|
def _get_optparse_version(self):
|
||||||
|
version = None
|
||||||
|
if self.VERSION:
|
||||||
|
if self.COPYRIGHT:
|
||||||
|
version = '%s v%s (%s)' % (self.APPNAME, self.VERSION, self.COPYRIGHT)
|
||||||
|
else:
|
||||||
|
version = '%s v%s' % (self.APPNAME, self.VERSION)
|
||||||
|
return version
|
||||||
|
|
||||||
def _handle_app_options(self):
|
def _handle_app_options(self):
|
||||||
"""
|
"""
|
||||||
Overload this method in subclasses if you want to handle options defined in subclass constructor.
|
Overload this method in subclasses if you want to handle options defined in subclass constructor.
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ from functools import partial
|
||||||
import getpass
|
import getpass
|
||||||
from inspect import getargspec
|
from inspect import getargspec
|
||||||
import logging
|
import logging
|
||||||
|
from optparse import OptionGroup, OptionParser
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
@ -26,8 +27,8 @@ import weboob
|
||||||
from weboob.modules import BackendsConfig
|
from weboob.modules import BackendsConfig
|
||||||
|
|
||||||
from .base import BaseApplication
|
from .base import BaseApplication
|
||||||
from .formatters import formatters_classes
|
from .formatters import formatters
|
||||||
from .results import Results, WhereCondition, WhereConditionException
|
from .results import Results, ResultsCondition, ResultsConditionException
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['ConsoleApplication']
|
__all__ = ['ConsoleApplication']
|
||||||
|
|
@ -37,8 +38,13 @@ class ConsoleApplication(BaseApplication):
|
||||||
SYNOPSIS = 'Usage: %prog [options (-h for help)] command [parameters...]'
|
SYNOPSIS = 'Usage: %prog [options (-h for help)] command [parameters...]'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
option_parser = OptionParser(self.SYNOPSIS, version=self._get_optparse_version())
|
||||||
|
app_options = OptionGroup(option_parser, '%s Options' % self.APPNAME.capitalize())
|
||||||
|
self.add_application_options(app_options)
|
||||||
|
option_parser.add_option_group(app_options)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
BaseApplication.__init__(self)
|
BaseApplication.__init__(self, option_parser=option_parser)
|
||||||
except BackendsConfig.WrongPermissions, e:
|
except BackendsConfig.WrongPermissions, e:
|
||||||
logging.error(u'Error: %s' % e)
|
logging.error(u'Error: %s' % e)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
@ -52,11 +58,15 @@ class ConsoleApplication(BaseApplication):
|
||||||
command = '%s %s' % (name, arguments)
|
command = '%s %s' % (name, arguments)
|
||||||
self._parser.description += ' %-30s %s\n' % (command, doc_string)
|
self._parser.description += ' %-30s %s\n' % (command, doc_string)
|
||||||
|
|
||||||
self._parser.add_option('-f', '--formatter', default='simple', choices=formatters_classes.keys(),
|
results_options = OptionGroup(self._parser, 'Results Options')
|
||||||
help='select output formatter (%s)' % u','.join(formatters_classes.keys()))
|
results_options.add_option('-c', '--condition', help='filter result items to display given a boolean condition')
|
||||||
self._parser.add_option('-s', '--select', help='select result item key(s) to display (comma-separated)')
|
results_options.add_option('-f', '--formatter', default='multiline', choices=formatters.keys(),
|
||||||
self._parser.add_option('-w', '--where', help='filter results to display with boolean condition')
|
help='select output formatter (%s)' % u','.join(formatters.keys()))
|
||||||
|
results_options.add_option('-s', '--select', help='select result item key(s) to display (comma-separated)')
|
||||||
|
self._parser.add_option_group(results_options)
|
||||||
|
|
||||||
|
def add_application_options(self, group):
|
||||||
|
pass
|
||||||
def _handle_app_options(self):
|
def _handle_app_options(self):
|
||||||
self._formatter = formatters_classes[self.options.formatter]
|
self._formatter = formatters_classes[self.options.formatter]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue