bcrypt.ctf/bcrypter/cli/cmd.py

46 lines
1.3 KiB
Python

from bcrypter.lib.result import Result
class Command:
NAME = '[Abstract]Command'
ARGS = []
FLAGS = []
OPTIONS = []
def __init__(self, args: list[string]) -> None:
pass
@classmethod
def parse(cls: Command, cmd: list[str]) -> Result[Command]:
for arg in cmd[1:]:
# flag or option
if arg.startswith('--'):
match = cls._match_arg(arg[2:])
if match
'''
Check whether FLAGS and OPTIONS are defined consistently
(ie no duplicate names or parsing ambiguity)
'''
@classmethod
def _is_well_defined(cls: Command) -> Result[None]:
raise NotImplementedException('Command.is_consistent()')
'''
Attempt to match an arg to its flag or option
NOTE: _match_arg() assumes _is_well_defined() == True
'''
@classmethod
def _match_arg(cls: Command, arg: str) -> Result[None]:
for opt in chain(cls.FLAGS, cls.OPTIONS):
if opt.matches(arg):
return Result.succeed(None)
return Result.fail()
class Builtin(Command):
self.NAME = '[Abstract]Builtin'
def __init__(self,
repl_builtins: list[Builtin],
repl_cmds: list[Command]) -> None:
super().__init__()
self._repl_builtins = repl_builtins
self._repl_cmds = repl_cmds