diff options
author | davidovski <david@davidovski.xyz> | 2021-10-30 20:04:31 +0100 |
---|---|---|
committer | davidovski <david@davidovski.xyz> | 2021-10-30 20:04:31 +0100 |
commit | cbf2dc42201ee56e0d2c9f565eee3208ef725aa2 (patch) | |
tree | 7d9f957020543b593ce877cb3c2eb14fc790d38f /src | |
parent | 8799108979f6ffb12e28f0c871a0759c9b45e322 (diff) |
added notes to keep track of how everything should operate
Diffstat (limited to 'src')
-rw-r--r-- | src/__main__.py | 3 | ||||
-rw-r--r-- | src/options.py | 95 | ||||
-rw-r--r-- | src/xi.py | 117 |
3 files changed, 130 insertions, 85 deletions
diff --git a/src/__main__.py b/src/__main__.py index d1abc56..43d8cb7 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,6 +1,5 @@ import xi if __name__ == "__main__": - pass - + xi.main() diff --git a/src/options.py b/src/options.py new file mode 100644 index 0000000..9064369 --- /dev/null +++ b/src/options.py @@ -0,0 +1,95 @@ +import sys + +options = { + "h": { + "name": "help", + "flag" : True, + "desc" : "prints the command usage and exists the program", + } + "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" : "/" + }, + "l": { + "name" : "no-sync", + "flag" : True, + "desc" : "skip syncing with repo sources (not recommended)" + } + } + +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 = {"args" : []} + + 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["args"].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["args"].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") + + if "verbs" in globals(): + print("Available actions:") + for verb in verbs: + print(f"\t{verb}") + + + @@ -1,88 +1,39 @@ -import sys +import options -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 +def search(terms): + print(f"searching for {terms}") + pass - # 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) +def install(terms): + print(f"installing for {terms}") + pass - # 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) - +def remove(terms): + print(f"removing for {terms}") + pass - 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() +verbs = { v: globals()[v] for v in [ + "search", + "install" + "remove" + ] + } +def main(): + opts = options.parse_args() + args = opts["args"] + + if opts["h"]: + options.print_usage() + return + + if len(args) > 0: + verb = args[0].lower() + ( + verbs[verb] if verb in verbs else search + )( + args[1:] if len(args) > 1 else [] + ) + else: + options.print_usage() + return |