The preparse project allows to preparse arguments before the main parsing. Its ability to deabbreviate options is especially useful for use with click
.
The preparse project allows for preprocessing a list of arguments so that they can be parsed definitively in a later step. This way, the functionality of another parsing system can be modified. The project is especially adapted to work with the click project.
Here is the implementation of the expit project (Version 1.0.10) to illustrate how preparse can be used to modify click.
import math
import click
import preparse
__all__ = ["function", "main"]
def function(x: float) -> float:
"The expit function."
try:
p = math.exp(-x)
except OverflowError:
p = float("+inf")
return 1 / (1 + p)
@preparse.PreParser(posix=False).click()
@click.command(add_help_option=False)
@click.help_option("-h", "--help")
@click.version_option(None, "-V", "--version")
@click.argument("x", type=float)
def main(x: float) -> None:
"Apply the expit function to x."
click.echo(function(x))
PreParser
class
This class is the centerpiece of the preparse.
Its behaviour follows the principles of getopt
from C.
Calling the class creates und returns a new instance.
This property determines the approach of the instance to abbreviations of long options.
This method returns a decorator that infuses the current instance into parse_args.
This method returns a copy of the current instance.
This property holds a dictionary. Its keys are the options. Its values specify nargs.
This property decides how to order flags and positional arguments.
Passing a value equal to "infer_given"
to the property
saves Order.POSIX
to the property
if bool(os.environ.get("POSIXLY_CORRECT"))
and elsewise Order.GIVEN
.
Passing a value equal to "infer_permute"
to the property
saves Order.POSIX
to the property
if bool(os.environ.get("POSIXLY_CORRECT"))
and elsewise Order.PERMUTE
.
This method is at the core of the class.
It returns a preparsed list of arguments.
If None
is passed as args
then sys.argv[1:]
is used.
If the method is applied repeatedly
to a list there will be no changes after the first run.
This property represents the name of the program.
If the value passed to the property is None
then str(sys.argv[0])
is saved.
This method alters the current instance
to reflect a click.Command
object.
Concretely the optdict
property is changed.
This method alters the current instance
to reflect a click.Context
object.
Concretely the prog
property is changed.
This method a dict representing the current instance.
This property holds a function that is called
whenever parse_args
encounters a warning.
The warning is given as the only positional arguments.
No keyword arguments are given.
Its return value is discarded.
Therefore, str
is used to discard the warnings.
Click
classThis dataclass provides decorators for embedding a PreParser object.
Calling the class creates und returns a new instance.
This magic method implements self(target)
.
It works as a decorator that embeds its parser
into a given target
from the library
click.
This property determines if the reflectClickCommand
method of parser
is to be called.
This property determines if the reflectClickContext
method of parser
is to be called.
This property holds the parser to be embedded.
These classes are subclasses
of enum.IntEnum
.
Any value different from 0
(meaning no) and 1
(meaning yes)
is interpreted as 2
(meaning intermediate).
Name | Value | Explanation |
---|---|---|
REJECT |
0 |
The parser rejects abbreviations and interprets them as unknown options instead. |
KEEP |
1 |
The parser keeps abbreviations as abbreviations. |
COMPLETE |
2 |
The parser replaces abbreviations with the full options. |
Name | Value | Explanation |
---|---|---|
GIVEN |
0 |
The parser does not seperate flags from positional arguments. |
POSIX |
1 |
The parser assumes the posix standard. |
PERMUTE |
2 |
The parser corrects to posix standard. |
Name | Value | Explanation |
---|---|---|
NO_ARGUMENT |
0 |
The option does not take an argument. |
REQUIRED_ARGUMENT |
1 |
The option requires an argument. |
OPTIONAL_ARGUMENT |
2 |
The option may take an argument. |
These classes are subclasses of Warning
that are specifically designed to communicate issues
with parsing.
This abstract base class is the base for all other warning types.
This class warns about ambiguous options.
This class warns about invalid options.
This class warns about missing required arguments.
This class warns about unallowed arguments.
This class warns about unrecognized options.