aboutsummaryrefslogtreecommitdiff
path: root/src/command.c
blob: 2cb8c43afaef8896b1be49f7e025e8f647474543 (plain) (blame)
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
/* See LICENSE file for copyright & license details. */
#include <getopt.h>
#include <stdio.h>
#include <string.h>

#include "command.h"

#define CFG_API_KEY 20000
#define CFG_STEAM_ID 20001

void
print_cli_failure(const char *arg0)
{
    fprintf(stderr,
    "Error when parsing command-line arguments.\n"
    "An illegal value may have been passed.\n"
    "For more help, enter: '%s -h'\n\n",
    arg0);
}

void
print_help(const char *arg0)
{
    fprintf(stderr,
    "%s [options]\n"
        "\n"
        "Command-line options:\n"
        "\n"
        "    --api-key [value]\n"
        "        Set the Steam Web API key used to [value].\n"
        "        For more information, visit\n"
        "        https://www.steamcommunity.com/dev/\n"
        "\n"
        "    -h --help\n"
        "        Displays this help message and exits the program.\n"
        "\n"
        "    --steam-id [value]\n"
        "        Set the ID of the Steam profile for which the Montage will\n"
        "        be created to [value].\n"
        "\n"
        "    -v --version\n"
        "        Displays the currently running version and exits the\n"
        "        program.\n"
        "\n"
        "    -w, --width [value]\n"
        "        Set the width of the montage to [value] (in tiles).\n"
        "\n"
        "config.txt options\n"
        "!Note!: All config.txt options are overridden by command line\n"
        "options!:\n"
        "\n"
        "    api-key=[value]\n"
        "        Set the Steam Web API key used to [value].\n"
        "        For more information, visit\n"
        "        https://www.steamcommunity.com/dev/\n"
        "\n"
        "    steam-id=[value]\n"
        "        Set the ID of the Steam profile for which the Montage will\n"
        "        be created to [value].\n"
        "\n"
        "    width=[value]\n"
        "        Set the width of the montage to [value] (in tiles).\n"
        "\n"
        "\n", arg0);
}

int
parse_args(struct options *opts, int argc, char *argv[])
{
    int c;
    int option_index = 0;
    struct config *cfg = &opts->cfg;

    static struct option long_options[] = {
        {OPT_API_KEY,  required_argument, 0, CFG_API_KEY},
        {OPT_HELP,     no_argument,       0, 'h'},
        {OPT_STEAM_ID, required_argument, 0, CFG_STEAM_ID},
        {OPT_VERSION,  no_argument,       0, 'v'},
        {OPT_WIDTH,    required_argument, 0, 'w'},
        {0,            0,                 0, 0}
    };

    while ((c = getopt_long (argc, argv, "hvw:",
            long_options, &option_index)) != EOF) {
        switch(c) {
            case CFG_API_KEY:
                if (!parse_api_key(optarg, &cfg->api_key[0])) {
                    return 0;
                }
                break;

            case 'h':
                opts->help = 1;
                break;

            case CFG_STEAM_ID:
                if (!parse_steam_id(optarg, &cfg->steam_id[0])) {
                    return 0;
                }
                break;

            case 'v':
                opts->version = 1;
                break;

            case 'w':
                if (!parse_width(optarg, &cfg->width)) {
                    return 0;
                }
                break;

            default:
                return 0;
        };
    }

    return 1;
}
(git 2.53.0) at 2026-05-13 17:18:56 +0000