csnip  0.1
clopts.h
Go to the documentation of this file.
1 #ifndef CSNIP_CLOPTS_H
2 #define CSNIP_CLOPTS_H
3 
42 #include <stdio.h>
43 #include <stdbool.h>
44 
45 #include <csnip/err.h>
46 
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
52 struct csnip_clopts_s;
53 
56 
58 typedef struct csnip_clopts_s csnip_clopts;
59 
86 typedef int (*csnip_clopts_parser)(const csnip_clopts* opts,
88  const char* argval);
89 
92  char short_name;
93  const char* long_name;
94  const char* description;
96  bool takes_val;
100  void* usr;
102 };
103 
112  const char *description;
113 
114  /* The array of clopts_optinfo members */
115  int n_optinfo;
118 };
119 
120 /* Operations on csnip_clopts */
121 
128 
149  int n_optinfo,
150  const csnip_clopts_optinfo* optinfo);
151 
192  int argc,
193  char** argv,
194  int* ret_pos_args,
195  bool do_clear);
196 
205 void csnip_clopts_clear(csnip_clopts* opts);
206 
213  const csnip_clopts_optinfo* optinfo,
214  const char* argval);
217  const csnip_clopts_optinfo* optinfo,
218  const char* argval);
221  const csnip_clopts_optinfo* optinfo,
222  const char* argval);
225  const csnip_clopts_optinfo* optinfo,
226  const char* argval);
229  const csnip_clopts_optinfo* optinfo,
230  const char* argval);
233  const csnip_clopts_optinfo* optinfo,
234  const char* argval);
237  const csnip_clopts_optinfo* optinfo,
238  const char* argval);
248  const csnip_clopts_optinfo* optinfo,
249  const char* argval);
250 
253  const csnip_clopts_optinfo* optinfo,
254  const char* argval);
255 
258 #ifdef __cplusplus
259 }
260 #endif
261 
290 #define csnip_clopts_Addvar(opts, \
291  short_name, \
292  long_name, \
293  description, \
294  ptr_target, \
295  err) \
296  csnip_clopts__Addvar((opts), (short_name), (long_name), \
297  (description), (ptr_target), (err), \
298  csnip__parser, csnip__O, csnip__errval)
299 
301 #define csnip_clopts__Addvar(opts_, \
302  short_name_, \
303  long_name_, \
304  description_, \
305  ptr_target_, \
306  err, \
307  /* Internal variable names: */ \
308  parser_, \
309  O, \
310  errval) \
311  do { \
312  csnip_clopts_parser parser_ = csnip_clopts__Getparser(ptr_target_); \
313  csnip_clopts_optinfo O = { \
314  .short_name = short_name_, \
315  .long_name = long_name_, \
316  .description = description_, \
317  .takes_val = true, \
318  .parser = parser_, \
319  .usr = (void*)ptr_target_, \
320  }; \
321  int errval = csnip_clopts_add(opts_, 1, &O); \
322  if (errval) \
323  csnip_err_Raise(errval, err); \
324  } while(0)
356 #define csnip_clopts_Addflag(opts, \
357  short_name, \
358  long_name, \
359  description, \
360  ptr_target, \
361  err) \
362  csnip_clopts__Addflag((opts), (short_name), (long_name), \
363  (description), (ptr_target), (err), \
364  csnip__O, csnip__errval)
365 
367 #define csnip_clopts__Addflag(opts_, \
368  short_name_, \
369  long_name_, \
370  description_, \
371  ptr_target_, \
372  err, \
373  O, \
374  errval) \
375  do { \
376  csnip_clopts_optinfo O = { \
377  .short_name = short_name_, \
378  .long_name = long_name_, \
379  .description = description_, \
380  .takes_val = false, \
381  .parser = csnip_clopts_flagparser_bool, \
382  .usr = (void*)ptr_target_ \
383  }; \
384  int errval = csnip_clopts_add(opts_, 1, &O); \
385  if (errval) \
386  csnip_err_Raise(errval, err); \
387  } while (0)
391 #ifndef __cplusplus
392 
393 /* C11 version using _Generic */
395 #define csnip_clopts__Getparser(ptr_target_) \
396  _Generic(*ptr_target_, \
397  char: csnip_clopts_parser_uchar, \
398  unsigned char: csnip_clopts_parser_uchar, \
399  int: csnip_clopts_parser_uint, \
400  unsigned int: csnip_clopts_parser_uint, \
401  long: csnip_clopts_parser_ulong, \
402  unsigned long: csnip_clopts_parser_ulong, \
403  long long: csnip_clopts_parser_ullong, \
404  unsigned long long: csnip_clopts_parser_ullong, \
405  float: csnip_clopts_parser_float, \
406  double: csnip_clopts_parser_double, \
407  long double: csnip_clopts_parser_ldouble, \
408  char*: csnip_clopts_parser_pchar, \
409  char const*: csnip_clopts_parser_pchar)
411 #else
412 
413 /* C++ version using templates */
415 template<typename T> csnip_clopts_parser csnip_clopts__getparser(T* type);
416 #define csnip_clopts__tspec(type, suffix) \
417  template<> csnip_clopts_parser csnip_clopts__getparser<type>(type*) \
418  { \
419  return csnip_clopts_parser_ ## suffix; \
420  }
421 csnip_clopts__tspec(char, uchar)
422 csnip_clopts__tspec(unsigned char, uchar)
423 csnip_clopts__tspec(int, uint)
424 csnip_clopts__tspec(unsigned int, uint)
425 csnip_clopts__tspec(long, ulong)
426 csnip_clopts__tspec(unsigned long, ulong)
427 csnip_clopts__tspec(long long, ullong)
428 csnip_clopts__tspec(unsigned long long, ullong)
429 csnip_clopts__tspec(float, float)
430 csnip_clopts__tspec(double, double)
431 csnip_clopts__tspec(long double, ldouble)
432 csnip_clopts__tspec(char*, pchar)
433 csnip_clopts__tspec(char const*, pchar)
434 #undef csnip_clopts__tspec
435 #define csnip_clopts__Getparser(ptr_target_) csnip_clopts__getparser(ptr_target_)
438 #endif
439 
442 #endif /* CSNIP_CLOPTS_H */
443 
444 #if defined(CSNIP_SHORT_NAMES) && !defined(CSNIP_CLOPTS_HAVE_SHORT_NAMES)
445 #define clopts csnip_clopts
446 #define clopts_optinfo csnip_clopts_optinfo
447 #define clopts_parser csnip_clopts_parser
448 #define clopts_init csnip_clopts_init
449 #define clopts_clear csnip_clopts_clear
450 #define clopts_add_defaults csnip_clopts_add_defaults
451 #define clopts_add csnip_clopts_add
452 #define clopts_process csnip_clopts_process
453 #define clopts_clear csnip_clopts_clear
454 #define clopts_parser_uchar csnip_clopts_parser_uchar
455 #define clopts_parser_uint csnip_clopts_parser_uint
456 #define clopts_parser_ulong csnip_clopts_parser_ulong
457 #define clopts_parser_ullong csnip_clopts_parser_ullong
458 #define clopts_parser_float csnip_clopts_parser_float
459 #define clopts_parser_double csnip_clopts_parser_double
460 #define clopts_parser_ldouble csnip_clopts_parser_ldouble
461 #define clopts_parser_pchar csnip_clopts_parser_pchar
462 #define clopts_Addvar csnip_clopts_Addvar
463 #define clopts_Addflag csnip_clopts_Addflag
464 #define CSNIP_CLOPTS_HAVE_SHORT_NAMES
465 #endif /* CSNIP_SHORT_NAMES && !CSNIP_CLOPTS_HAVE_SHORT_NAMES */
Error handling.
int csnip_clopts_parser_ullong(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parse argument as long long int.
int csnip_clopts_parser_uchar(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parse argument as integer of type char.
Definition: clopts.c:229
int csnip_clopts_parser_pchar(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Assign a char* to point to the argument.
Definition: clopts.c:331
int csnip_clopts_flagparser_bool(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parser for flags, bool target.
Definition: clopts.c:342
int csnip_clopts_parser_ulong(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parse argument as a long int.
int csnip_clopts_parser_double(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parse argument as double.
int csnip_clopts_parser_float(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parse argument as float.
int csnip_clopts_parser_ldouble(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parse argument as long double.
int csnip_clopts_parser_uint(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Parse argument as an int.
int(* csnip_clopts_parser)(const csnip_clopts *opts, const csnip_clopts_optinfo *optinfo, const char *argval)
Argument parser function pointer type.
Definition: clopts.h:86
int csnip_clopts_add(csnip_clopts *opts, int n_optinfo, const csnip_clopts_optinfo *optinfo)
Add processable options.
Definition: clopts.c:49
void csnip_clopts_clear(csnip_clopts *opts)
Clear clopts assignments, freeing memory.
Definition: clopts.c:224
int csnip_clopts_add_defaults(csnip_clopts *opts)
Add handlers for default options.
Definition: clopts.c:37
int csnip_clopts_process(csnip_clopts *opts, int argc, char **argv, int *ret_pos_args, bool do_clear)
Process command line arguments.
Definition: clopts.c:212
Descriptor for a single command line option.
Definition: clopts.h:91
csnip_clopts_parser parser
The parser function callback.
Definition: clopts.h:98
char short_name
Short (single character) form.
Definition: clopts.h:92
void * usr
User data pointer for the callback.
Definition: clopts.h:100
const char * long_name
Long option form.
Definition: clopts.h:93
bool takes_val
True if option takes a value, false if it's a flag.
Definition: clopts.h:96
const char * description
Option description.
Definition: clopts.h:94
Set of descriptors for all command line options.
Definition: clopts.h:105
const char * description
General program description.
Definition: clopts.h:112
csnip_clopts_optinfo * optinfo
The options array.
Definition: clopts.h:117
int n_optinfo_cap
Capacity of the option array.
Definition: clopts.h:116
int n_optinfo
Number of command line options.
Definition: clopts.h:115