ArgTyper
- class argtyper.ArgTyper(func: Callable, progname: Optional[str] = None, ignore_types: Optional[List] = None, ignore_args: Optional[List[str]] = None, hardcoded_names: Optional[Dict[str, Any]] = None, hardcoded_types: Optional[Dict[Any, Any]] = None, arg_defaults: Optional[Dict[str, Any]] = None, version: Optional[str] = None)
The main Argtyper class
Argtyper is a wrapper to allow the creation of an argparser.ArgumentParser by using function type information and (optionally) decorators. You can either use it to parse command line arguments or strings into arguments for function calls
- Parameters
func – The function to call/wrap as entrypoint
progname – Optional program name. If not set to None, only run this ArgTyper if the program name matches this parameter. This option only has an effect if the ArgTyper is used to parse command line arguments, and not if strings are parsed
ignore_types – A List of types for those the parameters will be ignored when creating the parser for commands and subcommands
ignore_args – List of argument names to ignore when creating the parser for commands and subcommands
hardcoded_args – Mapping of
argname:valuesto be used as argument for parameters with those nameshardcoded_types – Mapping of
<types>:valuesto be used as argument for parameters with those typesarg_defaults – Mapping of Parameter names to default values. This will be passed to
argparse. ArgumentParser.set_defaults()after processing the defaults for the Command wrappers. So, this will always overwrite defaults set somewhere else.version – If provided, the
--versionand-varguments will be added and will print the provided version string.%(prog)scan be used to reference the current program name
- async call_parser_async(input_args: List[str]) List
Run the parser on the input arguments and execute the corresponding functions
- call_parser_sync(input_args: List[str]) List
Run the parser on the input arguments and execute the corresponding functions
- get_function_calls(input_args) List[Tuple[Callable, Dict[str, Any]]]
Run the parser on the input and return a List with a mapping of (function , kwargs) for matches
- get_parser()
Prepare and return the ArgumentParser instance
- class argtyper.base.ArgTyperAttribute
A generic base class for ArgTyper function attributes
- classmethod get(func: Callable, raise_exc: bool = False, default: Optional[Any] = None)
Get an existing attribute for a function.
If the function does not have this attribute set default will be returned
- Parameters
func – The callable for which we want to retrieve this ArgTyper attribute
raise_exc – If set to True, this will raise an exception, if the function does not have this attribute set
default – The default value to return, in case we don’t find the element and do not raise an exception
- classmethod get_or_create(func: Callable, default=None)
Get an existing endpoint. If the endpoint does not exist it will be created
- Parameters
func – The callable for which we want to retrieve this ArgTyper attribute
default – The default value to set this attribute to, in case it can’t be found and needs to be created. If set do None (default), an instance of the class will be created. Otherwise, default will be used.
- get_set_options(ignore=None) Dict
Returns the arg_options that do not have the default value set
- classmethod has_attribute(func: Callable) bool
Check if a Callable has any ArgTyper attribute set
Decorators
- class argtyper.Command(*, ignore_args: typing.Optional[typing.List[str]] = None, ignore_types: typing.Optional[typing.List] = None, hardcoded_names: typing.Optional[typing.Dict[str, typing.Any]] = None, hardcoded_types: typing.Optional[typing.Dict[typing.Any, typing.Any]] = None, arg_defaults: typing.Optional[typing.Dict[str, typing.Any]] = None, prog=<argtyper.base.DEFAULT object>, usage=<argtyper.base.DEFAULT object>, description=<argtyper.base.DEFAULT object>, epilog=<argtyper.base.DEFAULT object>, parents=<argtyper.base.DEFAULT object>, formatter_class=<argtyper.base.DEFAULT object>, prefix_chars=<argtyper.base.DEFAULT object>, fromfile_prefix_chars=<argtyper.base.DEFAULT object>, argument_default=<argtyper.base.DEFAULT object>, conflict_handler=<argtyper.base.DEFAULT object>, add_help=<argtyper.base.DEFAULT object>, allow_abbrev=<argtyper.base.DEFAULT object>, exit_on_error=<argtyper.base.DEFAULT object>, help=<argtyper.base.DEFAULT object>)
Command object to hold information on ArgTyper commands
The attributes are either used to instantiate an
argparser.ArgumentParserinstance or are passed tosubparsers.add_parser()in case this command is a subcommand of another command. Look for a description in the official documentation of argparser. The argument help will only take effect for subcommands.The remaining arguments described here are used for ArgTyper configuration.
- Parameters
ignore_args – List of parameter names to be ignored when creating the argument parser
ignore_types – List of types to be ignored when creating the argument parser.
harcoded_names – Mapping of argname:values to be used as argument for parameters with those names This will always have precedence and overwrite anything else that might have been passed. Use arg_defaults in case this is not the behaviour you want. It also Takes precedence over hardcode_types
hardcoded_types – Mapping of <types>:values to be used as argument for parameters with those types. This will always overwrite parameters with this type and ignore things set in arg_defaults.
arg_defaults – Mapping of Parameter names to default values. This will be passed to argparse.ArgumentParser.set_defaults
- class argtyper.Argument(reference, *name_or_flags: str, action: typing.Union[str, typing.Type[argparse.Action], argtyper.base.DEFAULT] = <argtyper.base.DEFAULT object>, nargs: typing.Union[int, str, argtyper.base.DEFAULT] = <argtyper.base.DEFAULT object>, const: typing.Any = <argtyper.base.DEFAULT object>, default: typing.Any = <argtyper.base.DEFAULT object>, type: typing.Union[typing.Callable[[str], argtyper.base._T], argparse.FileType, argtyper.base.DEFAULT] = <argtyper.base.DEFAULT object>, choices: typing.Union[typing.Iterable[argtyper.base._T], argtyper.base.DEFAULT] = <argtyper.base.DEFAULT object>, required: typing.Union[bool, argtyper.base.DEFAULT] = <argtyper.base.DEFAULT object>, help: typing.Optional[typing.Union[str, argtyper.base.DEFAULT]] = <argtyper.base.DEFAULT object>, metavar: typing.Optional[typing.Union[str, typing.Tuple[str, ...], argtyper.base.DEFAULT]] = <argtyper.base.DEFAULT object>, dest: typing.Optional[typing.Union[str, argtyper.base.DEFAULT]] = <argtyper.base.DEFAULT object>, version: typing.Union[str, argtyper.base.DEFAULT] = <argtyper.base.DEFAULT object>, **kwargs: typing.Any)
Set options for a specific attribute
This allows one to set additional information or change default values created by ArgTyper for a function parameter. This works similar to the options you can pass to argparsers
add_argument. There are two main differences here:The first parameter (reference) is the name of the function parameter for which you want to change the default options
The next parameter (name_or_flags) is required for add_argument, but will default to the parameters name if left blank here.
The rest of the parameters can be used to override values created automatically by ArgTyper or add additional information (like description or help text)
- Parameters
reference – The name of the function parameter that we want to change the options for
name_or_flags – The alternate names or flags we want to use for this parameter
- class argtyper.SubCommand(subfunction: Union[Callable, str], name: Optional[str] = None)
Add a subcommand to this ArgTyper command
When calling subcommands, all predecessors will be called with their respective arguments as well.
Warning
There is no check in place if you have a circle in your subcommands. (e.g. sub1 -> sub2 -> sub1 -> …). This can happen for example if you use ‘strings’ to resolve command names inside a class. But in that case you will get a
RecursionError- Parameters
subfunction – the function to be used/called as subcommand. This can either be a Callable or string. Callables will be used ‘as is’. For strings, the function will be resolved from innermost to outermost namespace. This can be used to e.g. reference other functions inside the same class to be used as subcommands
name – Optionally, a name to be used for this subcommand
- class argtyper.SubParser(*, title=<argtyper.base.DEFAULT object>, description=<argtyper.base.DEFAULT object>, prog=<argtyper.base.DEFAULT object>, parser_class=<argtyper.base.DEFAULT object>, action=<argtyper.base.DEFAULT object>, option_sring=<argtyper.base.DEFAULT object>, dest=<argtyper.base.DEFAULT object>, required=<argtyper.base.DEFAULT object>, help=<argtyper.base.DEFAULT object>, metavar=<argtyper.base.DEFAULT object>)
Add additional info to the subparser instance, in case the command has subcommands.
This information is passed to
ArgumentParser.add_subparsers
- class argtyper.ArgumentGroup(arguments: List[str], title: Optional[str] = None, description=None)
Add an argument group to this function
- Parameters
arguments – A list of function argument names to be placed in this group. This list should contain the “original” names, not the ones you might have set with Attribute
title – Optionally, a title for this group
description – Optionally, a description for this group
- class argtyper.MutuallyExclusiveArgumentGroup(arguments: List[str], required=False)
Add an argument group to this function
- Parameters
arguments – A list of function argument names to be placed in this group. This list should contain the “original” names, not the ones you might have set with Attribute. Arguments in a mutually exclusive group need to be optional arguments only
required – Indicate if at least one of the arguments is required to be set or not (default: False)
Exceptions
Exceptions for ArgTyper
- exception argtyper.exceptions.ArgParserException(parser: ArgParser, message: Optional[str])
Thrown on Parser errors
- Parameters
parser – The ArgParser instance that raised this errors
message – Optional error message
- exception argtyper.exceptions.ArgParserExitException(parser: ArgParser, status: int, message: Optional[str])
Thrown when the ArgumentParser would have called sys.exit(<code>)
- Parameters
parser – The ArgParser instance that raised this errors
status – Status/Exit code of the ArgParser
message – Optional error message
- exception argtyper.exceptions.ArgTyperArgumentException(func: Callable, argument: str, message: str)
Thrown when something is wrong with a provided Arguement()
- exception argtyper.exceptions.ArgTyperException
Thrown on ArgTyper problems