NASA Logo
Ocean Color Science Software

ocssw V2022
argpar.h
Go to the documentation of this file.
1 
29 #ifndef __ARGPAR_H__
30 #define __ARGPAR_H__
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #include <limits.h>
37 #include <math.h>
38 #include <stdio.h>
39 
40 #ifndef ARGPAR_API_VERSION
41 
42 #define ARGPAR_API_VERSION 2001000
43 
44 #define ARGPAR_API_VERSION_STR "2.1.0"
45 #endif
46 
48 #ifndef MAX_PARFILES
49 #define MAX_PARFILES 64
50 #endif
51 
53 #ifndef PARFILE_STR
54 #define PARFILE_STR "parfile"
55 #endif
56 
58 extern const char *argpar_program_name;
59 
61 extern FILE *argpar_ostream;
62 
87 typedef struct argpar_option {
89  const char *name;
91  int key;
98  const char *arg;
100  int flags;
103  const char *doc;
111  int group;
112 } argpar_option;
113 
116 #define ARGPAR_CAST_ARGS 0x1
117 
120 #define ARGPAR_SKIP_PARFILES 0x2
121 
125 #define ARGPAR_NO_KEYARGS 0x4
126 
128 #define ARGPAR_NO_HELP 0x8
129 
132 #define ARGPAR_NO_EXIT 0x10
133 
136 #define ARGPAR_ACCEPT_ANY 0x20
137 
141 #define ARGPAR_STORE_PARAMS 0x40
142 
143 
146 #define OPTION_ARG_OPTIONAL 0x1
147 
149 #define OPTION_HIDDEN 0x2
150 
152 #define OPTION_DOC 0x4
153 
156 #define OPTION_DBL 0x10
157 
160 #define OPTION_INT 0x20
161 
164 #define OPTION_PARENT 0x40
165 
167 #define OPTION_CHILD 0x80
168 
171 #define OPTION_DOC_NO_BREAK 0x100
172 
175 #define OPTION_ALIAS 0x200
176 
179 #define OPTION_ENUM (OPTION_DOC | 0x400)
180 
183 #define OPTION_ATTR (OPTION_DOC | OPTION_HIDDEN | 0x800)
184 
189 typedef struct argpar_state {
191  struct argpar *argpar;
192 
194  unsigned argc;
196  char **argv;
197 
199  unsigned next;
200 
202  unsigned int flags;
203 
205  unsigned arg_num;
206 
208  unsigned quoted;
209 
211  void *input;
212 
215  void **child_inputs;
216 
218  void *hook;
219 
222  const char *parfile;
223 
226  char *name;
227 
230  const char *arg_name;
231 
233  const char *arg_alias;
234 
236  const char *arg_value;
237 
242 
244  double argv_as_dbl;
247 } argpar_state;
248 
251 extern const char *argpar_program_version;
252 
255 extern void (*argpar_program_version_hook) (FILE *stream, argpar_state *state);
256 
264 #define ARGPAR_KEY_ARG 0
265 
271 #define ARGPAR_KEY_ARGS -8
272 
275 #define ARGPAR_KEY_END -1
276 
280 #define ARGPAR_KEY_INIT -3
281 
284 #define ARGPAR_KEY_FINI -4
285 
288 #define ARGPAR_KEY_SUCCESS -5
289 
292 #define ARGPAR_KEY_ERROR -6
293 
296 #define ARGPAR_KEY_NO_ARGS -7
297 
300 #define ARGPAR_KEY_UNKNOWN -8
301 
303 #define ARGPAR_KEY_HELP_ARGS_DOC -1
304 
308 #define ARGPAR_KEY_HELP_PRE_DOC -2
309 
313 #define ARGPAR_KEY_HELP_POST_DOC -3
314 
316 #define ARGPAR_KEY_HELP_HEADER -4
317 
319 #define ARGPAR_KEY_HELP_OPTION_DOC -5
320 
323 #define ARGPAR_KEY_HELP_EXTRA -7 /* After all other documentation; text is NULL for this key. */
324 
327 #define ARGPAR_ERR_ABORT -1
328 
331 #define ARGPAR_ERR_USAGE -2
332 
334 #define ARGPAR_ERR_UNKNOWN -5
335 
338 #define ARGPAR_ERR_PARFILE -3
339 
342 #define ARGPAR_LIMIT_REACHED -4
343 
353 typedef int (*argpar_parser)(int key, char *argv, argpar_state *state);
354 
364 typedef char *(*help_filter)(int key, const char *text, void *input);
365 
370 typedef struct argpar_child {
372  struct argpar *argpar;
373 
376  unsigned flags;
377 
380  const char *header;
381 
383  int group;
384 } argpar_child;
385 
391 typedef struct argpar {
399  const char *args_doc;
402  const char *doc;
413 } argpar;
414 
421 int argpar_clean(argpar *p);
422 
435 int argpar_parse_file(argpar *p, const char *path, unsigned flags, void *input);
436 
452 int argpar_parse_args(argpar *p, unsigned argc, char *argv[], unsigned flags, unsigned *end_index, void *input);
453 
461 
469 
479 int argpar_help(argpar *argpar, FILE *stream, unsigned flags, char *name);
480 
489 
498 
509 int argpar_help_json(argpar *argpar, FILE *stream, unsigned flags, char *name);
510 
515 const char *argpar_version();
516 
525 char **argpar_split_str(char *str, const char *delim);
526 
536 char **argpar_split_trim(char *str, const char *delim);
537 
538 #ifndef NULL_INT
539 #define NULL_INT INT_MIN
540 #endif
541 
542 #ifndef EMPTY_INT
543 #define EMPTY_INT INT_MAX
544 #endif
545 
556 int *argpar_split_int(char *str, const char *delim);
557 
569 double *argpar_split_dbl(char *str, const char *delim);
570 
571 #ifdef __cplusplus
572 }
573 #endif
574 
575 #endif // __ARGPAR_H__
int * argpar_split_int(char *str, const char *delim)
Splits a string on a delimiter, returning a NULL_INT-terminated list of ints.
Definition: argpar.c:792
Master structure containing options, document strings, child parsers, and text filters....
Definition: argpar.h:391
double argv_as_dbl
value part of key=value pair, parsed as a double, if OPTION_DBL was used.
Definition: argpar.h:244
int(* argpar_parser)(int key, char *argv, argpar_state *state)
Pointer to a callback function to call for each argument and option parsed.
Definition: argpar.h:353
int argpar_clean(argpar *p)
Free any space consumed by argpar for parfiles.
Definition: argpar.c:656
int argpar_usage(argpar_state *state)
Print usage summary, called within a argpar_parser.
Definition: argpar-help.c:506
const argpar_option * options
Array of option structures, terminated by an empty entry (IE: {0}).
Definition: argpar.h:393
unsigned parfile_buffer_count
Used internally to store the number of parfiles parsed. Initialize to zero.
Definition: argpar.h:410
const char * argpar_program_version
Used as the program version string. If set, a version=1 option is automatically added....
int argpar_usage_default_json(argpar *argpar)
Print the default usage summary with all available sections, in a format more suitable for automated ...
Definition: argpar-json.c:417
unsigned int flags
Flags passed to parser functions to modify the behavior.
Definition: argpar.h:202
unsigned next
The index in ARGV of the next arg that to be parsed. May be modified.
Definition: argpar.h:199
int argpar_usage_json(argpar_state *state)
Print usage summary, called within a argpar_parser, in a format more suitable for automated parsing.
Definition: argpar-json.c:414
int argv_as_int_err
Error state of integer casting, if OPTION_INT was used; 1 otherwise.
Definition: argpar.h:241
argpar_parser parser
Parser function handed all options and arguments, as well as various ARGPAR_KEY_ keys for events such...
Definition: argpar.h:396
int group
The group after which to print the child's arguments.
Definition: argpar.h:383
unsigned flags
This should probably be the flags passed to the parser and help functions, but is currently unused....
Definition: argpar.h:376
char * name
The name used when printing messages. This is initialized to ARGV[0], or PROGRAM_INVOCATION_NAME if t...
Definition: argpar.h:226
char ** argv
Pointer to arguments passed to argpar_parse_args.
Definition: argpar.h:196
const char * doc
A (generally short) description of an option. In case of a documentation option, this may be as lengt...
Definition: argpar.h:103
const char * arg_value
value part of key=value pair.
Definition: argpar.h:236
const char * args_doc
A string defining the desired arguments to the program, printed as part of the usage documentation.
Definition: argpar.h:399
Child parser for nesting argpars.
Definition: argpar.h:370
char ** argpar_split_str(char *str, const char *delim)
Splits a string on a delimiter, returning a NULL-terminated list of strings.
Definition: argpar.c:751
instr * input
const char * argpar_program_name
Used as a default for the program's invocation name if one is not found.
int state(double tjdTDB, JPLIntUtilType *util, double posvel[13][6], double *pnut)
int argpar_usage_default(argpar *argpar)
Print the default usage summary with all available sections.
Definition: argpar-help.c:509
int argpar_parse_file(argpar *p, const char *path, unsigned flags, void *input)
Parse a key=value store file.
Definition: argpar.c:737
int flags
Modify the behavior of the argument by OR'ing one or more OPTION_ macros.
Definition: argpar.h:100
string path
Definition: color_dtdb.py:221
long argv_as_int
value part of key=value pair, parsed as a long, if OPTION_INT was used.
Definition: argpar.h:239
void ** child_inputs
Values to pass to child parsers, same length as the number of child parsers for the current parser.
Definition: argpar.h:215
int argpar_parse_args(argpar *p, unsigned argc, char *argv[], unsigned flags, unsigned *end_index, void *input)
Parse an array of key=value pairs and/or key arguments.
Definition: argpar.c:686
void * input
Arbitrary pointer given by the user, generally to store parsed arguments.
Definition: argpar.h:211
const char * header
If non-NULL, don't merge this child's options with the the parent's. If header length is non-zero,...
Definition: argpar.h:380
unsigned argc
Total number of arguments being parsed by a call of argpar_parse_args.
Definition: argpar.h:194
double * argpar_split_dbl(char *str, const char *delim)
Splits a string on a delimiter, returning a NAN-terminated list of doubles.
Definition: argpar.c:816
int argpar_help(argpar *argpar, FILE *stream, unsigned flags, char *name)
Print the default usage summary with all available sections.
Definition: argpar-help.c:512
const char * arg_alias
key part of key=value pair.
Definition: argpar.h:233
void * hook
For the parser's use. Initialized to NULL.
Definition: argpar.h:218
const char * argpar_version()
Returns the source code version and the implemented API version.
Definition: argpar.c:747
flags
Definition: DDAlgorithm.h:22
unsigned arg_num
The number of key arguments processed so far.
Definition: argpar.h:205
State variable to be filled before each call to the parser callback.
Definition: argpar.h:189
int argpar_help_json(argpar *argpar, FILE *stream, unsigned flags, char *name)
Print the default usage summary with all available sections, in a format more suitable for automated ...
Definition: argpar-json.c:420
const argpar_child * children
Array of argpar_child structures containing parsers to nest within this one. All options and argument...
Definition: argpar.h:406
char ** argpar_split_trim(char *str, const char *delim)
Splits a string on a delimiter, returning a NULL-terminated list of strings, trimming left and right ...
Definition: argpar.c:776
unsigned quoted
The index of the first argument after –, if found.
Definition: argpar.h:208
const char * name
The key to search for.
Definition: argpar.h:89
const char * parfile
The name of the parfile being parsed, or NULL if the argument comes from the command line.
Definition: argpar.h:222
int argv_as_dbl_err
Error state of double casting, if OPTION_DBL was used; 1 otherwise.
Definition: argpar.h:246
struct argpar * argpar
Underlying argpar struct.
Definition: argpar.h:372
const char * doc
A string printed before and/or after the options. To display text after options, use a vertical-tab (...
Definition: argpar.h:402
help_filter help_filter
A function passed all documentation to print.
Definition: argpar.h:408
void(* argpar_program_version_hook)(FILE *stream, argpar_state *state)
Called to print the version string. If set, a version=1 option is automatically added.
Definition: argpar.c:27
Definition: aerosol.c:136
const char * arg
What to display as the right side of the argument in the usage statement.
Definition: argpar.h:98
Stores the configuration for a par argument.
Definition: argpar.h:87
float p[MODELMAX]
Definition: atrem_corl1.h:131
struct argpar * argpar
Pointer to the parent argpar object.
Definition: argpar.h:191
FILE * argpar_ostream
Used as the stream for printing help and usage messages. Defaults to STDERR.
Definition: argpar.c:24
int key
The value to pass to the parser callback. Must be a positive and non-zero.
Definition: argpar.h:91
const char * arg_name
key part of key=value pair. When an alias is used, this is the name of the actual option,...
Definition: argpar.h:230
int group
The group associated with this option. If 0 and a documentation option, it is auto-incremented.
Definition: argpar.h:111
char ** parfile_buffer
Used internally to store the parfiles. Initialize to NULL.
Definition: argpar.h:412