mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 18:15:28 +03:00
work on
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
__all__ = ["ArgParse"]
|
||||||
|
|
||||||
|
|
||||||
|
from argenta.orchestrator.argparse.entity import ArgParse
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
__all__ = ["BooleanArgument", "PositionalArgument", "OptionalArgument"]
|
__all__ = ["BooleanArgument", "PositionalArgument", "OptionalArgument"]
|
||||||
|
|
||||||
|
|
||||||
from argenta.orchestrator.arguments.models import (BooleanArgument,
|
from argenta.orchestrator.argparse.arguments.models import (BooleanArgument,
|
||||||
PositionalArgument,
|
PositionalArgument,
|
||||||
OptionalArgument)
|
OptionalArgument)
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class PositionalArgument(BaseArgument):
|
|||||||
|
|
||||||
|
|
||||||
class OptionalArgument(BaseArgument):
|
class OptionalArgument(BaseArgument):
|
||||||
def __init__(self, name: str, prefix: Literal['-', '--', '---']):
|
def __init__(self, name: str, prefix: Literal['-', '--', '---'] = '--'):
|
||||||
"""
|
"""
|
||||||
Optional argument, must have the value
|
Optional argument, must have the value
|
||||||
:param name: name of the argument
|
:param name: name of the argument
|
||||||
@@ -39,12 +39,14 @@ class OptionalArgument(BaseArgument):
|
|||||||
|
|
||||||
|
|
||||||
class BooleanArgument(BaseArgument):
|
class BooleanArgument(BaseArgument):
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str, prefix: Literal['-', '--', '---'] = '--'):
|
||||||
"""
|
"""
|
||||||
Boolean argument, does not require a value
|
Boolean argument, does not require a value
|
||||||
:param name: name of the argument
|
:param name: name of the argument
|
||||||
|
:param prefix: prefix of the argument
|
||||||
"""
|
"""
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.prefix = prefix
|
||||||
|
|
||||||
def get_string_entity(self):
|
def get_string_entity(self):
|
||||||
return self.name
|
return self.prefix + self.name
|
||||||
|
|||||||
@@ -1,4 +1,46 @@
|
|||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
from argenta.orchestrator.argparse.arguments.models import (BooleanArgument,
|
||||||
|
OptionalArgument,
|
||||||
|
PositionalArgument)
|
||||||
|
|
||||||
|
|
||||||
class ArgParse:
|
class ArgParse:
|
||||||
def __init__(self, name: str, description: str):
|
def __init__(self, name: str = 'Argenta',
|
||||||
|
description: str = 'Argenta available arguments',
|
||||||
|
epilog: str = 'github.com/koloideal/Argenta | made by kolo',
|
||||||
|
args: list[PositionalArgument | OptionalArgument | BooleanArgument] = None) -> None:
|
||||||
|
"""
|
||||||
|
Cmd argument parser and configurator at startup
|
||||||
|
:param name: the name of the ArgParse instance
|
||||||
|
:param description: the description of the ArgParse instance
|
||||||
|
:param epilog: the epilog of the ArgParse instance
|
||||||
|
:param args: registered and processed arguments
|
||||||
|
"""
|
||||||
self.name = name
|
self.name = name
|
||||||
self.description = description
|
self.description = description
|
||||||
|
self.epilog = epilog
|
||||||
|
|
||||||
|
self.entity = ArgumentParser(prog=name, description=description, epilog=epilog)
|
||||||
|
self.args: list[PositionalArgument | OptionalArgument | BooleanArgument] | None = args
|
||||||
|
|
||||||
|
def set_args(self, *args: PositionalArgument | OptionalArgument | BooleanArgument):
|
||||||
|
"""
|
||||||
|
Sets the arguments to be processed
|
||||||
|
:param args: processed arguments
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
self.args.extend(args)
|
||||||
|
|
||||||
|
def register_args(self):
|
||||||
|
"""
|
||||||
|
Registers initialized command line arguments
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
for arg in self.args:
|
||||||
|
if type(arg) is PositionalArgument:
|
||||||
|
self.entity.add_argument(arg.get_string_entity())
|
||||||
|
elif type(arg) is OptionalArgument:
|
||||||
|
self.entity.add_argument(arg.get_string_entity())
|
||||||
|
elif type(arg) is BooleanArgument:
|
||||||
|
self.entity.add_argument(arg.get_string_entity(), action='store_true')
|
||||||
|
|||||||
@@ -1,20 +1,15 @@
|
|||||||
from argparse import ArgumentParser
|
|
||||||
|
|
||||||
from argenta.app import App
|
from argenta.app import App
|
||||||
from argenta.orchestrator.argparse.arguments import (PositionalArgument,
|
from argenta.orchestrator.argparse import ArgParse
|
||||||
OptionalArgument,
|
|
||||||
BooleanArgument)
|
|
||||||
|
|
||||||
|
|
||||||
class Orchestrator:
|
class Orchestrator:
|
||||||
def __init__(self, *args: PositionalArgument | OptionalArgument | BooleanArgument):
|
def __init__(self, arg_parser: ArgParse):
|
||||||
"""
|
"""
|
||||||
An orchestrator and configurator that defines the behavior of an integrated system, one level higher than the App
|
An orchestrator and configurator that defines the behavior of an integrated system, one level higher than the App
|
||||||
:param args: logged command line arguments at startup
|
:param arg_parser: Cmd argument parser and configurator at startup
|
||||||
"""
|
"""
|
||||||
self.args = args
|
self.arg_parser: ArgParse = arg_parser
|
||||||
self.argparse: ArgumentParser = ArgumentParser()
|
self.arg_parser.register_args()
|
||||||
self._register_args()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def start_polling(app: App) -> None:
|
def start_polling(app: App) -> None:
|
||||||
@@ -26,18 +21,9 @@ class Orchestrator:
|
|||||||
app.run_polling()
|
app.run_polling()
|
||||||
|
|
||||||
def get_args(self):
|
def get_args(self):
|
||||||
return self.argparse.parse_args()
|
|
||||||
|
|
||||||
def _register_args(self):
|
|
||||||
"""
|
"""
|
||||||
Registers initialized command line arguments
|
Returns the arguments parsed
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
for arg in self.args:
|
return self.arg_parser.entity.parse_args()
|
||||||
if type(arg) is PositionalArgument:
|
|
||||||
self.argparse.add_argument(arg.get_string_entity())
|
|
||||||
elif type(arg) is OptionalArgument:
|
|
||||||
self.argparse.add_argument(arg.get_string_entity())
|
|
||||||
elif type(arg) is BooleanArgument:
|
|
||||||
self.argparse.add_argument(arg.get_string_entity(), action='store_const')
|
|
||||||
|
|
||||||
+1
-1
@@ -7,7 +7,7 @@ parser = argparse.ArgumentParser(prog='ProgramName',
|
|||||||
|
|
||||||
parser.add_argument('filename') # positional argument
|
parser.add_argument('filename') # positional argument
|
||||||
parser.add_argument('-c', '--count') # option that takes a value
|
parser.add_argument('-c', '--count') # option that takes a value
|
||||||
parser.add_argument('-v', '--verbose',
|
parser.add_argument('-v',
|
||||||
action='store_const')
|
action='store_const')
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|||||||
@@ -5,14 +5,16 @@ from argenta.app.defaults import PredeterminedMessages
|
|||||||
from argenta.app.dividing_line import DynamicDividingLine
|
from argenta.app.dividing_line import DynamicDividingLine
|
||||||
from argenta.app.autocompleter import AutoCompleter
|
from argenta.app.autocompleter import AutoCompleter
|
||||||
from argenta.orchestrator import Orchestrator
|
from argenta.orchestrator import Orchestrator
|
||||||
|
from argenta.orchestrator.argparse import ArgParse
|
||||||
from argenta.orchestrator.argparse.arguments import (PositionalArgument,
|
from argenta.orchestrator.argparse.arguments import (PositionalArgument,
|
||||||
OptionalArgument,
|
OptionalArgument,
|
||||||
BooleanArgument)
|
BooleanArgument)
|
||||||
|
|
||||||
|
|
||||||
|
arg_parser = ArgParse(args=[PositionalArgument('test'), OptionalArgument('some'), BooleanArgument('verbose')])
|
||||||
app: App = App(dividing_line=DynamicDividingLine(),
|
app: App = App(dividing_line=DynamicDividingLine(),
|
||||||
autocompleter=AutoCompleter('./mock/.hist'))
|
autocompleter=AutoCompleter('./mock/.hist'))
|
||||||
orchestrator: Orchestrator = Orchestrator(PositionalArgument('name'))
|
orchestrator: Orchestrator = Orchestrator(arg_parser)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Reference in New Issue
Block a user