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