# `importlib-metadata-argparse-version`
[![PyPI][pypi-version-badge-link]][pypi-link]
[![Python versions][pypi-pyversions-badge-link]][pypi-link]
[![License][license-image]][license-link]
[![Tests][tests-image]][tests-link]
[![Coverage status][coverage-image]][coverage-link]
Python's [`argparse`] module action to define CLI version with a delayed
call to [`importlib.metadata.version`] only when `--version` argument
is passed.
## Rationale
When you use `importlib.metadata` for adding the version to a CLI utility,
you need to import `importlib.metadata` and call
`importlib.metadata.version("<your-package>")` at initialization time.
If you only want to execute other parts of the CLI
(eg. like passing the option `--help`), `importlib.metadata` will be
imported too even when is not needed at all.
The problem is easily fixed by this module.
## Usage
```python
import argparse
from importlib_metadata_argparse_version import ImportlibMetadataVersionAction
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--version",
action=ImportlibMetadataVersionAction,
version_from="your-package-name",
)
```
This is a rough equivalent to something like:
```python
import argparse
import importlib.metadata
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--version",
action="version",
version=importlib_metadata.version("your-package-name"),
)
```
...but with the difference that `importlib.metadata` will only be
imported when you call `--version`, so it is more efficient.
When using `ImportlibMetadataVersionAction` the `version` kwarg
accepts `%(version)s` as a placeholder like `%(prog)s`. So you
can write something like this to display the program name before the
version:
```python
parser.add_argument(
"-v", "--version",
action=ImportlibMetadataVersionAction,
version_from="your-package-name",
version="%(prog)s %(version)s",
)
# or
parser.version = "%(prog)s %(version)s"
parser.add_argument(
"-v", "--version",
action=ImportlibMetadataVersionAction,
version_from="your-package-name",
)
```
And the `version` kwarg becomes optional, being `"%(version)s"`
the default value.
### Infer package name
The argument `version_from` can be ommitted and the package name
will be inferred from the caller package location:
```python
parser.add_argument(
"-v", "--version",
action=ImportlibMetadataVersionAction,
)
```
## For convenience
If you forget to define the kwarg `version_from` in the argument or the
inferred package name is not found, an exception will be raised at
initialization time. Python's [`argparse`] built-in `"version"` action raises
an `AttributeError` **only when you call your program with `--version`**, which
is unsafe because could lead you to forget to define the `version` passing
the error unnoticed until you test it manually.
[`argparse`]: https://docs.python.org/3/library/argparse.html
[`importlib.metadata.version`]: https://docs.python.org/3/library/importlib.metadata.html?highlight=importlib%20metadata#distribution-versions
[pypi-link]: https://pypi.org/project/importlib-metadata-argparse-version
[pypi-version-badge-link]: https://img.shields.io/pypi/v/importlib-metadata-argparse-version?logo=pypi&logoColor=white
[pypi-pyversions-badge-link]: https://img.shields.io/pypi/pyversions/importlib-metadata-argparse-version?logo=python&logoColor=white
[license-image]: https://img.shields.io/pypi/l/importlib-metadata-argparse-version?color=light-green&logo=freebsd&logoColor=white
[license-link]: https://github.com/mondeja/importlib-metadata-argparse-version/blob/master/LICENSE
[tests-image]: https://img.shields.io/github/actions/workflow/status/mondeja/importlib-metadata-argparse-version/ci.yml?logo=github&label=tests&branch=master
[tests-link]: https://github.com/mondeja/importlib-metadata-argparse-version/actions?query=workflow%3ACI
[coverage-image]: https://img.shields.io/codecov/c/github/mondeja/importlib-metadata-argparse-version?logo=codecov&logoColor=white
[coverage-link]: https://app.codecov.io/gh/mondeja/importlib-metadata-argparse-version
Raw data
{
"_id": null,
"home_page": null,
"name": "importlib-metadata-argparse-version",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "argparse, cli, importlib, metadata, performance, version",
"author": null,
"author_email": "\u00c1lvaro Mond\u00e9jar Rubio <mondejar1994@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/2f/de/69cd270f0b22bd85799f6383afbd4c44ce04ed66c2bcc8f58d0509c3a89e/importlib_metadata_argparse_version-3.0.0.tar.gz",
"platform": null,
"description": "# `importlib-metadata-argparse-version`\n\n[![PyPI][pypi-version-badge-link]][pypi-link]\n[![Python versions][pypi-pyversions-badge-link]][pypi-link]\n[![License][license-image]][license-link]\n[![Tests][tests-image]][tests-link]\n[![Coverage status][coverage-image]][coverage-link]\n\nPython's [`argparse`] module action to define CLI version with a delayed\ncall to [`importlib.metadata.version`] only when `--version` argument\nis passed.\n\n## Rationale\n\nWhen you use `importlib.metadata` for adding the version to a CLI utility,\nyou need to import `importlib.metadata` and call\n`importlib.metadata.version(\"<your-package>\")` at initialization time.\nIf you only want to execute other parts of the CLI\n(eg. like passing the option `--help`), `importlib.metadata` will be\nimported too even when is not needed at all.\n\nThe problem is easily fixed by this module.\n\n## Usage\n\n```python\nimport argparse\n\nfrom importlib_metadata_argparse_version import ImportlibMetadataVersionAction\n\nparser = argparse.ArgumentParser()\nparser.add_argument(\n \"-v\", \"--version\",\n action=ImportlibMetadataVersionAction,\n version_from=\"your-package-name\",\n)\n```\n\nThis is a rough equivalent to something like:\n\n```python\nimport argparse\nimport importlib.metadata\n\nparser = argparse.ArgumentParser()\nparser.add_argument(\n \"-v\", \"--version\",\n action=\"version\",\n version=importlib_metadata.version(\"your-package-name\"),\n)\n```\n\n...but with the difference that `importlib.metadata` will only be\nimported when you call `--version`, so it is more efficient.\n\nWhen using `ImportlibMetadataVersionAction` the `version` kwarg\naccepts `%(version)s` as a placeholder like `%(prog)s`. So you\ncan write something like this to display the program name before the\nversion:\n\n```python\nparser.add_argument(\n \"-v\", \"--version\",\n action=ImportlibMetadataVersionAction,\n version_from=\"your-package-name\",\n version=\"%(prog)s %(version)s\",\n)\n\n# or\n\nparser.version = \"%(prog)s %(version)s\"\nparser.add_argument(\n \"-v\", \"--version\",\n action=ImportlibMetadataVersionAction,\n version_from=\"your-package-name\",\n)\n```\n\nAnd the `version` kwarg becomes optional, being `\"%(version)s\"`\nthe default value.\n\n### Infer package name\n\nThe argument `version_from` can be ommitted and the package name\nwill be inferred from the caller package location:\n\n```python\nparser.add_argument(\n \"-v\", \"--version\",\n action=ImportlibMetadataVersionAction,\n)\n```\n\n## For convenience\n\nIf you forget to define the kwarg `version_from` in the argument or the\ninferred package name is not found, an exception will be raised at\ninitialization time. Python's [`argparse`] built-in `\"version\"` action raises\nan `AttributeError` **only when you call your program with `--version`**, which\nis unsafe because could lead you to forget to define the `version` passing\nthe error unnoticed until you test it manually.\n\n[`argparse`]: https://docs.python.org/3/library/argparse.html\n[`importlib.metadata.version`]: https://docs.python.org/3/library/importlib.metadata.html?highlight=importlib%20metadata#distribution-versions\n[pypi-link]: https://pypi.org/project/importlib-metadata-argparse-version\n[pypi-version-badge-link]: https://img.shields.io/pypi/v/importlib-metadata-argparse-version?logo=pypi&logoColor=white\n[pypi-pyversions-badge-link]: https://img.shields.io/pypi/pyversions/importlib-metadata-argparse-version?logo=python&logoColor=white\n[license-image]: https://img.shields.io/pypi/l/importlib-metadata-argparse-version?color=light-green&logo=freebsd&logoColor=white\n[license-link]: https://github.com/mondeja/importlib-metadata-argparse-version/blob/master/LICENSE\n[tests-image]: https://img.shields.io/github/actions/workflow/status/mondeja/importlib-metadata-argparse-version/ci.yml?logo=github&label=tests&branch=master\n[tests-link]: https://github.com/mondeja/importlib-metadata-argparse-version/actions?query=workflow%3ACI\n[coverage-image]: https://img.shields.io/codecov/c/github/mondeja/importlib-metadata-argparse-version?logo=codecov&logoColor=white\n[coverage-link]: https://app.codecov.io/gh/mondeja/importlib-metadata-argparse-version\n",
"bugtrack_url": null,
"license": null,
"summary": "Argparse action to define CLI version with a delayed call to importlib.metadata",
"version": "3.0.0",
"project_urls": {
"Bug tracker": "https://github.com/mondeja/importlib-metadata-argparse-version/issues",
"Changelog": "https://github.com/mondeja/importlib-metadata-argparse-version/releases",
"Documentation": "https://github.com/mondeja/importlib-metadata-argparse-version?tab=readme-ov-file#usage",
"Source": "https://github.com/mondeja/importlib-metadata-argparse-version"
},
"split_keywords": [
"argparse",
" cli",
" importlib",
" metadata",
" performance",
" version"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "36f087b2f5bbfea81746b89ea2a403d46a8b9ea5eb38fef6dae6d8a7ea2cdf56",
"md5": "c096fe067e1b5906dfb2a5db751f556b",
"sha256": "364895f73c7b5bc3435cefa0789bf629d63ca3e43f7d28609c06e544fd7bf302"
},
"downloads": -1,
"filename": "importlib_metadata_argparse_version-3.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c096fe067e1b5906dfb2a5db751f556b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5304,
"upload_time": "2024-11-02T02:53:19",
"upload_time_iso_8601": "2024-11-02T02:53:19.384797Z",
"url": "https://files.pythonhosted.org/packages/36/f0/87b2f5bbfea81746b89ea2a403d46a8b9ea5eb38fef6dae6d8a7ea2cdf56/importlib_metadata_argparse_version-3.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2fde69cd270f0b22bd85799f6383afbd4c44ce04ed66c2bcc8f58d0509c3a89e",
"md5": "c626596ddee5e8d78e29d8a316e76ff0",
"sha256": "d75ead959c6ddf8b61f2967f7dcdf6f8171f67541bd40edf56db0e8aa8b562c7"
},
"downloads": -1,
"filename": "importlib_metadata_argparse_version-3.0.0.tar.gz",
"has_sig": false,
"md5_digest": "c626596ddee5e8d78e29d8a316e76ff0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 5496,
"upload_time": "2024-11-02T02:53:20",
"upload_time_iso_8601": "2024-11-02T02:53:20.579435Z",
"url": "https://files.pythonhosted.org/packages/2f/de/69cd270f0b22bd85799f6383afbd4c44ce04ed66c2bcc8f58d0509c3a89e/importlib_metadata_argparse_version-3.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-02 02:53:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mondeja",
"github_project": "importlib-metadata-argparse-version",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "importlib-metadata-argparse-version"
}