README
The argtoml package wraps around argparse.
It adds the content of a toml file to the cli options.
After parsing, it creates a types.SimpleNameSpace object.
install
Argtoml has no mandatory dependencies outside of pythons standard library.
pip install argtoml
You can optionally install tomli_w if you want to save your configuration at runtime.
pip install 'argtoml[save]'
usage
If there's a src/config.toml
debug = true
home = "~"
[project]
author = "Jono"
name = "argconfig"
pyproject = "./pyproject.toml"
and a src/__main__.py
from argtoml import parse_args # , ArgumentParser
args = parse_args(path=True)
print(args.debug)
print(args.home)
print(args.project.author)
print(args.project.name)
print(args.project.pyproject)
then the shell can look like
$ pwd
/home/jono/project
$ python src/__main__.py --project.name argtoml --no-debug
False
/home/jono
Jono
argtoml
/home/jono/project/pyproject.toml
documentation
There is none, the code is not that large, but I expect you to only use:
parse_args(
# An argparse parser for adding extra arguments not present in the toml.
parser: Optional[argparse.ArumentParser] = None,
# An extra help message.
description: str = "",
# The location of the toml file.
toml_path: pathlib.Path = Path("config.toml"),
# The dictionary in which to look for the toml file.
toml_dir: Optional[TPath] = None,
# Whether to try to interpret strings as paths.
base_path: Union[Path, bool] = True,
# Whether to look for the toml file in the parent of the toml_dir folder.
grandparent: bool = True
) -> SimpleNamespace
save(args: Union[SimpleNamespace, dict], path: pathlib.Path):
with open(path, "wb") as f:
tomli_w.dump(args, f)
toml file location
You are encouraged to specify the location of the toml file when calling parse_args with an absolute path like this:
parse_args(toml_path="/home/user/dir/my_config.toml")
If you provide a relative path, argtoml will look for my_config.toml in the package directory if the main file using argtoml is from a package, otherwise argtoml will look for my_config.toml in the same directory as the main file.
This automatic toml-finding function might change in the future, so probably just provide absolute paths.
packaging
If you want to ship a toml file with your package, make sure to add the toml file to your package.
You should also call parse_args with a relative toml_path.
notes
This is a personal tool thus far, some idiosyncrasies remain:
- Adding dotted arguments not present in the toml might break everything I didn't even test this.
- I don't feel like adding other formats but toml.
- I don't know if, in the above example, the user can do something like
python __main__.py --project {author="jo3"} --project.author jjj, but it should crash if they do this. - Interpreting strings as paths probably only works with unix style paths.
todos
- Add toml comments as argument descriptions.
- Pretty-print the output of parse_args.
- Load and merge multiple toml files
Release files for argtoml 0.5.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| argtoml-0.5.10.tar.gz | 11.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| argtoml-0.5.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.7 kB
Release files / argtoml-0.5.10.tar.gz
| Download URL | argtoml-0.5.10.tar.gz |
|---|---|
| Size | 11.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f87b052e9a30cdae6a99adda7e96df1a3dc4e5b2a4b4afa1df449080241eb81b
|
|
BLAKE2b-256 checksum How to use checksums |
8e2b20550a2a50170537fc3ac48cf801e1764d7334352184a4d4fc7facb491fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.5
|
Release files / argtoml-0.5.10-py3-none-any.whl
| Download URL | argtoml-0.5.10-py3-none-any.whl |
|---|---|
| Size | 11.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d03c6acb8f18b96055e87e5a9f8ddd3ce5921b3bc7c36497d577c33052fe84d5
|
|
BLAKE2b-256 checksum How to use checksums |
e669cbedd2fccebb73157fe2774a6861e0a14335e12645948f163aaf0460dd24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.0.1 CPython/3.12.5
|