Updated parser.py

- Changed class@SoypakParser => class@_SoypackParser
- class@SoypakParser is now a singleton pattern which handles
  class@_SoypakParser.
- SoypakParser@add_action_command (static method): add a command to the
  action group (argparser).
This commit is contained in:
Ethan Smith-Coss 2023-07-05 19:05:25 +01:00
parent 52044e0b16
commit 6f8ef85d85
Signed by: TheOnePath
GPG Key ID: 4E7D436CE1A0BAF1

View File

@ -16,13 +16,15 @@ class OverrideHelpFormatter(argparse.RawDescriptionHelpFormatter, argparse.HelpF
return super().add_usage(usage, actions, groups, 'Usage: ')
class SoypakParser(argparse.ArgumentParser):
class _SoypakParser(argparse.ArgumentParser):
"""soypak parsing and translating program arguments given by `sys.argv`.
Class constructs a custom help message display which is shown with arguments `-h|--help` or when an invalid argument
is passed. There is mangling of attributes in in the parent class to provide a modern, simplistic, and coherent help
message display found with other package managers.
Class is to not be directly constructed and initialised. Use `SoypakParser` which implements a singleton pattern and
controls this class.
"""
_action_groups__ = []
def __init__(self) -> None:
self.NAME = module_info.__title__
USAGE = "%(prog)s [options] <command> [arguments]"
@ -56,13 +58,10 @@ class SoypakParser(argparse.ArgumentParser):
self.add_argument("command", nargs='?')
# overwrite the action group. @Note: this is to give custom positional arguments in the help message but aren't
# actual arguments. They are stored in the "command" attribute (defined above).
# :@TODO: When commands are registered, allow the register to submit info for the action group. This overwrite
# will then be configured using all the register commands.
self._positionals._group_actions = []
### argparse._StoreAction(option_strings=[], dest=cmd, help=desc)
# actual arguments. They are stored in the "command" action (defined above).
self._positionals._group_actions = _SoypakParser._action_groups__
# optional arguments
# optional arguments - stored in the Namespace
self.add_argument("--version", action="store_true",
help="show the program's version number and exit")
@ -71,4 +70,28 @@ class SoypakParser(argparse.ArgumentParser):
# we use `parse_known_args` to allow for splitting of largs/rargs
self.namespace, self.rargs = self.parse_known_args()
# return just the command collected and the rargs
return (self.namespace.command, self.rargs)
class SoypakParser:
"""soypak parsing and translating program arguments given by `sys.argv`.
Class constructs a custom help message display which is shown with arguments `-h|--help` or when an invalid argument
is passed. There is mangling of attributes in in the parent class to provide a modern, simplistic, and coherent help
message display found with other package managers.
"""
# Singleton pattern
__instance__ = None
def __new__(cls) -> _SoypakParser:
if not SoypakParser.__instance__:
SoypakParser.__instance__ = _SoypakParser()
return SoypakParser.__instance__
@staticmethod
def add_action_command(*, cmd, help):
_SoypakParser._action_groups__.append(
argparse._StoreAction(option_strings=[], dest=cmd, help=help)
)