Skip to content

tanjun.parsing#

Standard implementation of message command argument parsing.

AbstractParser = AbstractOptionParser module-attribute #

Deprecated alias of AbstractOptionParser.

ConverterSig = typing.Union[collections.Callable[Ellipsis, collections.Coroutine[typing.Any, typing.Any, _T]], collections.Callable[Ellipsis, _T]] module-attribute #

Type hint of a converter used within a parser instance.

This must be a callable or asynchronous callable which takes one position str, argument and returns the resultant value.

UNDEFINED = UndefinedT() module-attribute #

A singleton used to represent an undefined value within parsing logic.

UNDEFINED_DEFAULT = UNDEFINED module-attribute #

Deprecated alias of UNDEFINED.

UndefinedDefaultT = UndefinedT module-attribute #

Deprecated alias of Undefined.

AbstractOptionParser #

Bases: tanjun.MessageParser, abc.ABC

Abstract interface of a message content parser.

add_argument(key, /, converters=(), *, default=UNDEFINED, greedy=False, max_value=None, min_value=None, multi=False) abstractmethod #

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: str

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

default

The default value of this argument, if left as UNDEFINED then this will have no default.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

greedy

Whether or not this argument should be greedy (meaning that it takes in the remaining argument values).

TYPE: bool DEFAULT: False

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

multi

Whether this argument can be passed multiple times.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Self

This parser to enable chained calls.

RAISES DESCRIPTION
ValueError

If key isn't valid for any of the commands this parser is linked to where validate_arg_keys is True.

add_option(key, name, /, *names, converters=(), default, empty_value=UNDEFINED, max_value=None, min_value=None, multi=False) abstractmethod #

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: str

name

The name of this option used for identifying it in the parsed content.

TYPE: str

*names

Other names of this option used for identifying it in the parsed content.

TYPE: str DEFAULT: ()

default

The default value of this option, unlike arguments this is required for options.

TYPE: typing.Any

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

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.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

multi

If this option can be provided multiple times.

TYPE: bool DEFAULT: False

RETURNS DESCRIPTION
Self

This parser to enable chained calls.

RAISES DESCRIPTION
ValueError

If key isn't valid for any of the commands this parser is linked to where validate_arg_keys is True.

arguments() abstractmethod property #

Sequence of the positional arguments registered with this parser.

options() abstractmethod property #

Sequence of the named options registered with this parser.

Argument #

Bases: Parameter

Representation of a positional argument used by the standard parser.

__init__(key, /, *, converters=(), default=UNDEFINED, greedy=False, max_value=None, min_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: str

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

default

The default value of this argument, if left as UNDEFINED then this will have no default.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

greedy

Whether or not this argument should be greedy (meaning that it takes in the remaining argument values).

TYPE: bool DEFAULT: False

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

multi

Whether this argument can be passed multiple times.

TYPE: bool DEFAULT: False

is_greedy() property #

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.

Option #

Bases: Parameter

Representation of a named optional parameter used by the standard parser.

__init__(key, name, /, *names, converters=(), default=UNDEFINED, empty_value=UNDEFINED, max_value=None, min_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: str

name

The name of this option used for identifying it in the parsed content.

TYPE: str

*names

Other names of this option used for identifying it in the parsed content.

TYPE: str DEFAULT: ()

default

The default value of this argument, unlike arguments this is required for options.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

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.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

multi

If this option can be provided multiple times.

TYPE: bool DEFAULT: True

empty_value() property #

The value to return if the option is empty.

If this is UNDEFINED then a value will be required for the option.

names() property #

Sequence of the CLI names of this option.

Parameter #

Base class for parameters for the standard parser(s).

__init__(key, /, *, converters=(), default=UNDEFINED, max_value=None, min_value=None, multi=False) #

Initialise a parameter.

convert(ctx, value) async #

Convert the given value to the type of this parameter.

converters() property #

Sequence of the converters registered for this parameter.

copy() #

Copy the parameter.

RETURNS DESCRIPTION
Self

A copy of the parameter.

default() property #

The parameter's default.

If this is UNDEFINED then this parameter is required.

is_multi() property #

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 #

The key of this parameter used to pass the result to the command's callback.

max_value() property #

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_value() property #

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.

ShlexParser #

Bases: AbstractOptionParser

A shlex based AbstractOptionParser implementation.

__init__() #

Initialise a shlex parser.

UndefinedT #

Singleton used to indicate an undefined value within parsing logic.

with_argument(key, /, converters=(), *, default=UNDEFINED, greedy=False, max_value=None, min_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: str

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

default

The default value of this argument, if left as UNDEFINED then this will have no default.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

greedy

Whether or not this argument should be greedy (meaning that it takes in the remaining argument values).

TYPE: bool DEFAULT: False

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

multi

Whether this argument can be passed multiple times.

TYPE: bool DEFAULT: False

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 key isn't valid for any of the commands this command's parser is linked to where validate_arg_keys is True.

with_greedy_argument(key, /, converters=(), *, default=UNDEFINED, max_value=None, min_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: str

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

default

The default value of this argument, if left as UNDEFINED then this will have no default.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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 key isn't valid for any of the commands this command's parser is linked to where validate_arg_keys is True.

with_multi_argument(key, /, converters=(), *, default=UNDEFINED, max_value=None, min_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: str

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

default

The default value of this argument, if left as UNDEFINED then this will have no default.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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 key isn't valid for any of the commands this command's parser is linked to where validate_arg_keys is True.

with_multi_option(key, name, /, *names, converters=(), default, empty_value=UNDEFINED, max_value=None, min_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: str

name

The name of this option used for identifying it in the parsed content.

TYPE: str

*names

Other names of this option used for identifying it in the parsed content.

TYPE: str DEFAULT: ()

default

The default value of this argument, unlike arguments this is required for options.

TYPE: typing.Any

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

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.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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 key isn't valid for any of the commands this command's parser is linked to where validate_arg_keys is True.

with_option(key, name, /, *names, converters=(), default, empty_value=UNDEFINED, max_value=None, min_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: str

name

The name of this option used for identifying it in the parsed content.

TYPE: str

*names

Other names of this option used for identifying it in the parsed content.

TYPE: str DEFAULT: ()

default

The default value of this argument, unlike arguments this is required for options.

TYPE: typing.Any

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: _MaybeIterable[ConverterSig[typing.Any]] DEFAULT: ()

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.

TYPE: _UndefinedOr[typing.Any] DEFAULT: UNDEFINED

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

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.

TYPE: typing.Optional[_CmpProto[typing.Any]] DEFAULT: None

multi

If this option can be provided multiple times.

TYPE: bool DEFAULT: False

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 key isn't valid for any of the commands this command's parser is linked to where validate_arg_keys is True.

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: _CommandT

RETURNS DESCRIPTION
tanjun.abc.MessageCommand

The command with the parser set.

RAISES DESCRIPTION
ValueError

If the command already has a parser set.