Ejemplo n.º 1
0
    def test_default_unparsed(self):
        flag = _flag.Flag(_argument_parser.ArgumentParser(),
                          _argument_parser.ArgumentSerializer(), 'fruit',
                          'apple', 'help')
        self.assertEqual('apple', flag.default_unparsed)

        flag = _flag.Flag(_argument_parser.IntegerParser(),
                          _argument_parser.ArgumentSerializer(), 'number', '1',
                          'help')
        self.assertEqual('1', flag.default_unparsed)

        flag = _flag.Flag(_argument_parser.IntegerParser(),
                          _argument_parser.ArgumentSerializer(), 'number', 1,
                          'help')
        self.assertEqual(1, flag.default_unparsed)
Ejemplo n.º 2
0
def DEFINE_multi_enum(name,
                      default,
                      enum_values,
                      help=None,
                      flag_values=_flagvalues.FLAGS,
                      case_sensitive=True,
                      **args):
    """Registers a flag whose value can be a list strings from enum_values.

    Use the flag on the command line multiple times to place multiple
    enum values into the list.  The 'default' may be a single string
    (which will be converted into a single-element list) or a list of
    strings.

    Args:
      name: str, the flag name.
      default: Union[Iterable[Text], Text, None], the default value of the flag;
          see `DEFINE_multi`.
      enum_values: [str], a non-empty list of strings with the possible values for
          the flag.
      help: str, the help message.
      flag_values: FlagValues, the FlagValues instance with which the flag will
          be registered. This should almost never need to be overridden.
      case_sensitive: Whether or not the enum is to be case-sensitive.
      **args: Dictionary with extra keyword args that are passed to the
          Flag __init__.
    """
    parser = _argument_parser.EnumParser(enum_values, case_sensitive)
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE_multi(parser, serializer, name, default, help, flag_values, **args)
Ejemplo n.º 3
0
def DEFINE_multi_float(name,
                       default,
                       help=None,
                       lower_bound=None,
                       upper_bound=None,
                       flag_values=_flagvalues.FLAGS,
                       **args):
    """Registers a flag whose value can be a list of arbitrary floats.

    Use the flag on the command line multiple times to place multiple
    float values into the list.  The 'default' may be a single float
    (which will be converted into a single-element list) or a list of
    floats.

    Args:
      name: str, the flag name.
      default: Union[Iterable[float], Text, None], the default value of the flag;
          see `DEFINE_multi`.
      help: str, the help message.
      lower_bound: float, min values of the flag.
      upper_bound: float, max values of the flag.
      flag_values: FlagValues, the FlagValues instance with which the flag will
          be registered. This should almost never need to be overridden.
      **args: Dictionary with extra keyword args that are passed to the
          Flag __init__.
    """
    parser = _argument_parser.FloatParser(lower_bound, upper_bound)
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE_multi(parser, serializer, name, default, help, flag_values, **args)
Ejemplo n.º 4
0
def DEFINE_integer(name,
                   default,
                   help=None,
                   lower_bound=None,
                   upper_bound=None,
                   flag_values=_flagvalues.FLAGS,
                   **args):
    """Registers a flag whose value must be an integer.

    If lower_bound, or upper_bound are set, then this flag must be
    within the given range.

    Args:
      name: str, the flag name.
      default: int|str|None, the default value of the flag.
      help: str, the help message.
      lower_bound: int, min value of the flag.
      upper_bound: int, max value of the flag.
      flag_values: FlagValues, the FlagValues instance with which the flag will
          be registered. This should almost never need to be overridden.
      **args: dict, the extra keyword args that are passed to DEFINE.
    """
    parser = _argument_parser.IntegerParser(lower_bound, upper_bound)
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE(parser, name, default, help, flag_values, serializer, **args)
    _register_bounds_validator_if_needed(parser, name, flag_values=flag_values)
Ejemplo n.º 5
0
def DEFINE_multi_integer(  # pylint: disable=invalid-name,redefined-builtin
        name,
        default,
        help,
        lower_bound=None,
        upper_bound=None,
        flag_values=_flagvalues.FLAGS,
        **args):
    """Registers a flag whose value can be a list of arbitrary integers.

  Use the flag on the command line multiple times to place multiple
  integer values into the list.  The 'default' may be a single integer
  (which will be converted into a single-element list) or a list of
  integers.

  Args:
    name: str, the flag name.
    default: [int]|str|None, the default value of the flag.
    help: str, the help message.
    lower_bound: int, min values of the flag.
    upper_bound: int, max values of the flag.
    flag_values: FlagValues, the FlagValues instance with which the flag will
        be registered. This should almost never need to be overridden.
    **args: Dictionary with extra keyword args that are passed to the
        Flag __init__.
  """
    parser = _argument_parser.IntegerParser(lower_bound, upper_bound)
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE_multi(parser, serializer, name, default, help, flag_values, **args)
Ejemplo n.º 6
0
def DEFINE_multi_string(  # pylint: disable=invalid-name,redefined-builtin
        name,
        default,
        help,
        flag_values=_flagvalues.FLAGS,
        **args):
    """Registers a flag whose value can be a list of any strings.

  Use the flag on the command line multiple times to place multiple
  string values into the list.  The 'default' may be a single string
  (which will be converted into a single-element list) or a list of
  strings.


  Args:
    name: str, the flag name.
    default: Union[Iterable[Text], Text, None], the default value of the flag;
      see `DEFINE_multi`.
    help: str, the help message.
    flag_values: FlagValues, the FlagValues instance with which the flag will be
      registered. This should almost never need to be overridden.
    **args: Dictionary with extra keyword args that are passed to the Flag
      __init__.

  Returns:
    a handle to defined flag.
  """
    parser = _argument_parser.ArgumentParser()
    serializer = _argument_parser.ArgumentSerializer()
    return DEFINE_multi(parser, serializer, name, default, help, flag_values,
                        **args)
Ejemplo n.º 7
0
def DEFINE_float(  # pylint: disable=invalid-name,redefined-builtin
        name,
        default,
        help,
        lower_bound=None,
        upper_bound=None,
        flag_values=_flagvalues.FLAGS,
        **args):
    """Registers a flag whose value must be a float.

  If lower_bound or upper_bound are set, then this flag must be
  within the given range.

  Args:
    name: str, the flag name.
    default: float|str|None, the default value of the flag.
    help: str, the help message.
    lower_bound: float, min value of the flag.
    upper_bound: float, max value of the flag.
    flag_values: FlagValues, the FlagValues instance with which the flag will be
      registered. This should almost never need to be overridden.
    **args: dict, the extra keyword args that are passed to DEFINE.

  Returns:
    a handle to defined flag.
  """
    parser = _argument_parser.FloatParser(lower_bound, upper_bound)
    serializer = _argument_parser.ArgumentSerializer()
    result = DEFINE(parser, name, default, help, flag_values, serializer,
                    **args)
    _register_bounds_validator_if_needed(parser, name, flag_values=flag_values)
    return result
Ejemplo n.º 8
0
 def __init__(self, name, default, help, enum_values,  # pylint: disable=redefined-builtin
              short_name=None, case_sensitive=True, **args):
   p = _argument_parser.EnumParser(enum_values, case_sensitive)
   g = _argument_parser.ArgumentSerializer()
   super(EnumFlag, self).__init__(
       p, g, name, default, help, short_name, **args)
   self.help = '<%s>: %s' % ('|'.join(enum_values), self.help)
Ejemplo n.º 9
0
def DEFINE_string(name,
                  default,
                  help=None,
                  flag_values=_flagvalues.FLAGS,
                  **args):
    """Registers a flag whose value can be any string."""
    parser = _argument_parser.ArgumentParser()
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE(parser, name, default, help, flag_values, serializer, **args)
Ejemplo n.º 10
0
def DEFINE_string(  # pylint: disable=invalid-name,redefined-builtin
        name,
        default,
        help,
        flag_values=_flagvalues.FLAGS,
        **args):
    """Registers a flag whose value can be any string."""
    parser = _argument_parser.ArgumentParser()
    serializer = _argument_parser.ArgumentSerializer()
    DEFINE(parser, name, default, help, flag_values, serializer, **args)
Ejemplo n.º 11
0
 def __init__(
         self,
         name,
         default,
         help,
         enum_class,  # pylint: disable=redefined-builtin
         short_name=None,
         **args):
     p = _argument_parser.EnumClassParser(enum_class)
     g = _argument_parser.ArgumentSerializer()
     super(EnumClassFlag, self).__init__(p, g, name, default, help,
                                         short_name, **args)
     self.help = '<%s>: %s' % ('|'.join(enum_class.__members__), self.help)
Ejemplo n.º 12
0
 def setUp(self):
   self.flag = _flag.Flag(
       _argument_parser.ArgumentParser(),
       _argument_parser.ArgumentSerializer(),
       'fruit', 'apple', 'help')