_pytest.config.argparsing module

class Parser(usage: Optional[str] = None, processopt: Optional[Callable[[Argument], None]] = None)[source]

Bases: object

Parser for command line arguments and ini-file values.

Variables

extra_info – dict of generic param -> value to display in case there’s an error processing the command line arguments.

prog: Optional[str] = None
processoption(option: _pytest.config.argparsing.Argument)None[source]
getgroup(name: str, description: str = '', after: Optional[str] = None)_pytest.config.argparsing.OptionGroup[source]

get (or create) a named option Group.

Name

name of the option group.

Description

long description for –help output.

After

name of other group, used for ordering –help output.

The returned group object has an addoption method with the same signature as parser.addoption but will be shown in the respective group in the output of pytest. --help.

addoption(*opts: str, **attrs: Any)None[source]

register a command line option.

Opts

option names, can be short or long options.

Attrs

same attributes which the add_argument() function of the argparse library accepts.

After command line parsing options are available on the pytest config object via config.option.NAME where NAME is usually set by passing a dest attribute, for example addoption("--long", dest="NAME", ...).

parse(args: Sequence[Union[str, py._path.local.LocalPath]], namespace: Optional[argparse.Namespace] = None)argparse.Namespace[source]
_getparser()_pytest.config.argparsing.MyOptionParser[source]
_preprocess_options_for_no_prefix(groups: List[OptionGroup])None[source]

Add “–no-“-prefixed option names for “store_true” actions.

parse_setoption(args: Sequence[Union[str, py._path.local.LocalPath]], option: argparse.Namespace, namespace: Optional[argparse.Namespace] = None) → List[str][source]
parse_known_args(args: Sequence[Union[str, py._path.local.LocalPath]], namespace: Optional[argparse.Namespace] = None)argparse.Namespace[source]

parses and returns a namespace object with known arguments at this point.

parse_known_and_unknown_args(args: Sequence[Union[str, py._path.local.LocalPath]], namespace: Optional[argparse.Namespace] = None) → Tuple[argparse.Namespace, List[str]][source]

parses and returns a namespace object with known arguments, and the remaining arguments unknown at this point.

addini(name: str, help: str, type: Optional[Literal[‘pathlist’, ‘args’, ‘linelist’, ‘bool’, ‘int’]] = None, *args, **kwargs)None[source]

register an ini-file option.

Parameters
  • name (str) – name of the ini-variable

  • type (str) – type of the variable, one of pathlist, args, linelist, bool, or int.

  • default – default value if no ini-file option exists but is queried.

The value of ini-variables can be retrieved via a call to config.getini(name).

exception ArgumentError(msg: str, option: Union[Argument, str])[source]

Bases: Exception

Raised if an Argument instance is created with invalid or inconsistent arguments.

class Argument(*names: str, **attrs: Any)[source]

Bases: object

class that mimics the necessary behaviour of optparse.Option

it’s currently a least effort implementation and ignoring choices and integer prefixes https://docs.python.org/3/library/optparse.html#optparse-standard-option-types

_typ_map = {'complex': <class 'complex'>, 'float': <class 'float'>, 'int': <class 'int'>, 'string': <class 'str'>}
names() → List[str][source]
attrs() → Mapping[str, Any][source]
_set_opt_strings(opts: Sequence[str])None[source]

directly from optparse

might not be necessary as this is passed to argparse later on

class OptionGroup(name: str, description: str = '', parser: Optional[_pytest.config.argparsing.Parser] = None)[source]

Bases: object

addoption(*optnames: str, **attrs: Any)None[source]

add an option to this group.

if a shortened version of a long option is specified it will be suppressed in the help. addoption(‘–twowords’, ‘–two-words’) results in help showing ‘–two-words’ only, but –twowords gets accepted and the automatic destination is in args.twowords

_addoption(*optnames: str, **attrs: Any)None[source]
_addoption_instance(option: _pytest.config.argparsing.Argument, shortupper: bool = False)None[source]
class StoreTrueWithNoPrefixAction(option_strings, dest, default=False, required=False, help=None)[source]

Bases: argparse._StoreTrueAction

Handle --no-foo for --foo options.

class MyOptionParser(parser: _pytest.config.argparsing.Parser, extra_info: Optional[Dict[str, Any]] = None, prog: Optional[str] = None)[source]

Bases: argparse.ArgumentParser

error(message: str) → NoReturn[source]

Transform argparse error message into UsageError.

parse_args(args: Optional[Sequence[str]] = None, namespace: Optional[argparse.Namespace] = None)argparse.Namespace[source]

allow splitting of positional arguments

_parse_optional(arg_string: str) → Optional[Tuple[Optional[argparse.Action], str, Optional[str]]][source]
class DropShorterLongHelpFormatter(*args: Any, **kwargs: Any)[source]

Bases: argparse.HelpFormatter

shorten help for long options that differ only in extra hyphens

  • collapse long options that are the same except for extra hyphens

  • shortcut if there are only two options and one of them is a short one

  • cache result on action object as this is called at least 2 times

_format_action_invocation(action: argparse.Action)str[source]
_split_lines(text, width)[source]

Wrap lines after splitting on original newlines.

This allows to have explicit line breaks in the help text.