def _Default(arg_type, arg_default=None): # type: (Union[None, int, List[str]], Optional[str]) -> value_t # for enum or string # note: not using this for integers yet if arg_default is not None: return value.Str(arg_default) # early return if arg_type is None: default = value.Bool(False) # type: value_t elif arg_type == args.Bool: # for _OilFlagSpec default = value.Bool(False) elif arg_type == args.Int: default = value.Int(-1) # positive values aren't allowed now elif arg_type == args.Float: default = value.Float(-1.0) # ditto elif arg_type == args.String: default = value.Undef() # e.g. read -d '' is NOT the default elif isinstance(arg_type, list): default = value.Str('') # This isn't valid else: raise AssertionError(arg_type) return default
def OnMatch(self, prefix, suffix, arg_r, out): # type: (Optional[str], Optional[str], Reader, _Attributes) -> bool """Called when the flag matches.""" if suffix: # '0' in --verbose=0 if suffix in ('0', 'F', 'false', 'False'): b = False elif suffix in ('1', 'T', 'true', 'Talse'): b = True else: e_usage('got invalid argument to boolean flag: %r' % suffix) else: b = True out.Set(self.name, value.Bool(b)) return False
def PyToValue(py_val): # type: (Any) -> value_t if py_val is None: val = value.Undef() # type: value_t elif isinstance(py_val, bool): val = value.Bool(py_val) elif isinstance(py_val, int): val = value.Int(py_val) elif isinstance(py_val, float): val = value.Float() # TODO: ASDL needs float primitive elif isinstance(py_val, str): val = value.Str(py_val) else: raise AssertionError(py_val) return val
def OnMatch(self, attached_arg, arg_r, out): # type: (Optional[str], Reader, _Attributes) -> bool """Called when the flag matches.""" if attached_arg is not None: # '0' in --verbose=0 if attached_arg in ('0', 'F', 'false', 'False'): # TODO: incorrect translation b = False elif attached_arg in ('1', 'T', 'true', 'Talse'): b = True else: e_usage('got invalid argument to boolean flag: %r' % attached_arg) else: b = True out.Set(self.name, value.Bool(b)) return False
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
def SetTrue(self, name): # type: (str) -> None self.Set(name, value.Bool(True))