94 bool (*on_arg)(
void *data,
int argc,
char **argv,
int i);
163 bool cr8r_opt_parse_ull(
cr8r_opt *
self,
char *opt);
164 bool cr8r_opt_parse_ll(
cr8r_opt *
self,
char *opt);
165 bool cr8r_opt_parse_ul(
cr8r_opt *
self,
char *opt);
166 bool cr8r_opt_parse_l(
cr8r_opt *
self,
char *opt);
167 bool cr8r_opt_parse_u(
cr8r_opt *
self,
char *opt);
168 bool cr8r_opt_parse_i(
cr8r_opt *
self,
char *opt);
169 bool cr8r_opt_parse_us(
cr8r_opt *
self,
char *opt);
170 bool cr8r_opt_parse_s(
cr8r_opt *
self,
char *opt);
186 bool cr8r_opt_parse_f(
cr8r_opt *
self,
char *opt);
187 bool cr8r_opt_parse_d(
cr8r_opt *
self,
char *opt);
188 bool cr8r_opt_parse_ld(
cr8r_opt *
self,
char *opt);
189 bool cr8r_opt_parse_cstr(
cr8r_opt *
self,
char *opt);
190 bool cr8r_opt_parse_b(
cr8r_opt *
self,
char *opt);
191 bool cr8r_opt_parse_i128(
cr8r_opt *
self,
char *opt);
192 bool cr8r_opt_parse_u128(
cr8r_opt *
self,
char *opt);
214 CR8R_OPT_ARGMODE_NONE = 0,
215 CR8R_OPT_ARGMODE_REQUIRED = 1,
216 CR8R_OPT_ARGMODE_OPTIONAL = 2
221 CR8R_OPTS_FATAL_ERRS = 1ull,
222 CR8R_OPTS_ALLOW_STRAY_DASH = 2ull,
223 CR8R_OPTS_CB_ON_DD = 4ull
226 #define _CR8R_OPT_GENERIC_OPTFIELDS(_dest, _short_name, _long_name, _description) .dest = (_dest),\
227 .arg_mode = CR8R_OPT_ARGMODE_REQUIRED, \
228 .short_name = (_short_name), \
229 .long_name = (_long_name), \
230 .description = (_description), \
231 .on_opt = _Generic(*(_dest), \
232 unsigned long long: cr8r_opt_parse_ull, \
233 long long: cr8r_opt_parse_ll, \
234 unsigned long: cr8r_opt_parse_ul, \
235 long: cr8r_opt_parse_l, \
236 unsigned: cr8r_opt_parse_u, \
237 int: cr8r_opt_parse_i, \
238 unsigned short: cr8r_opt_parse_us, \
239 short: cr8r_opt_parse_s, \
240 unsigned char: cr8r_opt_parse_uc, \
241 signed char: cr8r_opt_parse_sc, \
242 char: cr8r_opt_parse_c, \
243 float: cr8r_opt_parse_f, \
244 double: cr8r_opt_parse_d, \
245 long double: cr8r_opt_parse_ld, \
246 char*: cr8r_opt_parse_cstr, \
247 _Bool: cr8r_opt_parse_b, \
248 __int128: cr8r_opt_parse_i128, \
249 unsigned __int128: cr8r_opt_parse_u128),
260 #define CR8R_OPT_GENERIC_OPTIONAL(_dest, _short_name, _long_name, _description) ((cr8r_opt){\
261 _CR8R_OPT_GENERIC_OPTFIELDS((_dest),(_short_name), (_long_name), (_description))\
262 .on_missing = cr8r_opt_missing_optional,})
273 #define CR8R_OPT_GENERIC_REQUIRED(_dest, _short_name, _long_name, _description) ((cr8r_opt){\
274 _CR8R_OPT_GENERIC_OPTFIELDS((_dest), (_short_name), (_long_name), (_description))})
283 #define CR8R_OPT_ENUM_CASE(_dest, _num, _short_name, _long_name, _description) ((cr8r_opt){\
284 .dest=(_dest), .arg_mode=CR8R_OPT_ARGMODE_NONE, .short_name=(_short_name), .long_name=(_long_name), .description=(_description), .on_opt=cr8r_opt_set_enum_##_num, .on_missing=cr8r_opt_missing_optional})
291 #define CR8R_OPT_HELP(_opts, _description) ((cr8r_opt){.dest=(_opts), .short_name="h", .long_name="help", .description=(_description), .on_opt=cr8r_opt_print_help, .on_missing=cr8r_opt_missing_optional})
294 #define CR8R_OPT_END() ((cr8r_opt){.short_name=NULL, .long_name=NULL})
cr8r_opt_argmode
Constants to set how required an option is.
char * cr8r_sprint_u128(char buf[static 40], unsigned __int128 i)
Print a signed 128 bit integer into a buffer log_10(2**128-1) is between 38 and 39 so the decimal rep...
bool cr8r_opt_print_help(cr8r_opt *self, char *opt)
"Default" implementation of on_opt that prints help and exits Should NOT be called or used directly e...
bool cr8r_opt_parse_uc(cr8r_opt *self, char *opt)
Note that parse_uc will read single digit numbers as characters This can be sidestepped by writing th...
bool cr8r_opt_set_enum_1(cr8r_opt *self, char *opt)
Implementation of on_opt that sets *(int*)self->dest to 1.
bool cr8r_opt_parse_c(cr8r_opt *self, char *opt)
Note that parse_c will read single digit numbers as characters This can be sidestepped by writing the...
bool cr8r_opt_missing_optional(cr8r_opt *self)
"Default" implementation of on_missing that returns true to indicate it is ok for the option to be mi...
char * cr8r_sprint_i128(char buf[static 41], __int128 i)
Print a signed 128 bit integer into a buffer log_10(2**127) is between 38 and 39 so the decimal repre...
bool cr8r_opt_set_enum_0(cr8r_opt *self, char *opt)
Implementation of on_opt that sets *(int*)self->dest to 0.
bool cr8r_opt_ignore_arg(void *data, int argc, char **argv, int i)
"Default" implementation of on_arg that returns true and ignores a positional argument See cr8r_opt_c...
bool cr8r_opt_parse_sc(cr8r_opt *self, char *opt)
Note that parse_sc will read single digit numbers as characters This can be sidestepped by writing th...
cr8r_opts_flags
Flag constants to modify option parsing behavior.
bool cr8r_opt_parse(cr8r_opt *opts, cr8r_opt_cfg *cfg, int argc, char **argv)
Parse command line options and arguments.
General configuration settings for parsing options.
void * data
Custom data for on_arg if needed.
uint64_t flags
Flags to control option parsing Can be zero or more of the following or'd together: CR8R_OPTS_FATAL_E...
Description of a command line option A list of these, terminated with a certain sentinel value,...
bool(* on_missing)(cr8r_opt *self)
Function to call after reading command line arguments if this option was not found.
bool(* on_opt)(cr8r_opt *self, char *opt)
Function to call when this option is encountered.
const char * description
Description of this option, for printing in the command help.
const char * short_name
The short name for this option.
void * dest
POINTER to variable/memory to store result in.
int arg_mode
0 indicates this option does not take an argument, 1 indicates this option has a required argument,...
bool found
Flag indicating whether or not the option was found.
const char * long_name
The long name for this option.