Supporting Class APIs#
The DecreeTree
class has two parents.
It is not expected that they would be used directly.
However, the following is a reference guide for them and
for other classes used via composition.
Decree#
It is possible to create single-level commands using the
Decree
class:
- class decree_tree.decree.Decree(*, name=None, prog_is_name=False, version=None)[source]#
A class for creating a command using
argparse
.Configure variables that change during execution.
- Parameters:
name (
str
|None
) – the command name, if overriding it for the instanceprog_is_name (
bool
) – whether to useself.name
as the program name in helpversion (
str
|None
) – version information to display
- Raises:
ValueError – if name is an empty string
- argument_parser_class[source]#
the argument parser to use at the root level (and inherited there from)
alias of
ModifiedArgumentParser
- repr_kwargs()[source]#
Provide relevant non-default __init__ kwargs for __repr__.
- Returns:
dict
[str
,str
|bool
|None
] – the relevant kwargs in the desired order
- configure_parser()[source]#
Configure the parser for this object. Typically not called by nor overridden in end-user code.
- Return type:
None
- parser_options(subparser=False)[source]#
Add to and override options passed to the argparse parser.
- Parameters:
subparser (
bool
) – whether the options are applied to a subparser applied via subparsers.add_parser() versus a top level parser initialization- Returns:
dict
[str
,Any
] – the options to provide to the parser
- debug_print(message, **kwargs)[source]#
Print an internal debug statement. Could be overridden to use logging. By default, expects message to support str.format() substitution, including the self variable.
- Parameters:
message (
str
) – the message to print, using str.format() keywords**kwargs (
Any
) – the arguments to specify to str.format() in addition to self
- Return type:
None
- run(argv=None, options=None, *, debug_tracing=False, argument_parser_class=None)[source]#
Run the command by itself via command line or with defined arguments.
- Parameters:
argv (
Sequence
[str
] |None
) – command-line arguments to parseoptions (
Namespace
|None
) – processed arguments, circumventing parse_args if specifieddebug_tracing (
bool
) – whether to show debug tracing statementsargument_parser_class (
type
[ArgumentParser
] |None
) – an override for the argument parser class
- Returns:
Any
– the results, to facilitate testing
- add_arguments(parser)[source]#
Add arguments to
parser
(notself._decree_parser
), if any. Subclass overrides typically include a call tosuper().add_arguments(parser)
. Handle manual argument processing inprocess_options
, not here.- Parameters:
parser (
ArgumentParser
) – the parser to which arguments will be added- Return type:
None
- preprocess_options(argv=None, options=None)[source]#
Populate
self.options
if it isn’t already. Typically not called by nor overridden in end-user code.- Parameters:
argv (
Sequence
[str
] |None
) – command-line arguments to parseoptions (
Namespace
|None
) – processed arguments, circumventing parse_args if specified
- Return type:
None
Tree#
It is possible to create generic trees of objects using
the Tree
class:
- class decree_tree.tree.Tree(**kwargs)[source]#
Bases:
Generic
[T
]Manage a generic tree structure.
Initialize the tree structure.
- Parameters:
**kwargs (
Any
) – any keyword arguments needed for subclass customization
- append_child(child)[source]#
Append a new child.
- Parameters:
child (
TypeVar
(T
, bound= Tree) |Type
[TypeVar
(T
, bound= Tree)]) – the object, class, or subclass to append- Returns:
TypeVar
(T
, bound= Tree) – the child itself, in case it was created within this method
- property children: list[T][source]#
Provide a copy of the child objects. This could be a generator, but since the list of children is expected to be short, there shouldn’t be a need.
- Returns:
the list of child instances
- child_index(name, raise_exception=False)[source]#
Determine the index of a child.
- Parameters:
name (
str
) – the name of the child to look upraise_exception (
bool
) – whether to raise an exception if the child cannot be found
- Raises:
InvalidName – if the child cannot be found and raise_exception is True
- Returns:
int
|None
– the index of the child or None if not found
- property child_names: list[str][source]#
Provide a list of the names of the child objects. This could be a generator, but since the list of children is expected to be short, there shouldn’t be a need.
- Returns:
the list of the names of the child instances
- property count_children: int[source]#
Provide the number of children.
- Returns:
the number of children
- find_child(name, raise_exception=False)[source]#
Find a direct child by name and return it or None.
- Parameters:
name (
str
) – the name of the child to findraise_exception (
bool
) – whether to raise an exception if the child cannot be found
- Raises:
InvalidName – if the child cannot be found and raise_exception is True
- Returns:
TypeVar
(T
, bound= Tree) |None
– the child or None if not found
- get_child(*args)[source]#
Look up and provide the nested child object.
- Parameters:
*args (
str
) – the nested path of names to the requested object- Raises:
InvalidName – if a matching object cannot be found
- Returns:
Tree
[TypeVar
(T
, bound= Tree)] – the object found
- property get_name: str[source]#
A way to determine the “name” of an object. Rather than try to directly include the “name” variable from Decree in Tree, define a method that is intended to be overridden by a subclass. This method could perhaps be private, but that would force special handling in several Tree methods.
- Returns:
the “name” of the instance
- insert_child(index, child)[source]#
Insert a new child.
- Parameters:
index (
int
) – the position at which to insert the childchild (
TypeVar
(T
, bound= Tree) |Type
[TypeVar
(T
, bound= Tree)]) – the object, class, or subclass to insert
- Returns:
TypeVar
(T
, bound= Tree) – the child object
- property parent: T | None[source]#
Return the parent of the object, if any.
- Returns:
the parent object or None
- property parents: list[T][source]#
Generate a list of the parents of this tree, in order. This could be a generator, but since the list of parents is expected to be short, there shouldn’t be a need.
- Returns:
the parents
- property parent_names: list[str][source]#
Generate a list of the names of the parents of this tree, in order. This could be a generator, but since the list of parents is expected to be short, there shouldn’t be a need.
- Returns:
the names of the parents
- remove_child(name)[source]#
Remove a child.
- Parameters:
name (
str
) – The name of the child to remove.- Return type:
None
- replace_child(name, child, raise_exception=False)[source]#
Replace a child.
- Parameters:
name (
str
) – the name of the child to replacechild (
TypeVar
(T
, bound= Tree) |Type
[TypeVar
(T
, bound= Tree)]) – the new object, class, or subclassraise_exception (
bool
) – the raise_exception argument to the index() call
- Returns:
TypeVar
(T
, bound= Tree) |None
– the instantiated child object
- sort_children(*, key=None, reverse=False)[source]#
Alphabetically sort the list of subcommands in place.
- Parameters:
key (
Callable
[[TypeVar
(T
, bound= Tree)],SupportsLT
|SupportsGT
] |None
) – a single-argument function used to extract the comparison key for each childreverse (
bool
) – whether to sort in reverse
- Return type:
None
ModifiedArgumentParser#
The ModifiedArgumentParser
class enhances the standard
argparse.ArgumentParser
parser:
- class decree_tree.parser.ModifiedArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True)[source]#
Bases:
ArgumentParser
An enhanced argument parser subclass.
- parse_known_args(args=None, namespace=None)[source]#
Force parse_known_args to behave like parse_args. This ensures that usage statements for unexpected arguments are as specific as possible for the chosen subcommand. Note that add_subparsers calls will reuse the caller’s own class by default.
- Parameters:
args (
Sequence
[str
] |None
) – the list of strings to parsenamespace (
Any
|None
) – an object to take the attributes, defaulting to an argparse.Namespace object
- Returns:
tuple
[Any
,list
[str
]] – a two item tuple containing the populated namespace and the list of remaining argument strings