-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.py
46 lines (40 loc) · 1.07 KB
/
args.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
#
# EZ ARGPARSER v1.0.0
# args.py
# By: samulieronen
# Date: 26.Feb.2021
#
from .utils import validate_types
from .utils import get_count
class ArgumentSchemaError(Exception):
pass
class ArgumentError(Exception):
pass
def get_args(argDict, currentSchema, args, amount, name):
if amount < 0 or len(args) < amount:
argDict[name] = validate_types(args, currentSchema)
else:
argDict[name] = validate_types(args[:amount], currentSchema)
if amount == 1:
argDict[name] = argDict[name][0]
return argDict
def parse_args(args, schema):
argDict = {}
index = 0
keyIndex = 0
schemaKeys = list(schema.keys())
if len(schemaKeys) < 1:
return argDict
while index < len(args):
if args[index] != None and keyIndex < len(schemaKeys):
currentSchema = schema[schemaKeys[keyIndex]]
currentSchemaName = schemaKeys[keyIndex]
argDict = get_args(argDict, currentSchema, args[index:], get_count(currentSchema), currentSchemaName)
increment = get_count(currentSchema)
if increment < 0:
break
else:
index += (increment - 1)
keyIndex += 1
index += 1
return argDict