summaryrefslogtreecommitdiff
path: root/src/options.py
blob: 011c039b94afbd384844e3cc3ac09bd23cd0cb46 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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)"
            },
        "u": {
                "name" : "unsafe",
                "flag" : True,
                "desc" : "skip any checksum or signature verification"
                },
        "n": {
                "name" : "no-deps",
                "flag" : True,
                "desc" : "do not resolve dependencies"
                },
        "v": {
                "name" : "verbose",
                "flag" : True,
                "desc" : "print more"
            },
        "c": {
                "name" : "config",
                "flag" : False,
                "desc" : "specify the configuration file to use",
                "default" : "/etc/xipkg.conf"
            }
        }

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 = []

            # is a named argument with a --
            if arg[1] == "-" and len(arg) > 2 and arg[2:].split("=")[0] in names:
                option.appen(names[arg[2:].split("=")[0]])
            # is a single letter argument with a -
            else:
                for letter in arg[1:]:
                    if letter in options:
                        option.append(letter)

                if len(option) == 0:
                    parsed["args"].append(arg)


            # add the option and any values ot the parsed dict
            for opt in option:
                if opt is not None:
                    if options[opt]["flag"]:
                        parsed[opt] = True
                    else:
                        if "=" in arg:
                            parsed[opt] = arg.split("=")[1]
                        else:
                            index += 1
                            parsed[opt] = 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}")