Skip to content

Commit

Permalink
Add missing_args to NotEnoughArguments and fix iter method for Wrappe…
Browse files Browse the repository at this point in the history
…dArg
  • Loading branch information
tandemdude committed Dec 7, 2020
1 parent cc1c070 commit 01823c3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lightbulb/command_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,9 @@ class useful for extracting the arguments from the raw string and the property
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) < command.arg_details.minimum_arguments:
raise errors.NotEnoughArguments(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 = {}
Expand Down
6 changes: 5 additions & 1 deletion lightbulb/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, command: Command) -> None:
self.number_positional_args += 1

self.minimum_arguments = sum(
1 for a in self.args.values() if not a.ignore and a.required and a.argtype != inspect.Parameter.KEYWORD_ONLY
1 for a in self.args.values() if not a.ignore and a.required
)
self.maximum_arguments = (
float("inf")
Expand All @@ -182,6 +182,10 @@ def parse_arg(self, index: int, arg: inspect.Parameter) -> ArgInfo:

return ArgInfo(ignore, argtype, annotation, required, default)

def get_missing_args(self, args: typing.List[str]) -> typing.List[str]:
required_command_args = [name for name, arg in self.args.items() if not arg.ignore and arg.required]
return required_command_args[len(args):]


class Command:
"""
Expand Down
3 changes: 3 additions & 0 deletions lightbulb/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def __init__(self, seq: str, context: context_.Context) -> None:
super().__init__(seq)
self.context: context_.Context = context

def __iter__(self):
return iter(self.data)


def _resolve_id_from_arg(arg_string: str, regex: typing.Pattern) -> hikari.Snowflake:
if match := regex.match(arg_string):
Expand Down
4 changes: 3 additions & 1 deletion lightbulb/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ class NotEnoughArguments(CommandError):
Exception raised when a command is run without a sufficient number of arguments.
"""

def __init__(self, command: commands.Command) -> None:
def __init__(self, command: commands.Command, missing_args: typing.List[str]) -> None:
self.command: commands.Command = command
"""The command string that was attempted to be invoked."""
self.missing_args: typing.List[str] = missing_args
"""The required arguments that are missing."""


class TooManyArguments(CommandError):
Expand Down

0 comments on commit 01823c3

Please sign in to comment.