-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclapc.h
166 lines (154 loc) · 5.66 KB
/
clapc.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
#if defined _WIN32 || defined __CYGWIN__
#ifdef BUILDING_CLAPC
#define CLAPC_PUBLIC __declspec(dllexport)
#else
#define CLAPC_PUBLIC __declspec(dllimport)
#endif
#else
#ifdef BUILDING_CLAPC
#define CLAPC_PUBLIC __attribute__((visibility("default")))
#else
#define CLAPC_PUBLIC
#endif
#endif
#include <assert.h>
#include <stdbool.h>
typedef enum {
CLAP_ARG_TYPE_BOOL = 0,
CLAP_ARG_TYPE_INT,
CLAP_ARG_TYPE_FLOAT,
CLAP_ARG_TYPE_STRING,
} CLAPC_PUBLIC e_clap_arg_type;
/**
* Represents a command-line argument. This is used to define the arguments that
* the user can provide to the program.
*/
typedef struct {
/**
* The name of the argument. This is the long name of the argument, e.g.
* "--json"
*/
const char* name;
/**
* The short name of the argument. This is the short name of the argument,
* e.g. "-j"
*/
const char short_name;
/**
* The type of the argument. This can be one of the following:
*
* - CLAP_ARG_TYPE_BOOL
* - CLAP_ARG_TYPE_INT
* - CLAP_ARG_TYPE_FLOAT
* - CLAP_ARG_TYPE_STRING
*/
const e_clap_arg_type type;
/**
* The value of the argument. This is a pointer to the value of the argument.
* The type of the value is determined by the {@link type} field.
*/
void* value;
/**
* The description of the argument. This is a human-readable description of
* the argument. This is used to generate the help message for the argument.
*/
const char* description;
/**
* Whether the argument is required. If this is true, the argument must be
* provided by the user. If this is false, the argument is optional.
*/
bool required;
} CLAPC_PUBLIC s_clap_arg;
/**
* Gets the value of a boolean argument.
*
* @param arg The argument to get the value of
* @return The value of the argument
*/
#define clap_arg_get_bool(arg) \
({ \
assert((arg)->type == CLAP_ARG_TYPE_BOOL); \
((arg)->value) ? *(bool*)(arg)->value : false; \
})
/**
* Gets the value of an integer argument.
* @param arg The argument to get the value of
* @return The value of the argument
*/
#define clap_arg_get_int(arg) \
({ \
assert((arg)->type == CLAP_ARG_TYPE_INT); \
assert((arg)->value != NULL); \
*(int*)(arg)->value; \
})
/**
* Gets the value of a float argument.
* @param arg The argument to get the value of
* @return The value of the argument
*/
#define clap_arg_get_float(arg) \
({ \
assert((arg)->type == CLAP_ARG_TYPE_FLOAT); \
assert((arg)->value != NULL); \
*(float*)(arg)->value; \
})
/**
* Gets the value of a string argument.
* @param arg The argument to get the value of
* @return The value of the argument
*/
#define clap_arg_get_string(arg) \
({ \
assert((arg)->type == CLAP_ARG_TYPE_STRING); \
(char*)(arg)->value; \
})
/**
* Parses the command-line arguments and populates the values of the arguments
* in the {@link args} array.
*
* @param args The array of arguments to parse. This array should be
* null-terminated
* @param argv_ptr A pointer to the command-line arguments. This pointer will be
* updated to point to the next argument after the parsed arguments.
*/
CLAPC_PUBLIC void clapc_parse(s_clap_arg* args[], char*** argv_ptr);
/**
* Parses the command-line arguments and populates the values of the arguments
* in the {@link args} array. This function is similar to {@link clapc_parse},
* but it returns a boolean indicating whether the parsing was successful, and
* an error message if the parsing failed.
*
* @param args The array of arguments to parse. This array should be
* null-terminated
* @param argv_ptr A pointer to the command-line arguments. This pointer will be
* updated to point to the next argument after the parsed arguments.
* @param error A pointer to a string that will be updated with an error message
* if the parsing fails. This string should be freed by the caller.
* @return true if the parsing was successful, false otherwise
*/
CLAPC_PUBLIC
bool clapc_parse_safe(s_clap_arg* args[], char*** argv_ptr, char** error);
/**
* Frees the memory allocated for an argument.
*
* @param arg The argument to free
*/
CLAPC_PUBLIC __attribute__((nonnull)) void clapc_arg_free(s_clap_arg* arg);
/**
* Frees the memory allocated for an array of arguments.
*
* @param args The array of arguments to free
*/
CLAPC_PUBLIC void clapc_args_free(s_clap_arg* args[]);
/**
* Prints the help message for the program. This function takes the program name
* and description, as well as an array of arguments, and prints a help message
* that describes the program and the arguments that it accepts.
*
* @param program_name The name of the program
* @param description A description of the program
* @param args An array of arguments that the program accepts
*/
CLAPC_PUBLIC void clapc_print_help(
const char* program_name, const char* description, s_clap_arg* args[]);