Argx 1.0.2-build
Simple argument parser made in C
 
Loading...
Searching...
No Matches
Argx.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdbool.h>
4
5#include "types.h"
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
11 /**
12 * @brief Create a new Argxc instance with a specified ID and command-line arguments.
13 *
14 * @param id Identifier for the parser instance.
15 * @param argc Number of command-line arguments.
16 * @param argv Array of command-line argument strings.
17 * @return Argxc* Pointer to the created Argxc instance.
18 */
19 Argxc* argxcCreate(const char *id, int argc, char *argv[]);
20
21 /**
22 * @brief Create a new Argxc instance with default values (empty ID and no arguments).
23 *
24 * @return Argxc* Pointer to the default Argxc instance.
25 */
27
28 /**
29 * @brief Destroy an Argxc instance and free all associated memory.
30 *
31 * @param argxc Pointer to the Argxc instance to destroy.
32 */
33 void argxcDestroy(Argxc *argxc);
34
35 /**
36 * @brief Add a new option to the Argxc instance.
37 *
38 * @param argxc Pointer to the Argxc instance.
39 * @param option The option to add.
40 */
41 void argxcAddOption(Argxc *argxc, ArgxcOptions option);
42
43 /**
44 * @brief Add a sub-option to a parent option.
45 *
46 * @param parent Pointer to the parent option.
47 * @param subOption Sub-option to add under the parent.
48 */
49 void argxcAddSubOption(ArgxcOptions *parent, ArgxcOptions subOption);
50
51 /**
52 * @brief Find the index of a parameter by ID.
53 *
54 * @param argxc Pointer to the Argxc instance.
55 * @param id The identifier of the parameter to find.
56 * @return int Index of the parameter, or -1 if not found.
57 */
58 int argxcFindParam(Argxc *argxc, const char *id);
59
60 /**
61 * @brief Retrieve a parameter by ID.
62 *
63 * @param argxc Pointer to the Argxc instance.
64 * @param id The identifier of the parameter to retrieve.
65 * @return ArgxcParam The parameter object.
66 */
67 ArgxcParam argxcGetParam(Argxc *argxc, const char *id);
68
69 /**
70 * @brief Check if a parameter with the given ID exists.
71 *
72 * @param argxc Pointer to the Argxc instance.
73 * @param id The identifier of the parameter.
74 * @return true if the parameter exists, false otherwise.
75 */
76 bool argxcParamExists(Argxc *argxc, const char *id);
77
78 /**
79 * @brief Retrieve a sub-parameter from a given parameter by ID.
80 *
81 * @param argxc Pointer to the Argxc instance.
82 * @param param Pointer to the parent parameter.
83 * @param id The identifier of the sub-parameter.
84 * @return true if the sub-parameter exists, false otherwise.
85 */
86 bool argxcGetSubParam(Argxc *argxc, const ArgxcParam *param, const char *id);
87
88 /**
89 * @brief Compare if the given argv matches a list of ArgxcOptions.
90 *
91 * @param options Array of ArgxcOptions.
92 * @param optionsCount Number of options.
93 * @param argv Array of argument strings.
94 * @param argvCount Number of argument strings.
95 * @return true if argv matches the options, false otherwise.
96 */
97 bool argxcCompareArgs(ArgxcOptions *options, size_t optionsCount, char **argv, size_t argvCount);
98
99 /**
100 * @brief Generate documentation for the defined options.
101 *
102 * @param argxc Pointer to the Argxc instance.
103 * @param style Documentation output style.
104 * @param title Title of the documentation.
105 * @param mainInfo Additional info to be displayed in the documentation.
106 * @return char* Documentation string (must be freed by caller).
107 */
108 char *argxcCreateDocs(Argxc *argxc, ArgxcStyle style, const char *title, const char *mainInfo);
109
110 /**
111 * @brief Get the raw command-line arguments passed to Argxc.
112 *
113 * @param argxc Pointer to the Argxc instance.
114 * @param count Output: number of arguments returned.
115 * @return char** Array of argument strings.
116 */
117 char **argxcGetMainArgs(Argxc *argxc, size_t *count);
118
119 /**
120 * @brief Get the number of command-line arguments passed.
121 *
122 * @param argxc Pointer to the Argxc instance.
123 * @return int Number of arguments.
124 */
125 int argxcGetArgc(Argxc *argxc);
126
127 /**
128 * @brief Get all top-level options defined for the Argxc instance.
129 *
130 * @param argxc Pointer to the Argxc instance.
131 * @param count Output: number of options returned.
132 * @return ArgxcOptions* Array of options.
133 */
134 ArgxcOptions *argxcGetOptions(Argxc *argxc, size_t *count);
135
136 /**
137 * @brief Get the identifier of the Argxc instance.
138 *
139 * @param argxc Pointer to the Argxc instance.
140 * @return const char* The ID string.
141 */
142 const char *argxcGetId(Argxc *argxc);
143
144 /**
145 * @brief Create a new option.
146 *
147 * @param id Option identifier.
148 * @param param Parameter name (e.g., --param).
149 * @param sparam Short form (e.g., -p).
150 * @param info Description of the option.
151 * @param hasSubParams Whether the option has sub-parameters.
152 * @param hasAnySubParams Whether the option accepts any sub-parameters.
153 * @return ArgxcOptions The created option.
154 */
155 ArgxcOptions argxcCreateOption(const char *id, const char *param, const char *sparam,
156 const char *info, bool hasSubParams, bool hasAnySubParams);
157
158 /**
159 * @brief Free resources associated with an ArgxcOptions struct.
160 *
161 * @param option Pointer to the option to free.
162 */
163 void argxcFreeOption(ArgxcOptions *option);
164
165 /**
166 * @brief Free resources associated with an ArgxcParam struct.
167 *
168 * @param param Pointer to the parameter to free.
169 */
170 void argxcFreeParam(ArgxcParam *param);
171
172 /**
173 * @brief Free a string array returned from Argxc (e.g., argv array).
174 *
175 * @param array The array to free.
176 * @param count Number of elements in the array.
177 */
178 void argxcFreeStringArray(char **array, size_t count);
179
180 /**
181 * @brief Create an ArgxcError object.
182 *
183 * @param type Type/category of the error.
184 * @param error Error message.
185 * @param help Help message for resolving the error.
186 * @param code Exit code or error code.
187 * @return ArgxcError The created error struct.
188 */
189 ArgxcError argxcCreateError(const char *type, const char *error, const char *help, int code);
190
191 /**
192 * @brief Free resources associated with an ArgxcError struct.
193 *
194 * @param error Pointer to the error to free.
195 */
196 void argxcFreeError(ArgxcError *error);
197
198#ifdef __cplusplus
199}
200#endif
bool argxcParamExists(Argxc *argxc, const char *id)
Check if a parameter with the given ID exists.
Definition Argx.c:209
Argxc * argxcCreate(const char *id, int argc, char *argv[])
Create a new Argxc instance with a specified ID and command-line arguments.
Definition Argx.c:64
bool argxcGetSubParam(Argxc *argxc, const ArgxcParam *param, const char *id)
Retrieve a sub-parameter from a given parameter by ID.
Definition Argx.c:366
int argxcGetArgc(Argxc *argxc)
Get the number of command-line arguments passed.
Definition Argx.c:588
const char * argxcGetId(Argxc *argxc)
Get the identifier of the Argxc instance.
Definition Argx.c:601
bool argxcCompareArgs(ArgxcOptions *options, size_t optionsCount, char **argv, size_t argvCount)
Compare if the given argv matches a list of ArgxcOptions.
Definition Argx.c:499
void argxcFreeStringArray(char **array, size_t count)
Free a string array returned from Argxc (e.g., argv array).
Definition Argx.c:661
int argxcFindParam(Argxc *argxc, const char *id)
Find the index of a parameter by ID.
Definition Argx.c:155
void argxcAddOption(Argxc *argxc, ArgxcOptions option)
Add a new option to the Argxc instance.
Definition Argx.c:124
ArgxcOptions argxcCreateOption(const char *id, const char *param, const char *sparam, const char *info, bool hasSubParams, bool hasAnySubParams)
Create a new option.
Definition Argx.c:607
char ** argxcGetMainArgs(Argxc *argxc, size_t *count)
Get the raw command-line arguments passed to Argxc.
Definition Argx.c:570
void argxcFreeParam(ArgxcParam *param)
Free resources associated with an ArgxcParam struct.
Definition Argx.c:653
void argxcAddSubOption(ArgxcOptions *parent, ArgxcOptions subOption)
Add a sub-option to a parent option.
Definition Argx.c:138
ArgxcParam argxcGetParam(Argxc *argxc, const char *id)
Retrieve a parameter by ID.
Definition Argx.c:214
Argxc * argxcCreateDefault(void)
Create a new Argxc instance with default values (empty ID and no arguments).
Definition Argx.c:91
void argxcDestroy(Argxc *argxc)
Destroy an Argxc instance and free all associated memory.
Definition Argx.c:113
char * argxcCreateDocs(Argxc *argxc, ArgxcStyle style, const char *title, const char *mainInfo)
Generate documentation for the defined options.
Definition Argx.c:375
void argxcFreeOption(ArgxcOptions *option)
Free resources associated with an ArgxcOptions struct.
Definition Argx.c:624
void argxcFreeError(ArgxcError *error)
Free resources associated with an ArgxcError struct.
Definition Argx.c:679
ArgxcOptions * argxcGetOptions(Argxc *argxc, size_t *count)
Get all top-level options defined for the Argxc instance.
Definition Argx.c:593
ArgxcError argxcCreateError(const char *type, const char *error, const char *help, int code)
Create an ArgxcError object.
Definition Argx.c:667
Definition Argx.c:15
ArgxcStyle
Definition types.h:10