options.c 1.4 KiB raw
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
5
#include "io.h"
6
#include "options.h"
7
#include "types.h"
8
9
/* Create a new options struct. */
10
struct options options(int argc, char *argv[]) {
11
    return (struct options){
12
        .inputs   = { 0 },
13
        .ninputs  = 0,
14
        .modules  = { 0 },
15
        .nmodules = 0,
16
        .argv     = argv,
17
        .argc     = argc,
18
        .output   = NULL,
19
    };
20
}
21
22
/* Parse the command line options. */
23
int options_parse(struct options *o) {
24
    for (int i = 1; i < o->argc; i++) {
25
        if (o->argv[i][0] != '-') {
26
            o->inputs[o->ninputs++] = o->argv[i];
27
            continue;
28
        }
29
        char *arg = &o->argv[i][1];
30
31
        if (!strcmp(arg, "o")) {
32
            if (++i >= o->argc)
33
                bail("`-o` requires an output path");
34
            o->output = o->argv[i];
35
        } else if (!strcmp(arg, "mod")) {
36
            if (++i >= o->argc)
37
                bail("`-mod` requires a module path");
38
            o->modules[o->nmodules++] = o->argv[i];
39
        } else if (!strcmp(arg, "pkg") || !strcmp(arg, "entry")) {
40
            /* Ignored; consumed by the self-hosted compiler only. */
41
            i++;
42
        } else if (!strcmp(arg, "test") || !strcmp(arg, "dump")) {
43
            /* Ignored; consumed by the self-hosted compiler only. */
44
        } else {
45
            bail("unknown option `-%s`", arg);
46
        }
47
    }
48
    return 0;
49
}