def resolve_args_for_command(
        self, command: commands.Command, raw_arg_string: str
    ) -> typing.Tuple[typing.List[str], typing.Dict[str, str]]:
        """
        Resolve the appropriate command arguments from an unparsed string
        containing the raw command arguments.

        This method can be overridden if you wish to customise how arguments
        are parsed for all the commands. If you override this then it is important that
        it at least returns a tuple containing an empty list if no positional arguments
        were resolved, and an empty dict if no keyword arguments were resolved.

        If you override this method then you may find the :obj:`~.stringview.StringView`
        class useful for extracting the arguments from the raw string and the property
        :obj:`~.command.arg_details` which contains information about the command's arguments
        and which are optional or required.

        Args:
            command (:obj:`~.commands.Command`): The command to resolve the arguments for.
            raw_arg_string (:obj:`str`): String containing the raw, unparsed arguments.

        Returns:
            Tuple[ List[ :obj:`str` ], Dict[ :obj:`str1, :obj:`str` ] ]: Positional and keyword
                arguments the command should be invoked with.

        Raises:
            :obj:`~.errors.TooManyArguments`: The command does not ignore extra arguments and too many
                arguments were supplied by the user.
            :obj:`~.errors.NotEnoughArguments`: Not enough arguments were provided by the user to fill
                all required argument fields.
        """
        sv = stringview.StringView(raw_arg_string)
        positional_args, remainder = sv.deconstruct_str(
            max_parse=command.arg_details.maximum_arguments)
        if remainder and command.arg_details.kwarg_name is None and not command._allow_extra_arguments:
            raise errors.TooManyArguments(command)
        if (len(positional_args) +
                bool(remainder)) < command.arg_details.minimum_arguments:
            missing_args = command.arg_details.get_missing_args(
                [*positional_args, *([remainder] if remainder else [])])
            raise errors.NotEnoughArguments(command, missing_args)

        if not remainder:
            remainder = {}
        if remainder and command.arg_details.kwarg_name is not None:
            remainder = {command.arg_details.kwarg_name: remainder}
        return positional_args, remainder
def test_stringview_raises_UnclosedQuotes():
    sv = stringview.StringView('I "am thommo')
    with pytest.raises(errors.UnclosedQuotes) as exc_info:
        sv.deconstruct_str()
    assert exc_info.type is errors.UnclosedQuotes
def test_stringview_splits_quoted_args():
    sv = stringview.StringView('I "am thommo"')
    assert sv.deconstruct_str() == (["I", "am thommo"], "")
def test_stringview_splits_normal_args():
    sv = stringview.StringView("I am thommo")
    assert sv.deconstruct_str() == (["I", "am", "thommo"], "")
Exemple #5
0
def test_stringview_raises_ExpectedSpaces():
    sv = stringview.StringView('I "am"thommo')
    with pytest.raises(errors.ExpectedSpaces) as exc_info:
        sv.deconstruct_str()
    assert exc_info.type is errors.ExpectedSpaces
Exemple #6
0
def test_stringview_splits_quoted_args(text):
    sv = stringview.StringView(text)
    assert sv.deconstruct_str() == (["I", "am", "thommo"], "")