def GetFlagArgument(self, name): """Returns the flag argument object for name. Args: name: The flag name or Namespace destination. Raises: UnknownDestinationException: If there is no registered flag arg for name. Returns: The flag argument object for name. """ if name.startswith('--'): dest = name[2:].replace('-', '_') flag = name else: dest = name flag = '--' + name.replace('_', '-') ai = self._GetCommand().ai for arg in ai.flag_args + ai.ancestor_flag_args: if (dest == arg.dest or arg.option_strings and flag == arg.option_strings[0]): return arg raise parser_errors.UnknownDestinationException( 'No registered flag arg for [{}].'.format(name))
def IsSpecified(self, dest): """Returns True if args.dest was specified on the command line. Args: dest: str, The dest name for the arg to check. Raises: UnknownDestinationException: If there is no registered arg for dest. Returns: True if args.dest was specified on the command line. """ if not hasattr(self, dest): raise parser_errors.UnknownDestinationException( 'No registered arg for destination [{}].'.format(dest)) return dest in self._specified_args
def GetValue(self, dest): """Returns the value of the argument registered for dest. Args: dest: The dest of a registered argument. Raises: UnknownDestinationException: If no arg is registered for dest. Returns: The value of the argument registered for dest. """ try: return getattr(self, dest) except AttributeError: raise parser_errors.UnknownDestinationException( 'No registered arg for destination [{}].'.format(dest))
def GetPositionalArgument(self, name): """Returns the positional argument object for name. Args: name: The Namespace metavar or destination. Raises: UnknownDestinationException: If there is no registered positional arg for name. Returns: The positional argument object for name. """ dest = name.replace('-', '_').lower() meta = name.replace('_', '-').upper() for arg in self._GetCommand().ai.positional_args: if dest == arg.dest or meta == arg.metavar: return arg raise parser_errors.UnknownDestinationException( 'No registered positional arg for [{}].'.format(name))