def ShortOption(self, char, help=None):
        # type: (str, Optional[str]) -> None
        """Define an option that can be turned off with + and on with -."""

        assert len(char) == 1  # 'r' for -r +r
        self.options[char] = True

        self.defaults[char] = value.Undef()
        # '+' or '-'.  TODO: Should we make it a bool?
        self.fields[char] = flag_type.Str()
Example #2
0
    def PlusFlag(self, char, help=None):
        # type: (str, Optional[str]) -> None
        """Define an option that can be turned off with + and on with -.

    It's actually a ternary value: plus, minus, or unset.
    """
        assert len(char) == 1  # 'r' for -r +r
        self.plus_flags.append(char)

        self.defaults[char] = value.Undef()
        # '+' or '-'.  TODO: Should we make it a bool?
        self.fields[char] = flag_type.Str()
def _FlagType(arg_type):
    # type: (Union[None, int, List[str]]) -> flag_type_t

    if arg_type is None:
        typ = flag_type.Bool()  # type: flag_type_t
    elif arg_type == args.Int:
        typ = flag_type.Int()
    elif arg_type == args.Float:
        typ = flag_type.Float()
    elif arg_type == args.String:
        typ = flag_type.Str()
    elif isinstance(arg_type, list):
        typ = flag_type.Enum(arg_type)
    else:
        raise AssertionError(arg_type)

    return typ
    def ShortFlag(self, short_name, arg_type=None, help=None):
        # type: (str, Optional[int], Optional[str]) -> None
        """
    This is very similar to ShortFlag for FlagSpecAndMore, except we have
    separate arity0 and arity1 dicts.
    """
        assert short_name.startswith('-'), short_name
        assert len(short_name) == 2, short_name

        char = short_name[1]
        if arg_type is None:
            self.arity0[char] = True
        else:
            self.arity1[char] = args.SetToArg(char, _FlagType(arg_type))

        # TODO: callers should pass flag_type
        if arg_type is None:
            typ = flag_type.Bool()  # type: flag_type_t
            default = value.Bool(False)  # type: value_t
        elif arg_type == args.Int:
            typ = flag_type.Int()
            default = value.Int(-1)
        elif arg_type == args.Float:
            typ = flag_type.Float()
            default = value.Float(0.0)
        elif arg_type == args.String:
            typ = flag_type.Str()
            default = value.Str('')
        elif isinstance(arg_type, list):
            typ = flag_type.Enum(arg_type)
            default = value.Str('')  # This isn't valid
        else:
            raise AssertionError(arg_type)

        if self.typed:
            self.defaults[char] = default
        else:
            # TODO: remove when all builtins converted
            self.defaults[char] = value.Undef()

        self.fields[char] = typ