Beispiel #1
0
 def _KwargsForAttribute(self, name, attribute, required=False):
     """Constructs the kwargs for adding an attribute to argparse."""
     # If this is the only argument in the group, the help text should be the
     # "group" help.
     if len(filter(bool, self.attribute_to_args_map.values())) == 1:
         help_text = self.group_help
     else:
         # Expand the help text.
         help_text = attribute.help_text.format(
             resource=self.concept_spec.name)
     plural = attribute == self.concept_spec.anchor and self.plural
     kwargs_dict = {
         'help': help_text,
         'type': attribute.value_type,
         'completer': attribute.completer
     }
     if util.IsPositional(name):
         if plural and required:
             kwargs_dict.update({'nargs': '+'})
         # The following should not usually happen because anchor args are
         # required.
         elif plural and not required:
             kwargs_dict.update({'nargs': '*'})
         elif not required:
             kwargs_dict.update({'nargs': '?'})
     else:
         kwargs_dict.update({'metavar': util.MetavarFormat(name)})
         if required:
             kwargs_dict.update({'required': True})
         if plural:
             kwargs_dict.update({'type': arg_parsers.ArgList()})
     return kwargs_dict
Beispiel #2
0
 def _KwargsForAttribute(self, name, attribute, is_anchor=False):
     """Constructs the kwargs for adding an attribute to argparse."""
     # Argument is modal if it's the anchor, unless there are fallthroughs.
     # If fallthroughs can ever be configured in the ResourceInfo object,
     # a more robust solution will be needed, e.g. a GetFallthroughsForAttribute
     # method.
     required = is_anchor and not attribute.fallthroughs
     # If this is the only argument in the group, the help text should be the
     # "group" help.
     if len(list(filter(bool, self.attribute_to_args_map.values()))) == 1:
         help_text = self.group_help
     else:
         # Expand the help text.
         help_text = attribute.help_text.format(
             resource=self.resource_spec.name)
     plural = attribute == self.resource_spec.anchor and self.plural
     if attribute.completer:
         completer = attribute.completer
     elif not self.resource_spec.disable_auto_completers:
         completer = completers.CompleterForAttribute(
             self.resource_spec, attribute.name)
     else:
         completer = None
     kwargs_dict = {
         'help': help_text,
         'type': attribute.value_type,
         'completer': completer
     }
     if util.IsPositional(name):
         if plural and required:
             kwargs_dict.update({'nargs': '+'})
         # The following should not usually happen because anchor args are
         # required.
         elif plural and not required:
             kwargs_dict.update({'nargs': '*'})
         elif not required:
             kwargs_dict.update({'nargs': '?'})
     else:
         kwargs_dict.update({'metavar': util.MetavarFormat(name)})
         if required:
             kwargs_dict.update({'required': True})
         if plural:
             kwargs_dict.update({'type': arg_parsers.ArgList()})
     return kwargs_dict
Beispiel #3
0
 def _KwargsForAttribute(self, name, attribute, is_anchor=False):
     """Constructs the kwargs for adding an attribute to argparse."""
     # Argument is modal if it's the anchor, unless there are fallthroughs.
     # If fallthroughs can ever be configured in the ResourceInfo object,
     # a more robust solution will be needed, e.g. a GetFallthroughsForAttribute
     # method.
     required = is_anchor and not self.fallthroughs_map.get(
         attribute.name, [])
     final_help_text = self._GetHelpTextForAttribute(attribute,
                                                     is_anchor=is_anchor)
     plural = attribute == self.resource_spec.anchor and self.plural
     if attribute.completer:
         completer = attribute.completer
     elif not self.resource_spec.disable_auto_completers:
         completer = completers.CompleterForAttribute(
             self.resource_spec, attribute.name)
     else:
         completer = None
     kwargs_dict = {
         'help': final_help_text,
         'type': attribute.value_type,
         'completer': completer
     }
     if util.IsPositional(name):
         if plural and required:
             kwargs_dict.update({'nargs': '+'})
         # The following should not usually happen because anchor args are
         # required.
         elif plural and not required:
             kwargs_dict.update({'nargs': '*'})
         elif not required:
             kwargs_dict.update({'nargs': '?'})
     else:
         kwargs_dict.update({'metavar': util.MetavarFormat(name)})
         if required:
             kwargs_dict.update({'required': True})
         if plural:
             kwargs_dict.update({'type': arg_parsers.ArgList()})
     return kwargs_dict
Beispiel #4
0
 def _KwargsForAttribute(self, name, attribute, required=False):
     """Constructs the kwargs for adding an attribute to argparse."""
     # If this is the only argument in the group, the help text should be the
     # "group" help.
     if len(filter(bool, self.attribute_to_args_map.values())) == 1:
         help_text = self.group_help
     else:
         # Expand the help text.
         help_text = attribute.help_text.format(
             resource=self.concept_spec.name)
     kwargs_dict = {
         'help': help_text,
         'type': attribute.value_type,
         'completer': attribute.completer
     }
     if util.IsPositional(name):
         if required:
             kwargs_dict.update({'nargs': 1})
     else:
         kwargs_dict.update({'metavar': util.MetavarFormat(name)})
         if required:
             kwargs_dict.update({'required': True})
     return kwargs_dict