def _AddSpec(self, presentation_spec): """Adds a given presentation spec to the concept holder's spec registry. Args: presentation_spec: PresentationSpec, the spec to be added. Raises: ValueError: if two presentation specs have the same name, if two presentation specs are both positional, or if two args are going to overlap. """ # Check for duplicate spec names. for spec_name in self._specs: if self._ArgNameMatches(spec_name, presentation_spec.name): raise ValueError('Attempted to add two concepts with the same name: ' '[{}, {}]'.format(spec_name, presentation_spec.name)) if (util.IsPositional(spec_name) and util.IsPositional(presentation_spec.name)): raise ValueError('Attempted to add multiple concepts with positional ' 'arguments: [{}, {}]'.format(spec_name, presentation_spec.name)) # Also check for duplicate attribute names. for a, arg_name in presentation_spec.attribute_to_args_map.iteritems(): del a # Unused. name = util.NormalizeFormat(arg_name) if name in self._all_args: raise ValueError('Attempted to add a duplicate argument name: [{}]' .format(arg_name)) self._all_args.append(name) self._specs[presentation_spec.name] = presentation_spec
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
def title(self): """The title of the arg group for the spec, in all caps with spaces.""" name = self.name.upper() if not util.IsPositional(name): name = name[len(util.PREFIX):].replace('-', ' ') else: name = name.replace('_', ' ') return '{}'.format(name)
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
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
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