OB.DAAC Logo
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 
188 
191 
196 typedef struct argpar_state {
198  struct argpar *argpar;
199 
201  unsigned argc;
203  char **argv;
204 
206  unsigned next;
207 
209  unsigned int flags;
210 
212  unsigned arg_num;
213 
215  unsigned quoted;
216 
218  void *input;
219 
222  void **child_inputs;
223 
225  void *hook;
226 
229  const char *parfile;
230 
233  char *name;
234 
237  const char *arg_name;
238 
240  const char *arg_alias;
241 
243  const char *arg_value;
244 
249 
251  double argv_as_dbl;
254 } argpar_state;
255 
258 extern const char *argpar_program_version;
259 
262 extern void (*argpar_program_version_hook) (FILE *stream, argpar_state *state);
263 
271 #define ARGPAR_KEY_ARG 0
272 
278 #define ARGPAR_KEY_ARGS -8
279 
282 #define ARGPAR_KEY_END -1
283 
287 #define ARGPAR_KEY_INIT -3
288 
291 #define ARGPAR_KEY_FINI -4
292 
295 #define ARGPAR_KEY_SUCCESS -5
296 
299 #define ARGPAR_KEY_ERROR -6
300 
303 #define ARGPAR_KEY_NO_ARGS -7
304 
307 #define ARGPAR_KEY_UNKNOWN -8
308 
310 #define ARGPAR_KEY_HELP_ARGS_DOC -1
311 
315 #define ARGPAR_KEY_HELP_PRE_DOC -2
316 
320 #define ARGPAR_KEY_HELP_POST_DOC -3
321 
323 #define ARGPAR_KEY_HELP_HEADER -4
324 
326 #define ARGPAR_KEY_HELP_OPTION_DOC -5
327 
330 #define ARGPAR_KEY_HELP_EXTRA -7 /* After all other documentation; text is NULL for this key. */
331 
334 #define ARGPAR_ERR_ABORT -1
335 
338 #define ARGPAR_ERR_USAGE -2
339 
341 #define ARGPAR_ERR_UNKNOWN -5
342 
345 #define ARGPAR_ERR_PARFILE -3
346 
349 #define ARGPAR_LIMIT_REACHED -4
350 
360 typedef int (*argpar_parser)(int key, char *argv, argpar_state *state);
361 
371 typedef char *(*help_filter)(int key, const char *text, void *input);
372 
377 typedef struct argpar_child {
379  struct argpar *argpar;
380 
383  unsigned flags;
384 
387  const char *header;
388 
390  int group;
391 } argpar_child;
392 
398 typedef struct argpar {
406  const char *args_doc;
409  const char *doc;
420 } argpar;
421 
428 int argpar_clean(argpar *p);
429 
442 int argpar_parse_file(argpar *p, const char *path, unsigned flags, void *input);
443 
459 int argpar_parse_args(argpar *p, unsigned argc, char *argv[], unsigned flags, unsigned *end_index, void *input);
460 
468 
476 
486 int argpar_help(argpar *argpar, FILE *stream, unsigned flags, char *name);
487 
496 
505 
516 int argpar_help_json(argpar *argpar, FILE *stream, unsigned flags, char *name);
517 
522 const char *argpar_version();
523 
532 char **argpar_split_str(char *str, const char *delim);
533 
543 char **argpar_split_trim(char *str, const char *delim);
544 
545 #ifndef NULL_INT
546 #define NULL_INT INT_MIN
547 #endif
548 
549 #ifndef EMPTY_INT
550 #define EMPTY_INT INT_MAX
551 #endif
552 
563 int *argpar_split_int(char *str, const char *delim);
564 
576 double *argpar_split_dbl(char *str, const char *delim);
577 
578 #ifdef __cplusplus
579 }
580 #endif
581 
582 #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:785
Master structure containing options, document strings, child parsers, and text filters....
Definition: argpar.h:398
double argv_as_dbl
value part of key=value pair, parsed as a double, if OPTION_DBL was used.
Definition: argpar.h:251
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:360
int argpar_clean(argpar *p)
Free any space consumed by argpar for parfiles.
Definition: argpar.c:649
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:400
unsigned parfile_buffer_count
Used internally to store the number of parfiles parsed. Initialize to zero.
Definition: argpar.h:417
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:209
unsigned next
The index in ARGV of the next arg that to be parsed. May be modified.
Definition: argpar.h:206
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:248
argpar_parser parser
Parser function handed all options and arguments, as well as various ARGPAR_KEY_ keys for events such...
Definition: argpar.h:403
FILE * argpar_ostream
Used as the stream for printing help and usage messages. Defaults to STDERR.
Definition: argpar.h:190
int group
The group after which to print the child's arguments.
Definition: argpar.h:390
unsigned flags
This should probably be the flags passed to the parser and help functions, but is currently unused....
Definition: argpar.h:383
char * name
The name used when printing messages. This is initialized to ARGV[0], or PROGRAM_INVOCATION_NAME if t...
Definition: argpar.h:233
char ** argv
Pointer to arguments passed to argpar_parse_args.
Definition: argpar.h:203
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:243
const char * args_doc
A string defining the desired arguments to the program, printed as part of the usage documentation.
Definition: argpar.h:406
Child parser for nesting argpars.
Definition: argpar.h:377
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:744
instr * input
const char * argpar_program_name
Used as a default for the program's invocation name if one is not found.
Definition: argpar.h:187
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:730
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:246
void ** child_inputs
Values to pass to child parsers, same length as the number of child parsers for the current parser.
Definition: argpar.h:222
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:679
void * input
Arbitrary pointer given by the user, generally to store parsed arguments.
Definition: argpar.h:218
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:387
unsigned argc
Total number of arguments being parsed by a call of argpar_parse_args.
Definition: argpar.h:201
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:809
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:240
const char * str
Definition: l1c_msi.cpp:35
void * hook
For the parser's use. Initialized to NULL.
Definition: argpar.h:225
const char * argpar_version()
Returns the source code version and the implemented API version.
Definition: argpar.c:740
flags
Definition: DDAlgorithm.h:22
unsigned arg_num
The number of key arguments processed so far.
Definition: argpar.h:212
State variable to be filled before each call to the parser callback.
Definition: argpar.h:196
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:413
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:769
unsigned quoted
The index of the first argument after –, if found.
Definition: argpar.h:215
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:229
int argv_as_dbl_err
Error state of double casting, if OPTION_DBL was used; 1 otherwise.
Definition: argpar.h:253
struct argpar * argpar
Underlying argpar struct.
Definition: argpar.h:379
const char * doc
A string printed before and/or after the options. To display text after options, use a vertical-tab (...
Definition: argpar.h:409
help_filter help_filter
A function passed all documentation to print.
Definition: argpar.h:415
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:20
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:198
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:237
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:419