-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
arguments.py
58 lines (46 loc) · 1.7 KB
/
arguments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
from repl import Repl
from run import ExecuteScript
class ArgumentParserException(object):
def __init__(self, message, suggestion=None, fatal=True):
self.message = message
self.suggestion = suggestion
self.is_fatal = fatal
self.raise_exception()
def raise_exception(self):
print(f'[ERROR] {self.message}')
if self.suggestion:
print(self.suggestion)
if self.is_fatal:
sys.exit(1)
class ArgumentParser(object):
commands = None
flags = []
def __init__(self, arguments=sys.argv[1:]):
self.arguments = arguments
self.length = len(self.arguments)
def create_argument_parser(self):
for argument in self.arguments:
if not argument.startswith('-'):
if not self.commands:
self.commands = argument
else:
exception = ArgumentParserException(
f'Invalid flag: {argument}',
'Flags should start with a comment')
continue
if argument[2:] not in self.flags:
self.flags.append(argument[2:])
return self.commands, self.flags
def create_argument_parser(lexer, parser):
argument_parser = ArgumentParser()
command, flags = argument_parser.create_argument_parser()
if not command or command == 'repl':
Repl('>>> ', lexer, parser)
elif command == 'help':
print("Showing help")
else:
ExecuteScript(command,
error=ArgumentParserException,
lexer=lexer,
parser=parser)