ocssw
V2022
|
#include <limits.h>
#include <math.h>
#include <stdio.h>
Go to the source code of this file.
Classes | |
struct | argpar_option |
struct | argpar_state |
struct | argpar_child |
struct | argpar |
Typedefs | |
typedef int(* | argpar_parser) (int key, char *argv, argpar_state *state) |
typedef char *(* | help_filter) (int key, const char *text, void *input) |
Functions | |
int | argpar_clean (argpar *p) |
int | argpar_parse_file (argpar *p, const char *path, unsigned flags, void *input) |
int | argpar_parse_args (argpar *p, unsigned argc, char *argv[], unsigned flags, unsigned *end_index, void *input) |
int | argpar_usage (argpar_state *state) |
int | argpar_usage_default (argpar *argpar) |
int | argpar_help (argpar *argpar, FILE *stream, unsigned flags, char *name) |
int | argpar_usage_json (argpar_state *state) |
int | argpar_usage_default_json (argpar *argpar) |
int | argpar_help_json (argpar *argpar, FILE *stream, unsigned flags, char *name) |
const char * | argpar_version () |
char ** | argpar_split_str (char *str, const char *delim) |
char ** | argpar_split_trim (char *str, const char *delim) |
int * | argpar_split_int (char *str, const char *delim) |
double * | argpar_split_dbl (char *str, const char *delim) |
Variables | |
const char * | argpar_program_name |
FILE * | argpar_ostream |
const char * | argpar_program_version |
void(* | argpar_program_version_hook )(FILE *stream, argpar_state *state) |
Detailed Description
Library for reading command-line arguments in the form of key=value.
This library parses a pointer of null-terminated strings (usually argv) and may modify the array in the process. All pairs are stored for later access. Any argument that does not contain an equal sign is considered a "key argument" and is stored as-is in a non-associative array in the order in which they are found. An argument of "--" will cause argpar to treat all remaining arguments as key arguments.
Files containing newline separated key=value entries (called a par file) can also be parsed, triggered by a special key. Any argument matching parfile=file (configurable via the PARFILE_STR macro) will cause the parser to immediately open the file (relative to the current working directory) and parse through the entire file. Par files can also contain parfile=file entries, which will interrupt the current file parsing; these entries are relative to the current file being parsed instead of the current working directory, unless specified as absolute paths. All repeat keys will overwrite previous entries.
This library can also generate a usage statement based on the expected arguments.
This library should not be used in conjunction with argp as some macros may have been re-used in an attempt to make this as much like argp as possible.
argpar is not expected to free any strings passed in as arguments and they must remain in-memory and accessible until the argpar object is destroyed.
Definition in file argpar.h.
Macro Definition Documentation
◆ ARGPAR_ACCEPT_ANY
#define ARGPAR_ACCEPT_ANY 0x20 |
◆ ARGPAR_API_VERSION
#define ARGPAR_API_VERSION 2001000 |
◆ ARGPAR_API_VERSION_STR
#define ARGPAR_API_VERSION_STR "2.1.0" |
◆ ARGPAR_CAST_ARGS
#define ARGPAR_CAST_ARGS 0x1 |
◆ ARGPAR_ERR_ABORT
#define ARGPAR_ERR_ABORT -1 |
◆ ARGPAR_ERR_PARFILE
#define ARGPAR_ERR_PARFILE -3 |
◆ ARGPAR_ERR_UNKNOWN
#define ARGPAR_ERR_UNKNOWN -5 |
◆ ARGPAR_ERR_USAGE
#define ARGPAR_ERR_USAGE -2 |
◆ ARGPAR_KEY_ARG
#define ARGPAR_KEY_ARG 0 |
This is not an option at all, but rather a command line argument. If a parser receiving this key returns success, the fact is recorded, and the ARGP_KEY_NO_ARGS case won't be used. HOWEVER, if while processing the argument, a parser function decrements the NEXT field of the state it is passed, the option won't be considered processed; this is to allow you to actually modify the argument (perhaps into an option), and have it processed again.
◆ ARGPAR_KEY_ARGS
#define ARGPAR_KEY_ARGS -8 |
There are remaining arguments not parsed by any parser, which may be found starting at (STATE->argv + STATE->next). If success is returned, but STATE->next left untouched, it's assumed that all arguments were consume, otherwise, the parser should adjust STATE->next to reflect any arguments consumed.
◆ ARGPAR_KEY_END
#define ARGPAR_KEY_END -1 |
◆ ARGPAR_KEY_ERROR
#define ARGPAR_KEY_ERROR -6 |
◆ ARGPAR_KEY_FINI
#define ARGPAR_KEY_FINI -4 |
◆ ARGPAR_KEY_HELP_ARGS_DOC
#define ARGPAR_KEY_HELP_ARGS_DOC -1 |
◆ ARGPAR_KEY_HELP_EXTRA
◆ ARGPAR_KEY_HELP_HEADER
#define ARGPAR_KEY_HELP_HEADER -4 |
◆ ARGPAR_KEY_HELP_OPTION_DOC
#define ARGPAR_KEY_HELP_OPTION_DOC -5 |
◆ ARGPAR_KEY_HELP_POST_DOC
#define ARGPAR_KEY_HELP_POST_DOC -3 |
◆ ARGPAR_KEY_HELP_PRE_DOC
#define ARGPAR_KEY_HELP_PRE_DOC -2 |
◆ ARGPAR_KEY_INIT
#define ARGPAR_KEY_INIT -3 |
◆ ARGPAR_KEY_NO_ARGS
#define ARGPAR_KEY_NO_ARGS -7 |
◆ ARGPAR_KEY_SUCCESS
#define ARGPAR_KEY_SUCCESS -5 |
◆ ARGPAR_KEY_UNKNOWN
#define ARGPAR_KEY_UNKNOWN -8 |
◆ ARGPAR_LIMIT_REACHED
#define ARGPAR_LIMIT_REACHED -4 |
◆ ARGPAR_NO_EXIT
#define ARGPAR_NO_EXIT 0x10 |
◆ ARGPAR_NO_HELP
#define ARGPAR_NO_HELP 0x8 |
◆ ARGPAR_NO_KEYARGS
#define ARGPAR_NO_KEYARGS 0x4 |
◆ ARGPAR_SKIP_PARFILES
#define ARGPAR_SKIP_PARFILES 0x2 |
◆ ARGPAR_STORE_PARAMS
#define ARGPAR_STORE_PARAMS 0x40 |
◆ EMPTY_INT
◆ MAX_PARFILES
#define MAX_PARFILES 64 |
◆ NULL_INT
◆ OPTION_ALIAS
#define OPTION_ALIAS 0x200 |
◆ OPTION_ARG_OPTIONAL
#define OPTION_ARG_OPTIONAL 0x1 |
◆ OPTION_ATTR
#define OPTION_ATTR (OPTION_DOC | OPTION_HIDDEN | 0x800) |
◆ OPTION_CHILD
#define OPTION_CHILD 0x80 |
◆ OPTION_DBL
#define OPTION_DBL 0x10 |
Cast this option as a double. The value and any error will be reflected in the argpar_state struct during the parser callback.
◆ OPTION_DOC
#define OPTION_DOC 0x4 |
◆ OPTION_DOC_NO_BREAK
#define OPTION_DOC_NO_BREAK 0x100 |
◆ OPTION_ENUM
#define OPTION_ENUM (OPTION_DOC | 0x400) |
◆ OPTION_HIDDEN
#define OPTION_HIDDEN 0x2 |
◆ OPTION_INT
#define OPTION_INT 0x20 |
Cast this option as a long. The value and any error will be reflected in the argpar_state struct during the parser callback.
◆ OPTION_PARENT
#define OPTION_PARENT 0x40 |
◆ PARFILE_STR
#define PARFILE_STR "parfile" |
Typedef Documentation
◆ argpar_parser
typedef int(* argpar_parser) (int key, char *argv, argpar_state *state) |
Pointer to a callback function to call for each argument and option parsed.
- Parameters
-
[in] key argpar_option.key of recognized options or one of the ARGPAR_KEY_ macros. [in] argv value part of key=value parameter, or the entire argument in case of a key argument. [in] state Structure containing all the information needed to process an argument.
- Returns
- 0 to continue or any of the ARGPAR_ERR_ macros to fail.
◆ help_filter
Pointer to a callback function to call for each argument parsed.
- Parameters
-
[in] key argpar_option.key of recognized options or one of the ARGPAR_KEY_HELP_ macros. [in] text The text about to be printed. May be NULL for some ARGPAR_KEY_HELP_ keys. [in] input Currently unused, I think.
- Returns
- NULL to print nothing, the input text pointer to print it unchanged, or a malloc'd string to print which is free'd by the parser after use.
Function Documentation
◆ argpar_clean()
int argpar_clean | ( | argpar * | p | ) |
◆ argpar_help()
int argpar_help | ( | argpar * | argpar, |
FILE * | stream, | ||
unsigned | flags, | ||
char * | name | ||
) |
Print the default usage summary with all available sections.
- Parameters
-
[in] argpar argpar object from which to generate the usage summary. [in] stream Stream to which the help gets printed. [in] flags Flags controlling which sections to print. Currently unused. [in] name Program name to use.
- Returns
- 0 on success, non-0 on error.
Definition at line 512 of file argpar-help.c.
◆ argpar_help_json()
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 parsing.
- Parameters
-
[in] argpar argpar object from which to generate the usage summary. [in] stream Stream to which the help gets printed. [in] flags Flags controlling which sections to print. Currently unused. [in] name Program name to use.
- Returns
- 0 on success, non-0 on error.
Definition at line 420 of file argpar-json.c.
◆ argpar_parse_args()
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.
- Parameters
-
[in] p argpar object to use to parse file [in] argc Number of arguments to parse. [in] argv Array of key=value pairs and/or key arguments, starting at index 1. The first element is assumed to be the calling program's name. [in] flags Flags to modify the behavior of the parser. [out] end_index Set to the last index parsed. [in] input Pointer to pass to the configured parser callback.
- Returns
- 0 when the parsing is completed or the return value of the callback function if it bailed out early.
- See also
- ARGPAR_CAST_ARGS
◆ argpar_parse_file()
Parse a key=value store file.
- Parameters
-
[in] p argpar object to use to parse file [in] path Path of file, absolute or relative to the current directory. [in] flags Flags to modify the behavior of the parser. [in] input Pointer to pass to the configured parser callback.
- Returns
- 0 when the parsing is completed or the return value of the callback function if it bailed out early.
- See also
- ARGPAR_CAST_ARGS
◆ argpar_split_dbl()
double* argpar_split_dbl | ( | char * | str, |
const char * | delim | ||
) |
Splits a string on a delimiter, returning a NAN-terminated list of doubles.
- Parameters
-
[in] str String on which to split. This string is modified, replacing delimiters with '\0'. Return pointers are points to within this string. [in] delim Delimiters on which to split (each character is a separate delimiter).
- Returns
- NULL on failure, malloc'd array of doubles ending in NULL on success. Compare to INFINITY for empty strings. Conversion errors result in total failure (returning NULL). (N.B., the proper way to perform these comparisons are the macros isinf and isnan.)
◆ argpar_split_int()
int* argpar_split_int | ( | char * | str, |
const char * | delim | ||
) |
Splits a string on a delimiter, returning a NULL_INT-terminated list of ints.
- Parameters
-
[in] str String on which to split. This string is modified, replacing delimiters with '\0'. Return pointers are points to within this string. [in] delim Delimiters on which to split (each character is a separate delimiter).
- Returns
- NULL on failure, malloc'd array of ints ending in NULL_INT on success. Compare to EMPTY_INT for empty strings. Conversion errors result in total failure (returning NULL).
◆ argpar_split_str()
char** argpar_split_str | ( | char * | str, |
const char * | delim | ||
) |
Splits a string on a delimiter, returning a NULL-terminated list of strings.
- Parameters
-
[in] str String on which to split. This string is modified, replacing delimiters with '\0'. Return pointers are points to within this string. [in] delim Delimiters on which to split (each character is a separate delimiter).
- Returns
- NULL on failure, malloc'd array of strings ending in NULL on success.
◆ argpar_split_trim()
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 whitespace from each string.
- Parameters
-
[in] str String on which to split. This string is modified, replacing delimiters with '\0'. Return pointers are points to within this string. [in] delim Delimiters on which to split (each character is a separate delimiter).
- Returns
- NULL on failure, malloc'd array of strings ending in NULL on success.
◆ argpar_usage()
int argpar_usage | ( | argpar_state * | state | ) |
Print usage summary, called within a argpar_parser.
- Parameters
-
[in] state argpar_state from which to generate usage summary.
- Returns
- 0 on success, non-0 on error.
Definition at line 506 of file argpar-help.c.
◆ argpar_usage_default()
int argpar_usage_default | ( | argpar * | argpar | ) |
Print the default usage summary with all available sections.
- Parameters
-
[in] argpar argpar object from which to generate the usage summary.
- Returns
- 0 on success, non-0 on error.
Definition at line 509 of file argpar-help.c.
◆ argpar_usage_default_json()
int argpar_usage_default_json | ( | argpar * | argpar | ) |
Print the default usage summary with all available sections, in a format more suitable for automated parsing.
- Parameters
-
[in] argpar argpar object from which to generate the usage summary.
- Returns
- 0 on success, non-0 on error.
Definition at line 417 of file argpar-json.c.
◆ argpar_usage_json()
int argpar_usage_json | ( | argpar_state * | state | ) |
Print usage summary, called within a argpar_parser, in a format more suitable for automated parsing.
- Parameters
-
[in] state argpar_state from which to generate usage summary.
- Returns
- 0 on success, non-0 on error.
Definition at line 414 of file argpar-json.c.
◆ argpar_version()
const char* argpar_version | ( | ) |
Variable Documentation
◆ argpar_ostream
FILE * argpar_ostream |
◆ argpar_program_name
const char* argpar_program_name |
◆ argpar_program_version
const char* argpar_program_version |
Used as the program version string. If set, a version=1 option is automatically added. argpar_program_version_hook takes precedence.
Definition at line 98 of file leveltwo2csv-main.c.
◆ argpar_program_version_hook
void(* argpar_program_version_hook) (FILE *stream, argpar_state *state) |