-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement custom argparse action for version display
Substituted the simple version argument with a custom argparse VersionAction in main.py, enhancing the version output information. This new VersionAction class fetches version information from the __about__ file, combining it with the Python version, and formats it appropriately for output.
- Loading branch information
1 parent
d7142d5
commit e644e9e
Showing
2 changed files
with
32 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""Provides a custom argparse action to show program's version and exit.""" | ||
import logging | ||
import sys as _sys | ||
from argparse import SUPPRESS, Action | ||
|
||
from .__about__ import __version__ | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class VersionAction(Action): | ||
"""Custom argparse action to show program's version and exit.""" | ||
|
||
def __init__(self, **kwargs): | ||
# Set default values if not provided in kwargs | ||
kwargs.setdefault("dest", SUPPRESS) | ||
kwargs.setdefault("default", SUPPRESS) | ||
kwargs.setdefault("nargs", 0) | ||
kwargs.setdefault("help", "show program's version number and exit") | ||
super().__init__(**kwargs) | ||
self.version = kwargs.get("version2") | ||
|
||
def __call__(self, parser, namespace, values, option_string=None): | ||
version = f"%(prog)s {__version__}, Python {_sys.version_info.major}.{_sys.version_info.minor}" | ||
formatter = parser.formatter_class(prog=parser.prog) | ||
formatter.add_text(version) | ||
_sys.stdout.write(formatter.format_help()) | ||
parser.exit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters