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.
The project preparse is strongly inspired by the getopt and getopt_long libraries within C.
Here is the implementation of the expit project (Version 1.0.13) to illustrate how preparse can be used to modify click.
import math
import click
import preparse
__all__ = ["function", "main"]
def function(x: float) -> float:
"This function is the logistical sigmoid, i.e. the expit function."
try:
p = math.exp(-x)
except OverflowError:
p = float("+inf")
return 1 / (1 + p)
@preparse.PreParser().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:
"This command applies 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 boolean property determines whether long options are allowed.
This boolean property determines whether short options are allowed.
This Tuning property determines the approach to bundling of short option.
This method returns a decorator that infuses the current instance into parse_args.
This method returns a copy of the current instance.
This boolean property determines whether recognized abbreviations of long options are to be expanded to their full length.
This boolean property determines whether abbreviations of long options are expected.
This boolean property determines whether the order of arguments to conform to the POSIX standard, i.e. all arguments after the first positional argument also being positonal.
This property holds a dictionary.
Its keys are the options.
Its values specify Nargs.
Options not found within the dictionary cause a PreparseInvalidOptionWarning and are then assumed to have no arguments.
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 str property represents the name of the program.
If the value passed to the property is None
then str(sys.argv[0]) is saved.
This boolean property determines whether compatibility with both orders, POSIX and the interleaving of options with positonal arguments, should be enforced.
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 Tuning property determines the approach to the special argument '--'.
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.
Optdict class
This class is a subclass of datahold.OkayDict
with strings as keys
and with Nargs as values.
It is designed for PreParser.optdict.
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 |
|---|---|---|
MINIMIZE |
0 |
The PreParser minimizes the aspect in question. |
MAXIMIZE |
1 |
The PreParser maximizes the aspect in question. |
MAINTAIN |
2 |
The PreParser does not make any unforced changes to the aspect in question. |
| 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 can only happen for long options.
This class warns about invalid options.
This class warns about missing required arguments.
This class warns about unallowed arguments. This can only happen for long options.