Linking an argument parser and a callable.
Project description
The "arglink" Package
Introduction
The arglink package provides tools that links arguments of a parser and a callable.
The core functions are:
- Setup a parser based on arguments of a callable
- Build a kwargs dict to call the callable using the parsing results
Preparation
To use arglink, in the definition the callable,
each argument should either
- be assigned to a default value other than
None, or - have a type annotation.
Only int, float, bool, and str are supported.
Users should use setup_arglink to decorate the callable.
See the end of this documentation for an example.
setup_arglink accepts two optional parameters.
- help_messages
- Help messages for parameters.
- Keys are names of arguments and values are messages.
- ignore_patterns
- A list of regular expression patterns.
- Arguments matching any of those patterns will be ignored.
- By default, "self", "cls", and one ends with "_" will be ignored.
Usage
The core functions are:
setup_arglink: It attaches attributes required by this package and analyze the callable. See the following example.callable_to_parser: Add the arguments of a callable to a parser.parser_to_callable: Build the kwargs dict for calling the callable from the parsing results.
If a class is decorated or passed, its __init__ method will be analyzed.
Example
Import methods from arglink:
from arglink import setup_arglink, callable_to_parser
Decorate the target callable and prepare the parser:
>>> import typing
>>> @setup_arglink(
... help_messages={
... 'var_1': 'help message for var_1',
... 'var_a': 'help message for var_a',
... 'var_f': 'help message for var_f'
... }
... )
... class TargetClass:
... def __init__(
... self,
... var_to_skip_1_: list,
... var_to_skip_2_: list,
... var_1: int,
... var_2: float,
... var_3: str,
... var_a=1,
... var_b=1.1,
... var_c='',
... var_d1: int | None = None,
... var_d2: typing.Optional[int] = None,
... var_e=True,
... var_f=False,
... var_to_skip_3_=''
... ):
... pass
>>> parser = argparse.ArgumentParser()
>>> callable_to_parser(obj=TargetClass, parser=parser)
>>> parser.print_help() # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
The parser looks like:
usage: ... [-h] --var-1 INT --var-2 FLOAT --var-3 STR
[--var-a INT] [--var-b FLOAT] [--var-c STR]
[--var-d1 INT] [--var-d2 INT] [--var-e-toggle]
[--var-f-toggle]
<BLANKLINE>
options:
-h, --help show this help message and exit
<BLANKLINE>
arguments for "__main__.TargetClass.__init__":
--var-1 INT help message for var_1
--var-2 FLOAT
--var-3 STR
--var-a INT (default: 1) help message for var_a
--var-b FLOAT (default: 1.1)
--var-c STR (default: '')
--var-d1 INT (default: None)
--var-d2 INT (default: None)
--var-e-toggle (default: True)
--var-f-toggle (default: False) help message for var_f
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file arglink-1.3.0.tar.gz.
File metadata
- Download URL: arglink-1.3.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab0b70e9a34c7d8d5980201d8ef21b5222e6a678625e60695079f386d397b34f
|
|
| MD5 |
f2679bfe1f6bfdd4c2e40d92749e3af2
|
|
| BLAKE2b-256 |
2f49e89729742abd5d7d0dc2347315a927ec83fa516a92f8491aaf2d73985f11
|
File details
Details for the file arglink-1.3.0-py3-none-any.whl.
File metadata
- Download URL: arglink-1.3.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a0b259e25a7f43eb4db13a4af9065e6af1ae5cb7e4d10969f36d2209cc3456d
|
|
| MD5 |
cb7d0a8041324a0164067d3bd7efaaed
|
|
| BLAKE2b-256 |
3ed4ba79c8e6417ec51f8c21538ec6955a41758eac325a6ae286884a9085fe24
|