aboutsummaryrefslogtreecommitdiff
path: root/src/command.h
blob: 9aeb07e4d50dcf03b519ca50f8041fff82f27abc (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
#ifndef COMMAND_H
#define COMMAND_H

#include <stdio.h>
#include "common.h"

#define COMMAND(GROUP, COMMAND)\
int GROUP##_##COMMAND(int argc, char** argv)


#define COMMAND_HELP(GROUP, MSG) \
    COMMAND(GROUP, help) \
    { \
        fprintf(stderr, USAGE_STR MSG " <command>\n\nList of commands:\n"); \
        print_help(GROUP##_commands, ARRAY_LEN(GROUP##_commands)); \
        return 0; \
    }

#define COMMAND_GROUP(GROUP) \
    int GROUP(int argc, char** argv) \


#define COMMAND_GROUP_FUNC(GROUP) \
    COMMAND_GROUP(GROUP) \
    { \
        if (argc > 1) \
            for (int i = 0; i < ARRAY_LEN(GROUP##_commands); ++i) \
                if (!strcmp(GROUP##_commands[i].name, argv[1])) return GROUP##_commands[i].func(argc-1, argv+1); \
        return GROUP##_help(argc-1, argv+1); \
    }

#endif