aboutsummaryrefslogtreecommitdiff
path: root/include/BH/Args.h
blob: e94486918d64bac13d9c54d75670f18f58b45d4a (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
#ifndef BH_ARGS_H
#define BH_ARGS_H


#include "Common.h"


#define BH_ARGS_ARGUMENT   0
#define BH_ARGS_UNKNOWN   -1

#define BH_ARGS_VALUE      1
#define BH_ARGS_OPTIONAL   2


typedef struct BH_ArgsOption
{
    int key;
    const char *name;
    int flags;
    const char *description;
} BH_ArgsOption;


typedef int (*BH_ArgsCallback)(int key, char *arg, void *data);


/**
 * Parses command line \a options (given by \a argc and \a argv) and calls the
 * specified \a callback with \a data.
 *
 * There are few internal rules:
 * - If the "--" argument is specified, option parsing stops, and the rest of
 *   the arguments are processed as is.
 * - If the "-" argument is specified, it is processed as is.
 * - For long options, if a value is required, the string after the equal sign
 *   or the next argument will be used as the value ("--define=Value" or
 *   "--define Value").
 * - For short options, if a value is required, the string after the option
 *   letter or the next argument will be used as the value ("-DValue" or
 *   "-D Value").
 *
 * Options array should have last element filled with zeros.
 *
 * \param argc      Arguments count
 * \param argv      Arguments array
 * \param options   Options array pointer
 * \param callback  Callback function
 * \param data      Callback data
 *
 * \return On success, returns zero value.
 * \return On failure, returns error code.
 */
int BH_ArgsParse(int argc,
                 char **argv,
                 BH_ArgsOption *options,
                 BH_ArgsCallback callback,
                 void *data);


/**
 * Prints documentation for \a options (with specified \a padding).
 *
 * \param options  Options array pointer
 * \param padding  Padding
 */
void BH_ArgsHelp(BH_ArgsOption *options,
                 int padding);


#endif /* BH_ARGS_H */