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 part of the CLI
(eg. like with the argument --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,
importlib_metadata_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,
importlib_metadata_version_from="your-package-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-package-name",
)
And the version
kwarg becomes optional, being "%(version)s"
the default value.
For convenience
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
built-in "version"
action raises an
AttributeError
only when you call your program with --version
if
you forget to define the version
kwarg, which is less safer because
could lead you to pass the error unexpected until you test it.
And --version
checking is a really dumb test.
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-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b0d1856b47872d4961c466ddae10625ae3d8983273c59ee77d56f420b63e82f |
|
MD5 | 06297c21555094215b6782a1f0c986a4 |
|
BLAKE2b-256 | f7ff7b933ebc97a817576ceb03f0dfc3e2275782a5fbd0ba244c1323c6be7371 |
Hashes for importlib_metadata_argparse_version-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ddaaf26e6befbae9c5be0b83eba375d6155e5a055b47f0ed5a8581338659d6ac |
|
MD5 | 1515391af9027282e9536b7d12d6f2a0 |
|
BLAKE2b-256 | ed8eab2f0b94205ec1567d8dafbeb45a4caba7d825a6b9dbe6e7ef9b91a1436b |