def dispatch(self, argv): if '--version' in argv: print options.get_version() return # Handle resource completion if '--completion' in argv: self.resource_completion(argv) # Avoid to complete options when no [resource action] are # specified. if '--completion-option' in argv and len(argv) <= 3: return # If there's no arguments or the --help argument without # anything else, print the doc and exit. if len(argv) == 0 or argv[0] == '--help': self.print_resources() sys.stdout.write('\n') parser.parse_args() return # Load the resource module specified in the cmdline. self._load_resource(argv[0]) if self.resource: # Pass the remaining cmdline arguments. self.resource.run(argv[1:]) else: raise ControllerException("Resource '%s' is not loaded" % argv[0])
def _get_options(self, argv, action_name): """ """ # Get option completion. self.option_completion(argv, action_name) # Remove the auto-generated help option. parser.remove_option('--help') # Add custom help options. parser.add_option('--help', dest='help', action='store_true', default=None) attributes = self._get_method_attrs(action_name) if attributes: for option in attributes: parser.add_option('--%s' % option.get('name'), dest=option.get('name'), action=option.get('option_action', 'store'), default=option.get('default', None), help=optparse.SUPPRESS_HELP) else: attributes = [] # Here we go, parse the arg ! try: self.options, args = parser.parse_args(argv) except UnboundLocalError: raise ControllerException("Please specify an argument") # Sets the logger level according to the -v options. level = logging.DEBUG if self.options.debug else logging.INFO syncli_logger = logging.getLogger('syncli') syncli_logger.setLevel(level) if level == 'INFO': for handler in syncli_logger.handlers: formatter = logging.Formatter('%(message)s') handler.setFormatter(formatter) # 1) Transforms OptParse options to dict. # 2) Remove the entry if the value is None. self.options = dict((k, v) for k, v in vars(self.options).iteritems() \ if v is not None) # Display the help for this action ? if self.options.get('help'): self.print_options(action_name) # TODO: Raise an exception ? Are you sure ? raise ControllerException() return self.options, args[0] if len(args) else None