Exemple #1
0
  def _construct_partial_parser(self):
    """
      Construct an options parser containing only options added by __main__
      or global help options registered by the application.
    """
    values_copy = copy.deepcopy(self._option_values)
    parser = (options.parser()
              .interspersed_arguments(self._interspersed_args)
              .options(self._main_options)
              .usage(self._usage))

    if hasattr(self._commands.get(self._command), Application.OPTIONS_ATTR):
      if self._command is None:
        command_group = options.new_group('When running with no command')
      else:
        command_group = options.new_group('For command %s' % self._command)
      for option in getattr(self._commands[self._command], Application.OPTIONS_ATTR):
        op = copy.deepcopy(option)
        if not hasattr(values_copy, op.dest):
          setattr(values_copy, op.dest, op.default if op.default != optparse.NO_DEFAULT else None)
        Application.rewrite_help(op)
        op.default = optparse.NO_DEFAULT
        command_group.add_option(op)
      return parser.groups([command_group]).values(values_copy)
    else:
      return parser.values(values_copy)
Exemple #2
0
    def _construct_partial_parser(self):
        """
      Construct an options parser containing only options added by __main__
      or global help options registered by the application.
    """
        values_copy = copy.deepcopy(self._option_values)
        parser = (options.parser().interspersed_arguments(
            self._interspersed_args).options(self._main_options).usage(
                self._usage))

        if hasattr(self._commands.get(self._command),
                   Application.OPTIONS_ATTR):
            if self._command is None:
                command_group = options.new_group(
                    'When running with no command')
            else:
                command_group = options.new_group('For command %s' %
                                                  self._command)
            for option in getattr(self._commands[self._command],
                                  Application.OPTIONS_ATTR):
                op = copy.deepcopy(option)
                if not hasattr(values_copy, op.dest):
                    setattr(
                        values_copy, op.dest, op.default
                        if op.default != optparse.NO_DEFAULT else None)
                Application.rewrite_help(op)
                op.default = optparse.NO_DEFAULT
                command_group.add_option(op)
            return parser.groups([command_group]).values(values_copy)
        else:
            return parser.values(values_copy)
Exemple #3
0
 def register_option(function):
   if Inspection.find_calling_module() == '__main__':
     added_option = self._get_option_from_args(args, kwargs)
     if not hasattr(function, Application.OPTIONS_ATTR):
       new_group = options.new_group('For command %s' % function.__name__)
       setattr(function, Application.OPTIONS_ATTR, new_group)
     getattr(function, Application.OPTIONS_ATTR).prepend_option(added_option)
   return function
Exemple #4
0
 def register_option(function):
     if Inspection.find_calling_module() == '__main__':
         added_option = self._get_option_from_args(args, kwargs)
         if not hasattr(function, Application.OPTIONS_ATTR):
             new_group = options.new_group('For command %s' %
                                           function.__name__)
             setattr(function, Application.OPTIONS_ATTR, new_group)
         getattr(function,
                 Application.OPTIONS_ATTR).prepend_option(added_option)
     return function
Exemple #5
0
 def command_parser(self, command):
   assert command in self._commands
   values_copy = copy.deepcopy(self._option_values)
   parser = self._main_parser()
   command_group = options.new_group(('For %s only' % command) if command else 'Default')
   for option in getattr(self._commands[command], Application.OPTIONS_ATTR, []):
     op = copy.deepcopy(option)
     if not hasattr(values_copy, op.dest):
       setattr(values_copy, op.dest, op.default if op.default != optparse.NO_DEFAULT else None)
     self.rewrite_help(op)
     op.default = optparse.NO_DEFAULT
     command_group.add_option(op)
   parser = parser.groups([command_group]).values(values_copy)
   usage = self._commands[command].__doc__
   if usage:
     parser = parser.usage(usage)
   return parser
Exemple #6
0
 def command_parser(self, command):
   assert command in self._commands
   values_copy = copy.deepcopy(self._option_values)
   parser = self._main_parser()
   command_group = options.new_group(('For %s only' % command) if command else 'Default')
   for option in getattr(self._commands[command], Application.OPTIONS_ATTR, []):
     op = copy.deepcopy(option)
     if not hasattr(values_copy, op.dest):
       setattr(values_copy, op.dest, op.default if op.default != optparse.NO_DEFAULT else None)
     self.rewrite_help(op)
     op.default = optparse.NO_DEFAULT
     command_group.add_option(op)
   parser = parser.groups([command_group]).values(values_copy)
   usage = self._commands[command].__doc__
   if usage:
     parser = parser.usage(usage)
   return parser
Exemple #7
0
 def _add_module_option(self, module, option):
     calling_module = Application._get_module_key(module)
     if calling_module not in self._global_options:
         self._global_options[calling_module] = options.new_group(
             calling_module)
     self._global_options[calling_module].add_option(option)
Exemple #8
0
 def _add_module_option(self, module, option):
   calling_module = self._get_module_key(module)
   if calling_module not in self._global_options:
     self._global_options[calling_module] = options.new_group(calling_module)
   self._global_options[calling_module].add_option(option)