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 see the documentation for your CLI with --help
,
importlib.metadata
will be imported too even when is not needed.
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,
importlib_metadata_version_from="your-module-name",
)
This is a rough equivalent to something like:
import argparse
try:
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--version",
action="version",
version=importlib_metadata.version("your-module-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,
importlib_metadata_version_from="your-module-name",
version="%(prog)s %(version)s",
)
# or
parser.version = "%(prog)s %(version)s"
parser.add_argument(
"-v", "--version",
action=ImportlibMetadataVersionAction,
importlib_metadata_version_from="your-module-name",
)
And the version
kwarg becomes optional, being "%(version)s"
the default value.
Another convenient improvement is that, if you forget to define the
kwarg importlib_metadata_version_from
in the argument, a ValueError
will be raised at initialization time.
Python's argparse
"version"
action raises an AttributeError
only when you call your program with --version
, which is less safer
because could lead you to forget the version=
kwarg and the error
will pass unexpected until you test it.
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
Hashes for importlib-metadata-argparse-version-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23bed556660cd5a88ccd89841b000a2b6f15a08a5fed3a560baddfc4b21f5cc0 |
|
MD5 | 6f6586e754e1a657be33686e4723762c |
|
BLAKE2b-256 | 9aaec8b88e8084cbbcdc268adedc3806270fd6456270ef3d78c87892aa262cd0 |
Hashes for importlib_metadata_argparse_version-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c768864889198712aa766513d4e5ceded79d083179d7eaf39a86139a5576291 |
|
MD5 | 0d86d7472b7a197ff5fd71bcabdd89b3 |
|
BLAKE2b-256 | 9a61ce7d8bbd0bd23f42d7c96ef61887ae022456ca4c13977c7fceff68c08d65 |