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:
parent
52044e0b16
commit
6f8ef85d85
|
|
@ -16,13 +16,15 @@ class OverrideHelpFormatter(argparse.RawDescriptionHelpFormatter, argparse.HelpF
|
||||||
return super().add_usage(usage, actions, groups, 'Usage: ')
|
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`.
|
"""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
|
Class is to not be directly constructed and initialised. Use `SoypakParser` which implements a singleton pattern and
|
||||||
is passed. There is mangling of attributes in in the parent class to provide a modern, simplistic, and coherent help
|
controls this class.
|
||||||
message display found with other package managers.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
_action_groups__ = []
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.NAME = module_info.__title__
|
self.NAME = module_info.__title__
|
||||||
USAGE = "%(prog)s [options] <command> [arguments]"
|
USAGE = "%(prog)s [options] <command> [arguments]"
|
||||||
|
|
@ -56,13 +58,10 @@ class SoypakParser(argparse.ArgumentParser):
|
||||||
self.add_argument("command", nargs='?')
|
self.add_argument("command", nargs='?')
|
||||||
|
|
||||||
# overwrite the action group. @Note: this is to give custom positional arguments in the help message but aren't
|
# 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).
|
# actual arguments. They are stored in the "command" action (defined above).
|
||||||
# :@TODO: When commands are registered, allow the register to submit info for the action group. This overwrite
|
self._positionals._group_actions = _SoypakParser._action_groups__
|
||||||
# will then be configured using all the register commands.
|
|
||||||
self._positionals._group_actions = []
|
|
||||||
### argparse._StoreAction(option_strings=[], dest=cmd, help=desc)
|
|
||||||
|
|
||||||
# optional arguments
|
# optional arguments - stored in the Namespace
|
||||||
self.add_argument("--version", action="store_true",
|
self.add_argument("--version", action="store_true",
|
||||||
help="show the program's version number and exit")
|
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
|
# we use `parse_known_args` to allow for splitting of largs/rargs
|
||||||
self.namespace, self.rargs = self.parse_known_args()
|
self.namespace, self.rargs = self.parse_known_args()
|
||||||
|
|
||||||
|
# return just the command collected and the rargs
|
||||||
return (self.namespace.command, self.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)
|
||||||
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user