aboutsummaryrefslogtreecommitdiff
path: root/src/option.c
blob: 548b5b86593ecd59f301ff81839509aa3b6f34a7 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

#include "option.h"

static const char version[] = "qwe version 0.01";
static const char usage[] =
    "Usage: qwe [options...] <file>\n"
    "\n"
    "  -i              Hide info bar by default.\n"
    "  -h              Show help message and quit.\n"
    "  -v              Show the version number and quit.\n";

void print_usage()
{
    printf("%s\n", usage);
}

void print_version()
{
    printf("%s\n", version);
}

struct option _options;
const struct option *options = (const struct option *)&_options;

void parse_options(int argc, char **argv)
{
    // default options
    _options.fullscreen = false;
    _options.show_info = true;

    // override options from commandline parameters
    int opt;
    while ((opt = getopt(argc, argv, "hvi")) != -1) {
        switch (opt) {
        case '?':
            print_usage();
            exit(EXIT_FAILURE);
        case 'h':
            print_usage();
            exit(EXIT_SUCCESS);
        case 'v':
            print_version();
            exit(EXIT_SUCCESS);
        case 'i':
            _options.show_info = false;
        }
    }

    if (optind >= argc) {
        print_usage();
        exit(EXIT_FAILURE);
    }

    _options.file_name = argv[optind];
}