Argparse action to define CLI version with a delayed call to importlib.metadata
Project description
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.
For convenience
If you forget to define the kwarg version_from
in the argument, a ValueError
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 pass the error unnoticed
until you test it when you forget to define the version
kwarg.
Project details
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
File details
Details for the file importlib_metadata_argparse_version-2.0.1.tar.gz
.
File metadata
- Download URL: importlib_metadata_argparse_version-2.0.1.tar.gz
- Upload date:
- Size: 4.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c65657738b2ea7f48b2727ab0cd01fe7e3a8492928611ba0f3df4343058484c |
|
MD5 | 8ea8a3c3856058cc4e73299b71a834d2 |
|
BLAKE2b-256 | 69ff66f128bac3039019e33d97c42cc67087eeba5ea495abf331f35c6373ba58 |
File details
Details for the file importlib_metadata_argparse_version-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: importlib_metadata_argparse_version-2.0.1-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 087e44a534e6430f63ce74bda5c76522dcc1c878ca58fd52fc2d6ca4fbc05f59 |
|
MD5 | 83bfbdfe25107270e49f916b84da2de2 |
|
BLAKE2b-256 | c3e9e7d89b0df111e411883523971626b9d531b9649cbb0e631c01e171358c1e |