Esempio n. 1
0
def check_argument_name_clashes(arguments: []):
    """
    Checks if an argument name of a command has been used multiple times and raises an exception if so
    :param arguments: arguments of a command to check
    """
    duplicates = find_duplicates(list(map(lambda x: x.name, arguments)))
    if len(duplicates) > 0:
        clashing = ", ".join(duplicates.keys())
        raise ValueError("Argument names must be unique per command! Clashing arguments: {}".format(clashing))
Esempio n. 2
0
def check_command_name_clashes(names: [str]):
    """
    Checks if a command name has been used multiple times and raises an exception if so
    :param names: command names added in this decorator call
    """
    from telegram_click import COMMAND_LIST

    t = []
    t.extend(map(lambda x: x["names"], COMMAND_LIST))
    t.extend([names])
    t = functools.reduce(list.__add__, t)

    duplicates = find_duplicates(t)
    if len(duplicates) > 0:
        clashing = ", ".join(duplicates.keys())
        raise ValueError("Command names must be unique! Clashing names: {}".format(clashing))
Esempio n. 3
0
    def _validate_names(self):
        """
        Validates argument names and raises an exception if something is invalid
        """
        for name in self.names:
            if ARG_VALUE_SEPARATOR_CHAR in name:
                raise ValueError(
                    "Argument names must not contain '=' character: {}".format(
                        name))

        duplicates = find_duplicates(self.names)
        if len(duplicates) > 0:
            clashing = ", ".join(duplicates.keys())
            raise ValueError(
                "Argument names must be unique! Clashing arguments: {}".format(
                    clashing))
Esempio n. 4
0
    def __init__(self, name: str or [str], description: str, example: str, type: type = str, converter: callable = None,
                 optional: bool = False, default: any = None, validator: callable = None):
        """
        Creates a command argument object
        :param name: the name (or names) of the argument
        :param description: a short description of the argument
        :param example: an example (string!) value for this argument
        :param type: the expected type of the argument
        :param converter: a converter function to convert the string value to the expected type
        :param optional: specifies if this argument is optional
        :param default: an optional default value
        :param validator: a validator function
        """
        for c in name:
            if c.isspace():
                raise ValueError("Argument name must not contain whitespace!")
        self.names = [name] if not isinstance(name, list) else name
        duplicates = find_duplicates(self.names)
        if len(duplicates) > 0:
            clashing = ", ".join(duplicates.keys())
            raise ValueError("Argument names must be unique! Clashing arguments: {}".format(clashing))

        self.description = description.strip()
        self.example = example
        self.type = type
        if converter is None:
            if type is str:
                self.converter = lambda x: x
            elif type is bool:
                self.converter = self._boolean_converter
            elif type is int:
                self.converter = lambda x: int(x)
            elif type is float:
                self.converter = self._float_converter
            else:
                raise ValueError("If you want to use a custom type you have to provide a converter function too!")
        else:
            self.converter = converter
        self.optional = optional
        self.default = default
        self.validator = validator