opts.h
Go to the documentation of this file.
1 #pragma once
2 
11 
12 #include <stdbool.h>
13 #include <inttypes.h>
14 
19 typedef struct cr8r_opt cr8r_opt;
20 struct cr8r_opt {
26  void *dest;
33  bool found;
45  int arg_mode;
51  const char *short_name;
58  const char *long_name;
60  const char *description;
69  bool (*on_opt)(cr8r_opt *self, char *opt);
76  bool (*on_missing)(cr8r_opt *self);
77 };
78 
80 typedef struct{
82  void *data;
88  uint64_t flags;
94  bool (*on_arg)(void *data, int argc, char **argv, int i);
95 } cr8r_opt_cfg;
96 
100 
104 bool cr8r_opt_print_help(cr8r_opt *self, char *opt);
105 
107 bool cr8r_opt_set_enum_0(cr8r_opt *self, char *opt);
108 
110 bool cr8r_opt_set_enum_1(cr8r_opt *self, char *opt);
111 
114 bool cr8r_opt_ignore_arg(void *data, int argc, char **argv, int i);
115 
161 bool cr8r_opt_parse(cr8r_opt *opts, cr8r_opt_cfg *cfg, int argc, char **argv);
162 
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);
171 
175 bool cr8r_opt_parse_uc(cr8r_opt *self, char *opt);
176 
180 bool cr8r_opt_parse_sc(cr8r_opt *self, char *opt);
181 
185 bool cr8r_opt_parse_c(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);
193 
201 char *cr8r_sprint_i128(char buf[static 41], __int128 i);
202 
210 char *cr8r_sprint_u128(char buf[static 40], unsigned __int128 i);
211 
213 typedef enum{
214  CR8R_OPT_ARGMODE_NONE = 0,
215  CR8R_OPT_ARGMODE_REQUIRED = 1,
216  CR8R_OPT_ARGMODE_OPTIONAL = 2
218 
220 typedef enum{
221  CR8R_OPTS_FATAL_ERRS = 1ull,
222  CR8R_OPTS_ALLOW_STRAY_DASH = 2ull,
223  CR8R_OPTS_CB_ON_DD = 4ull
225 
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),
250 
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,})
263 
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))})
275 
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})
285 
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})
292 
294 #define CR8R_OPT_END() ((cr8r_opt){.short_name=NULL, .long_name=NULL})
295 
cr8r_opt_argmode
Constants to set how required an option is.
Definition: opts.h:213
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.
Definition: opts.h:220
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.
Definition: opts.h:80
void * data
Custom data for on_arg if needed.
Definition: opts.h:82
uint64_t flags
Flags to control option parsing Can be zero or more of the following or'd together: CR8R_OPTS_FATAL_E...
Definition: opts.h:88
Description of a command line option A list of these, terminated with a certain sentinel value,...
Definition: opts.h:20
bool(* on_missing)(cr8r_opt *self)
Function to call after reading command line arguments if this option was not found.
Definition: opts.h:76
bool(* on_opt)(cr8r_opt *self, char *opt)
Function to call when this option is encountered.
Definition: opts.h:69
const char * description
Description of this option, for printing in the command help.
Definition: opts.h:60
const char * short_name
The short name for this option.
Definition: opts.h:51
void * dest
POINTER to variable/memory to store result in.
Definition: opts.h:26
int arg_mode
0 indicates this option does not take an argument, 1 indicates this option has a required argument,...
Definition: opts.h:45
bool found
Flag indicating whether or not the option was found.
Definition: opts.h:33
const char * long_name
The long name for this option.
Definition: opts.h:58