File size: 1,870 Bytes
d766458 | 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 | import sys, getopt
# class for parsing arguments
class parse_args:
def __init__(self):
self.long,self.short = [],[]
self.info,self.help = [],[]
def txt(self,help):
self.help.append(["txt",help])
def add(self, arg, default, type, help=None):
self.long.append(arg[0])
key = arg[0].replace("=","")
self.info.append({"key":key, "type":type,
"value":default, "arg":[f"--{key}"]})
if len(arg) == 2:
self.short.append(arg[1])
s_key = arg[1].replace(":","")
self.info[-1]["arg"].append(f"-{s_key}")
if help is not None:
self.help.append(["opt",[arg,help]])
def parse(self,argv):
for opt, arg in getopt.getopt(argv,"".join(self.short),self.long)[0]:
for x in self.info:
if opt in x["arg"]:
if x["type"] is None: x["value"] = (x["value"] == False)
else: x["value"] = x["type"](arg)
opts = {x["key"]:x["value"] for x in self.info}
print(str(opts).replace(" ",""))
return dict2obj(opts)
def usage(self, err):
for type,info in self.help:
if type == "txt": print(info)
if type == "opt":
arg, helps = info
help = helps[0]
if len(arg) == 1: print("--%-15s : %s" % (arg[0],help))
if len(arg) == 2: print("--%-10s -%-3s : %s" % (arg[0],arg[1].replace(":",""),help))
for help in helps[1:]: print("%19s %s" % ("",help))
print(f"< {err} >")
print(" "+"-"*(len(err)+2))
print(" \ ^__^ ")
print(" \ (oo)\_______ ")
print(" (__)\ )\/\ ")
print(" ||----w | ")
print(" || || ")
sys.exit()
class dict2obj():
def __init__(self, dictionary):
for key in dictionary:
setattr(self, key, dictionary[key]) |