summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2021-10-30 17:58:56 +0100
committerdavidovski <david@davidovski.xyz>2021-10-30 17:58:56 +0100
commit8799108979f6ffb12e28f0c871a0759c9b45e322 (patch)
tree5b2af417bd35d62940ca1def081fbcbe93ec6170 /src
parent694d02b42eb247ab93bd15c26f5e70ce1bcd4cd0 (diff)
added argument parsing
Diffstat (limited to 'src')
-rw-r--r--src/colors.py21
-rw-r--r--src/xi.py89
2 files changed, 109 insertions, 1 deletions
diff --git a/src/colors.py b/src/colors.py
new file mode 100644
index 0000000..760c3f3
--- /dev/null
+++ b/src/colors.py
@@ -0,0 +1,21 @@
+def esc(code):
+ return f'\033[{code}m'
+
+RESET = esc(0)
+BLACK = esc(30)
+RED = esc(31)
+GREEN = esc(32)
+YELLOW = esc(33)
+BLUE = esc(34)
+MAGENTA = esc(35)
+CYAN = esc(36)
+WHITE = esc(37)
+DEFAULT = esc(39)
+LIGHT_BLACK = esc(90)
+LIGHT_RED = esc(91)
+LIGHT_GREEN = esc(92)
+LIGHT_YELLOW = esc(93)
+LIGHT_BLUE = esc(94)
+LIGHT_MAGENTA = esc(95)
+LIGHT_CYAN = esc(96)
+LIGHT_WHITE = esc(97)
diff --git a/src/xi.py b/src/xi.py
index 8240fc1..0711f0a 100644
--- a/src/xi.py
+++ b/src/xi.py
@@ -1 +1,88 @@
-print("xixixixixi")
+import sys
+
+options = {
+ "y" : {
+ "name" : "no-confirm",
+ "flag" : True,
+ "desc": "will not prompt the user"
+ },
+ "r" : {
+ "name" : "root",
+ "flag" : False,
+ "desc" : "specify the directory to use as the system root",
+ "default" : "/"
+ },
+ "h": {
+ "name": "help",
+ "flag" : True,
+ "desc" : "prints the command usage and exists the program",
+ }
+ }
+
+def parse_args():
+
+ # re-organise the options by name rather than by single letter
+ # a dict with "name": option_leter
+ names = { v["name"] if v["name"] else k : k for k,v in options.items()}
+
+ args = sys.argv
+ index = 1
+
+ # save all of the options into a "parsed" dictionary
+ parsed = {"other" : []}
+
+ while index < len(args):
+ arg = args[index]
+
+ if len(arg) > 1 and arg[0] == "-":
+ option = None
+
+ # is a named argument with a --
+ if arg[1] == "-" and len(arg) > 2 and arg[2:].split("=")[0] in names:
+ option = names[arg[2:].split("=")[0]]
+ # is a single letter argument with a -
+ elif arg[1] in options:
+ option = arg[1]
+ else:
+ parsed["other"].append(arg)
+
+ # add the option and any values ot the parsed dict
+ if option is not None:
+ if options[option]["flag"]:
+ parsed[option] = True
+ else:
+ if "=" in arg:
+ parsed[option] = arg.split("=")[1]
+ else:
+ index += 1
+ parsed[option] = args[index]
+ else:
+ parsed["other"].append(arg)
+
+
+ index += 1
+
+ # add all default values to the parsed options
+ for option in options:
+ if not option in parsed:
+ if options[option]["flag"]:
+ parsed[option] = False
+ else:
+ parsed[option] = options[option]["default"]
+
+ return parsed
+
+def print_usage():
+ for option,o in options.items():
+ name = o["name"]
+ description = o["desc"]
+ d = ("[default=" + o["default"] + "]") if not o["flag"] else ""
+
+ print(f"\t-{option}, --{name}\t{d}")
+ print(f"\t\t{description}\n")
+
+
+opts = parse_args()
+if opts["h"]:
+ print_usage()
+