示例#1
0
def lowest_common_type(types: Iterable[cli_types.CliType]):
    type_set = {type(t) for t in types}

    if len(type_set) == 1:
        # If there is only one type, use it
        return type_set.pop()

    if (len(type_set) == 2 and cli_types.CliInteger in type_set
            and cli_types.CliFloat in type_set):
        # If they're all numeric, they can be represented as floats
        return cli_types.CliFloat()

    if {
            cli_types.CliDir,
            cli_types.CliDict,
            cli_types.CliFile,
            cli_types.CliTuple,
            cli_types.CliList,
    } & type_set:
        # These complex types cannot be represented in a simpler way
        raise Exception("There is no common type between {}".format(
            ", ".join(type_set)))

    else:
        # Most of the time, strings can be used to represent primitive types
        return cli_types.CliString()
示例#2
0
    def get_type(self) -> cli_types.CliType:
        # Try the the flag name, then the description in that order

        name_type = infer_type(self.name)
        if name_type is not None:
            return name_type

        return infer_type(self.full_name()) or cli_types.CliString()
示例#3
0
def infer_type(string) -> typing.Optional[cli_types.CliType]:
    """
    Reads a string (argument description etc) to find hints about what type this argument might be. This is
    generally called by the get_type() methods
    """
    if bool_re.match(string):
        return cli_types.CliBoolean()
    elif float_re.match(string):
        return cli_types.CliFloat()
    elif int_re.match(string):
        return cli_types.CliInteger()
    elif file_re.match(string):
        return cli_types.CliFile()
    elif dir_re.match(string):
        return cli_types.CliDir()
    elif str_re.match(string):
        return cli_types.CliString()
    else:
        return cli_types.CliString()
示例#4
0
    def get_type(self) -> cli_types.CliType:
        # Try the argument name, then the flag name, then the description in that order
        arg_type = self.args.get_type()
        if arg_type is not None:
            return arg_type

        flag_type = infer_type(self.full_name())
        if flag_type is not None:
            return flag_type

        return infer_type(self.description) or cli_types.CliString()
示例#5
0
 def get_type(self):
     t = infer_type(self.name) or cli_types.CliString()
     return cli_types.CliList(t)
示例#6
0
 def get_type(self):
     return infer_type(self.name) or cli_types.CliString()