importlib-metadata-argparse-version
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
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:
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:
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:
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.
Metadata
Release files for importlib-metadata-argparse-version 3.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| importlib_metadata_argparse_version-3.0.0.tar.gz | 5.5 kB | Without provenance | Details for importlib_metadata_argparse_version-3.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| importlib_metadata_argparse_version-3.0.0-py3-none-any.whl | Python 3 | none | any | Details for importlib_metadata_argparse_version-3.0.0-py3-none-any.whl |
Total release size: 10.8 kB
Release files / importlib_metadata_argparse_version-3.0.0.tar.gz
| Download URL | importlib_metadata_argparse_version-3.0.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d75ead959c6ddf8b61f2967f7dcdf6f8171f67541bd40edf56db0e8aa8b562c7
|
|
BLAKE2b-256 checksum How to use checksums |
2fde69cd270f0b22bd85799f6383afbd4c44ce04ed66c2bcc8f58d0509c3a89e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Release files / importlib_metadata_argparse_version-3.0.0-py3-none-any.whl
| Download URL | importlib_metadata_argparse_version-3.0.0-py3-none-any.whl |
|---|---|
| Size | 5.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
364895f73c7b5bc3435cefa0789bf629d63ca3e43f7d28609c06e544fd7bf302
|
|
BLAKE2b-256 checksum How to use checksums |
36f087b2f5bbfea81746b89ea2a403d46a8b9ea5eb38fef6dae6d8a7ea2cdf56
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Security for importlib-metadata-argparse-version 3.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.