tanjun.parsing#
Standard implementation of message command argument parsing.
AbstractParser
module-attribute
#
AbstractParser = AbstractOptionParser
Deprecated alias of AbstractOptionParser.
ConverterSig
module-attribute
#
ConverterSig = _ConverterSig[Ellipsis, _T]
Type hint of a converter used within a parser instance.
This represents the signatures def (str, ...) -> Any
and
async def (str, ...) -> Any
where dependency injection is supported.
UNDEFINED
module-attribute
#
UNDEFINED = UndefinedT()
A singleton used to represent an undefined value within parsing logic.
AbstractOptionParser #
Bases: tanjun.MessageParser
, abc.ABC
Abstract interface of a message content parser.
arguments
property
abstractmethod
#
arguments: collections.Sequence[Argument]
Sequence of the positional arguments registered with this parser.
options
property
abstractmethod
#
options: collections.Sequence[Option]
Sequence of the named options registered with this parser.
add_argument
abstractmethod
#
add_argument(key, /, converters=(), *, default=UNDEFINED, greedy=False, min_length=None, max_length=None, min_value=None, max_value=None, multi=False)
Add a positional argument type to the parser..
Note
Order matters for positional arguments.
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this argument (may be used to pass the result of this argument to the command's callback during execution).
TYPE:
|
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
default |
The default value of this argument, if left as UNDEFINED then this will have no default. |
greedy |
Whether or not this argument should be greedy (meaning that it takes in the remaining argument values).
TYPE:
|
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this argument are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this argument are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
multi |
Whether this argument can be passed multiple times.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
This parser to enable chained calls. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
add_option
abstractmethod
#
add_option(key, name, /, *names, converters=(), default, empty_value=UNDEFINED, min_length=None, max_length=None, min_value=None, max_value=None, multi=False)
Add an named option to this parser.
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this option which will be used to pass the result of this option to the command's callback during execution as a keyword argument.
TYPE:
|
name |
The name of this option used for identifying it in the parsed content.
TYPE:
|
*names |
Other names of this option used for identifying it in the parsed content.
TYPE:
|
default |
The default value of this option, unlike arguments this is required for options. |
converters |
The converter(s) this option should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
empty_value |
The value to use if this option is provided without a value. If left as UNDEFINED then this option will error if it's provided without a value. |
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this option are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this option are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
multi |
If this option can be provided multiple times.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Self
|
This parser to enable chained calls. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Argument #
Bases: Parameter
Representation of a positional argument used by the standard parser.
is_greedy
property
#
is_greedy: bool
Whether this parameter is greedy.
Greedy parameters will consume the remaining message content as one string (with converters also being passed the whole string).
Note
Greedy and multi parameters cannot be used together.
__init__ #
__init__(key, /, *, converters=(), default=UNDEFINED, greedy=False, min_length=None, max_length=None, min_value=None, max_value=None, multi=False)
Initialise a positional argument.
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this argument (may be used to pass the result of this argument to the command's callback during execution).
TYPE:
|
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
default |
The default value of this argument, if left as UNDEFINED then this will have no default. |
greedy |
Whether or not this argument should be greedy (meaning that it takes in the remaining argument values).
TYPE:
|
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this option are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this option are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
multi Whether this argument can be passed multiple times.
Option #
Bases: Parameter
Representation of a named optional parameter used by the standard parser.
empty_value
property
#
empty_value: _UndefinedOr[typing.Any]
The value to return if the option is empty.
If this is UNDEFINED then a value will be required for the option.
__init__ #
__init__(key, name, /, *names, converters=(), default=UNDEFINED, empty_value=UNDEFINED, min_length=None, max_length=None, min_value=None, max_value=None, multi=True)
Initialise a named optional parameter.
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this option which will be used to pass the result of this argument to the command's callback during execution as a keyword argument.
TYPE:
|
name |
The name of this option used for identifying it in the parsed content.
TYPE:
|
*names |
Other names of this option used for identifying it in the parsed content.
TYPE:
|
default |
The default value of this argument, unlike arguments this is required for options. |
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
empty_value |
The value to use if this option is provided without a value. If left as UNDEFINED then this option will error if it's provided without a value. |
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this option are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this option are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
multi If this option can be provided multiple times.
Parameter #
Base class for parameters for the standard parser(s).
converters
property
#
converters: collections.Sequence[ConverterSig[typing.Any]]
Sequence of the converters registered for this parameter.
default
property
#
default: _UndefinedOr[typing.Any]
The parameter's default.
If this is UNDEFINED then this parameter is required.
is_multi
property
#
is_multi: bool
Whether this parameter is "multi".
Multi parameters will be passed a list of all the values provided for this parameter (with each entry being converted separately.)
key
property
#
key: str
The key of this parameter used to pass the result to the command's callback.
max_length
property
#
max_length: typing.Optional[int]
If set, this parameters's parsed values will have to have lengths less than or equal to this.
If any converters are provided then this should be compatible with the result of them.
max_value
property
#
max_value: typing.Optional[_CmpProto[typing.Any]]
If set, this parameters's parsed values will have to be less than or equal to this.
If any converters are provided then this should be compatible with the result of them.
min_length
property
#
min_length: typing.Optional[int]
If set, this parameters's parsed values will have to have lengths greater than or equal to this.
If any converters are provided then this should be compatible with the result of them.
min_value
property
#
min_value: typing.Optional[_CmpProto[typing.Any]]
If set, this parameters's parsed values will have to be greater than or equal to this.
If any converters are provided then this should be compatible with the result of them.
__init__ #
__init__(key, /, *, converters=(), default=UNDEFINED, min_length=None, max_length=None, min_value=None, max_value=None, multi=False)
Initialise a parameter.
ShlexParser #
UndefinedT #
Singleton used to indicate an undefined value within parsing logic.
with_argument #
with_argument(key, /, converters=(), *, default=UNDEFINED, greedy=False, min_length=None, max_length=None, min_value=None, max_value=None, multi=False)
Add an argument to a message command through a decorator call.
Warning
Since order matters for positional arguments, you'll want to keep in mind that decorator execution starts at the decorator closest to the command and goes upwards with this deciding where a positional argument is located in a command's signature.
Note
If no parser is explicitly set on the command this is decorating before this decorator call then this will set ShlexParser as the parser.
Examples:
import tanjun
@tanjun.parsing.with_argument("command", converters=int, default=42)
@tanjun.parsing.with_parser
@tanjun.component.as_message_command("command")
async def command(self, ctx: tanjun.abc.Context, /, argument: int):
...
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this argument (may be used to pass the result of this argument to the command's callback during execution).
TYPE:
|
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
default |
The default value of this argument, if left as UNDEFINED then this will have no default. |
greedy |
Whether or not this argument should be greedy (meaning that it takes in the remaining argument values).
TYPE:
|
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this argument are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this argument are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
multi Whether this argument can be passed multiple times.
RETURNS | DESCRIPTION |
---|---|
collections.abc.Callable[[tanjun.abc.MessageCommand], tanjun.abc.MessageCommand]
|
Decorator function for the message command this argument is being added to. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
with_greedy_argument #
with_greedy_argument(key, /, converters=(), *, default=UNDEFINED, min_length=None, max_length=None, min_value=None, max_value=None)
Add a greedy argument to a message command through a decorator call.
A greedy argument will consume the remaining positional arguments and pass them through to the converters as one joined string while also requiring that at least one more positional argument is remaining unless a default is set.
Warning
Since order matters for positional arguments, you'll want to keep in mind that decorator execution starts at the decorator closest to the command and goes upwards with this deciding where a positional argument is located in a command's signature.
Note
If no parser is explicitly set on the command this is decorating before this decorator call then this will set ShlexParser as the parser.
Examples:
import tanjun
@tanjun.parsing.with_greedy_argument("command", converters=StringView)
@tanjun.parsing.with_parser
@tanjun.component.as_message_command("command")
async def command(self, ctx: tanjun.abc.Context, /, argument: StringView):
...
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this argument (may be used to pass the result of this argument to the command's callback during execution).
TYPE:
|
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
default |
The default value of this argument, if left as UNDEFINED then this will have no default. Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this argument are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this argument are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
RETURNS | DESCRIPTION |
---|---|
collections.abc.Callable[[tanjun.abc.MessageCommand], tanjun.abc.MessageCommand]
|
Decorator function for the message command this argument is being added to. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
with_multi_argument #
with_multi_argument(key, /, converters=(), *, default=UNDEFINED, min_length=None, max_length=None, min_value=None, max_value=None)
Add a multi-argument to a message command through a decorator call.
A multi argument will consume the remaining positional arguments and pass them to the converters through multiple calls while also requiring that at least one more positional argument is remaining unless a default is set and passing through the results to the command's callback as a sequence.
Warning
Since order matters for positional arguments, you'll want to keep in mind that decorator execution starts at the decorator closest to the command and goes upwards with this deciding where a positional argument is located in a command's signature.
Note
If no parser is explicitly set on the command this is decorating before this decorator call then this will set ShlexParser as the parser.
Examples:
import tanjun
@tanjun.parsing.with_multi_argument("command", converters=int)
@tanjun.parsing.with_parser
@tanjun.component.as_message_command("command")
async def command(self, ctx: tanjun.abc.Context, /, argument: collections.abc.Sequence[int]):
...
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this argument (may be used to pass the result of this argument to the command's callback during execution).
TYPE:
|
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
default |
The default value of this argument, if left as UNDEFINED then this will have no default. |
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this argument are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this argument are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
RETURNS | DESCRIPTION |
---|---|
collections.abc.Callable[[tanjun.abc.MessageCommand], tanjun.abc.MessageCommand]
|
Decorator function for the message command this argument is being added to. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
with_multi_option #
with_multi_option(key, name, /, *names, converters=(), default, empty_value=UNDEFINED, min_length=None, max_length=None, min_value=None, max_value=None)
Add an multi-option to a command's parser through a decorator call.
A multi option will consume all the values provided for an option and pass them through to the converters as an array of strings while also requiring that at least one value is provided for the option unless a default is set.
Note
If no parser is explicitly set on the command this is decorating before this decorator call then this will set ShlexParser as the parser.
Examples:
import tanjun
@tanjun.parsing.with_multi_option("command", converters=int, default=())
@tanjun.parsing.with_parser
@tanjun.component.as_message_command("command")
async def command(self, ctx: tanjun.abc.Context, /, argument: collections.abc.Sequence[int]):
...
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this option which will be used to pass the result of this argument to the command's callback during execution as a keyword argument.
TYPE:
|
name |
The name of this option used for identifying it in the parsed content.
TYPE:
|
*names |
Other names of this option used for identifying it in the parsed content.
TYPE:
|
default |
The default value of this argument, unlike arguments this is required for options. |
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
empty_value |
The value to use if this option is provided without a value. If left as UNDEFINED then this option will error if it's provided without a value. |
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this option are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this option are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
RETURNS | DESCRIPTION |
---|---|
collections.abc.Callable[[tanjun.abc.MessageCommand], tanjun.abc.MessageCommand]
|
Decorator function for the message command this option is being added to. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
with_option #
with_option(key, name, /, *names, converters=(), default, empty_value=UNDEFINED, min_length=None, max_length=None, min_value=None, max_value=None, multi=False)
Add an option to a message command through a decorator call.
Note
If no parser is explicitly set on the command this is decorating before this decorator call then this will set ShlexParser as the parser.
Examples:
import tanjun
@tanjun.parsing.with_option("command", converters=int, default=42)
@tanjun.parsing.with_parser
@tanjun.component.as_message_command("command")
async def command(self, ctx: tanjun.abc.Context, /, argument: int):
...
PARAMETER | DESCRIPTION |
---|---|
key |
The string identifier of this option which will be used to pass the result of this argument to the command's callback during execution as a keyword argument.
TYPE:
|
name |
The name of this option used for identifying it in the parsed content.
TYPE:
|
*names |
Other names of this option used for identifying it in the parsed content.
TYPE:
|
default |
The default value of this argument, unlike arguments this is required for options. |
converters |
The converter(s) this argument should use to handle values passed to it during parsing. If no converters are provided then the raw string value will be passed. Only the first converter to pass will be used.
TYPE:
|
empty_value |
The value to use if this option is provided without a value. If left as UNDEFINED then this option will error if it's provided without a value. |
min_length |
Assert that a string argument's length is greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_length |
Assert that a string argument's length is less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
min_value |
Assert that the parsed value(s) for this option are greater than or equal to this. If any converters are provided then this should be compatible with the result of them. |
max_value |
Assert that the parsed value(s) for this option are less than or equal to this. If any converters are provided then this should be compatible with the result of them. |
multi |
If this option can be provided multiple times.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
collections.abc.Callable[[tanjun.abc.MessageCommand], tanjun.abc.MessageCommand]
|
Decorator function for the message command this option is being added to. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
with_parser #
with_parser(command)
Add a shlex parser command parser to a supported command.
Example#
@tanjun.with_argument("arg", converters=int)
@tanjun.with_parser
@tanjun.as_message_command("hi")
async def hi(ctx: tanjun.MessageContext, arg: int) -> None:
...
PARAMETER | DESCRIPTION |
---|---|
command |
The message command to set the parser on.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tanjun.abc.MessageCommand
|
The command with the parser set. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the command already has a parser set. |