![]() |
librnr
1.14.5
RoadNarrows Robotics Common Library 1
|
Standard command-line options built-in options parsing and validation definitions. More...
#include <stdio.h>#include <errno.h>#include <getopt.h>#include <stdlib.h>#include <stdarg.h>#include <libgen.h>#include <limits.h>#include <string.h>#include "rnr/rnrconfig.h"#include "rnr/pkg.h"#include "rnr/new.h"#include "rnr/assoc.h"#include "rnr/log.h"#include "rnr/opts.h"Go to the source code of this file.
Classes | |
| struct | OptsCtl_T |
Macros | |
| #define | OPTS_LONGOPT_LOG "log" |
| log long option name | |
| #define | OPTS_LONGOPT_LOGFILE "logfile" |
| log file long option name | |
| #define | OPTS_LONGOPT_LOGNOCOLOR "log-no-color" |
| log with no color option | |
| #define | OPTS_LONGOPT_LOGNOTIME "log-no-timestamp" |
| log no timestamps option | |
| #define | OPTS_RESET_ARGCV(optind, argc, argv) { argc -= optind; argv += optind; } |
| Reset argc,argv to start of non-option command arguments. More... | |
| #define | MAX_FMT_BUF ((size_t)1024) |
| Format buffer size. | |
| #define | HELP_OPT_OVER 30 |
| opt help desc 1st line indent More... | |
| #define | HELP_OPT_OVER_2 (HELP_OPT_OVER+2) |
| opt help desc 2nd line+ indent | |
| #define | HELP_MAX_COL 79 |
| max help column | |
Functions | |
| int | HelpIndent (int over) |
| Indents (spaces) over, returning number of added indentation. More... | |
| int | HelpPutNL (int over) |
| Puts help newline, returning new output column position. More... | |
| static int | HelpPutBuf (int col, int colstart, char buf[], size_t *pbuflen) |
| Puts help buffer, returning new output column position. More... | |
| static int | HelpPutStr (int col, int colstart, const char *s) |
| Puts help string, returning new output column position. More... | |
| static int | HelpPutC (int col, int colstart, int c) |
| Puts help character, returning new output column position. More... | |
| static int | HelpGetC (int col, int colstart, char buf[], size_t bufsiz, size_t *pbuflen, int c) |
| Gets help character into local buffer, returning new/same output column position. More... | |
| static int | OptsPrintf (int col, int colstart, const char *sFmtDesc, const char *sPgmName, OptsInfo_T *pOptsInfo) |
| Prints formatted description. More... | |
| static void | OptsPrintOpt (const char *argv0, OptsInfo_T *pOptsInfo) |
| Prints option syntax and description in standard 'usage' format. More... | |
| static void | OptsHelp (const char *argv0, OptsInfo_T *pOptsInfo, OptsPgmInfo_T *pPgmInfo, bool_t bHasLogging) |
| Prints command-line help. More... | |
| static void | OptsVersion (const char *argv0, const PkgInfo_T *pPkgInfo) |
| Prints command-line program version string(s). More... | |
| static void | OptsLogSetLevel (const char *argv0, const char *sOptName, char *optarg) |
| Set program logging level. More... | |
| static void | OptsLogSetFile (const char *argv0, const char *sOptName, char *optarg) |
| Set program logging output file. More... | |
| static void | OptsLogDisableColor (const char *argv0, const char *sOptName, char *optarg) |
| Disable program logging in color. More... | |
| static void | OptsLogDisableTimestamp (const char *argv0, const char *sOptName, char *optarg) |
| Disable program logging with timestamps. More... | |
| static OptsCtl_T * | OptsNew (OptsInfo_T *pOptsInfo, bool_t bHasLogging) |
| Allocate and build short and long options getopt_long() parameters from the provided option info. More... | |
| static void | OptsDelete (OptsCtl_T *pOptsCtl) |
| Delete an allocated options control structure. More... | |
| char ** | OptsGet (const char *argv0, const PkgInfo_T *pPkgInfo, OptsPgmInfo_T *pPgmInfo, OptsInfo_T *pOptsInfo, bool_t bHasLogging, int *pargc, char *argv[]) |
| Gets, validates, and sets all command line options. More... | |
| int | OptsCvtArgStr (const char *argv0, const char *sOptName, char *optarg, void *pOptVal) |
| Convert options string argument to string. More... | |
| int | OptsCvtArgBool (const char *argv0, const char *sOptName, char *optarg, void *pOptVal) |
| Convert options boolean argument to bool_t. More... | |
| int | OptsCvtArgInt (const char *argv0, const char *sOptName, char *optarg, void *pOptVal) |
| Convert options integer argument to integer. More... | |
| int | OptsCvtArgFloat (const char *argv0, const char *sOptName, char *optarg, void *pOptVal) |
| Convert options float argument to double. More... | |
| int | OptsCvtArgLogLevel (const char *argv0, const char *sOptName, char *optarg, void *pOptVal) |
| Convert options string argument to log threshold level. More... | |
| char * | OptsFmtStr (char *buf, size_t buflen, void *pOptVal) |
| String option value string formatter. More... | |
| char * | OptsFmtInt (char *buf, size_t buflen, void *pOptVal) |
| Integer option value string formatter. More... | |
| char * | OptsFmtFloat (char *buf, size_t buflen, void *pOptVal) |
| Float option value string formatter. More... | |
| char * | OptsFmtBool (char *buf, size_t buflen, void *pOptVal) |
| Boolean option value string formatter. More... | |
| char * | OptsFmtChar (char *buf, size_t buflen, void *pOptVal) |
| Character option value string formatter. More... | |
| char * | OptsFmtLogLevel (char *buf, size_t buflen, void *pOptVal) |
| Log Level option value string formatter. More... | |
| void | OptsInvalid (const char *argv0, const char *sFmt,...) |
| Invalid option or option argument print and exit. More... | |
Variables | |
| static Nvp_T | OptsLogLevelTbl [] |
| static int | OptsLogLevel = LOG_LEVEL_DFT |
| working log level value More... | |
| static char * | OptsLogFile = LOG_FILENAME_DFT |
| working log file value | |
| static bool_t | OptsLogNoColor = false |
| working log no color value | |
| static bool_t | OptsLogNoTimestamp = false |
| working log no timestamp value | |
| static OptsInfo_T | OptsBuiltIn [] |
| Built-In Options. | |
Standard command-line options built-in options parsing and validation definitions.
Definition in file opts.c.
| #define HELP_OPT_OVER 30 |
opt help desc 1st line indent
PrettyPrint formats
Definition at line 224 of file opts.c.
Referenced by OptsPrintOpt().
| #define OPTS_RESET_ARGCV | ( | optind, | |
| argc, | |||
| argv | |||
| ) | { argc -= optind; argv += optind; } |
|
static |
Gets help character into local buffer, returning new/same output column position.
| col | Current column position. |
| colstart | Column to start. |
| buf | [out] Buffer to put. |
| bufsiz | Buffer maximum size. |
| pbuflen | [out] Buffer insert point. |
| c | Help character to get (put into buffer). |
Definition at line 324 of file opts.c.
References HelpPutBuf().
Referenced by OptsPrintf().
|
inline |
Indents (spaces) over, returning number of added indentation.
| over | Number of spaces. |
Definition at line 232 of file opts.c.
Referenced by HelpPutNL(), and OptsPrintOpt().
|
static |
Puts help buffer, returning new output column position.
| col | Current column position. |
| colstart | Column to start. |
| buf | Buffer to output. |
| pbuflen | [out] Buffer length. |
Definition at line 250 of file opts.c.
References HELP_MAX_COL, and HelpPutNL().
Referenced by HelpGetC(), and OptsPrintf().
|
static |
Puts help character, returning new output column position.
| col | Current column position. |
| colstart | Column to start. |
| c | Help character. |
Definition at line 301 of file opts.c.
References HELP_MAX_COL, and HelpPutNL().
Referenced by OptsPrintf().
|
inline |
Puts help newline, returning new output column position.
| over | Number of spaces. |
Definition at line 238 of file opts.c.
References HelpIndent().
Referenced by HelpPutBuf(), HelpPutC(), HelpPutStr(), and OptsPrintf().
|
static |
Puts help string, returning new output column position.
| col | Current column position. |
| colstart | Column to start. |
| s | Help string. |
Definition at line 275 of file opts.c.
References HELP_MAX_COL, HelpPutNL(), and NULL.
Referenced by OptsPrintf().
| int OptsCvtArgBool | ( | const char * | argv0, |
| const char * | sOptName, | ||
| char * | optarg, | ||
| void * | pOptVal | ||
| ) |
Convert options boolean argument to bool_t.
1 or "true" converts to true.
0 or "false" converts to false.
| argv0 | Command name. | |
| sOptName | Option name. | |
| optarg | Parsed option argument to convert (optional). | |
| [out] | pOptVal | Pointer to converted option value. |
true | OPTSBADARG() |
Definition at line 1018 of file opts.c.
References OK, and OPTSBADARG.
| int OptsCvtArgFloat | ( | const char * | argv0, |
| const char * | sOptName, | ||
| char * | optarg, | ||
| void * | pOptVal | ||
| ) |
Convert options float argument to double.
Accepted format: optionally signed floating-point number.
| argv0 | Command name. | |
| sOptName | Option name. | |
| optarg | Parsed option argument to convert (optional). | |
| [out] | pOptVal | Pointer to converted option value. |
1.0 | OPTSBADARG() |
Definition at line 1109 of file opts.c.
References OK, and OPTSBADARG.
| int OptsCvtArgInt | ( | const char * | argv0, |
| const char * | sOptName, | ||
| char * | optarg, | ||
| void * | pOptVal | ||
| ) |
Convert options integer argument to integer.
Accepted formats: decimal hexidecimal(0x<hexdigits>) octal(0<octaldigits>).
| argv0 | Command name. | |
| sOptName | Option name. | |
| optarg | Parsed option argument to convert (optional). | |
| [out] | pOptVal | Pointer to converted option value. |
1 | OPTSBADARG() |
Definition at line 1065 of file opts.c.
References OK, and OPTSBADARG.
Referenced by OptsCvtArgLogLevel().
| int OptsCvtArgLogLevel | ( | const char * | argv0, |
| const char * | sOptName, | ||
| char * | optarg, | ||
| void * | pOptVal | ||
| ) |
Convert options string argument to log threshold level.
Format: "off", "error", "diag1", "diag2", "diag3", "diag4", "diag5", or >= 0
| argv0 | Command name. | |
| sOptName | Option name. | |
| optarg | Parsed option argument to convert (optional). | |
| [out] | pOptVal | Pointer to converted option value. |
LOG_LEVEL_DFT | OPTSBADARG() |
Definition at line 1151 of file opts.c.
References arraysize, LOG_LEVEL_DFT, LOG_LEVEL_OFF, NvpName2Val(), OK, and OptsCvtArgInt().
Referenced by OptsLogSetLevel().
| int OptsCvtArgStr | ( | const char * | argv0, |
| const char * | sOptName, | ||
| char * | optarg, | ||
| void * | pOptVal | ||
| ) |
Convert options string argument to string.
| argv0 | Command name. | |
| sOptName | Option name. | |
| optarg | Parsed option argument to convert (optional). | |
| [out] | pOptVal | Pointer to converted option value. |
"" (zero-length string)Definition at line 987 of file opts.c.
References new_strdup(), and OK.
Referenced by OptsGet(), and OptsLogSetFile().
|
static |
Delete an allocated options control structure.
| pOptsCtl | Options control structure. |
Definition at line 832 of file opts.c.
References OptsCtl_T::m_pLongOpts, OptsCtl_T::m_sShortOpts, and NULL.
Referenced by OptsGet().
| char* OptsFmtBool | ( | char * | buf, |
| size_t | buflen, | ||
| void * | pOptVal | ||
| ) |
Boolean option value string formatter.
| [out] | buf | Output buffer. |
| buflen | Buffer length. | |
| pOptVal | Option value. |
| char* OptsFmtChar | ( | char * | buf, |
| size_t | buflen, | ||
| void * | pOptVal | ||
| ) |
| char* OptsFmtFloat | ( | char * | buf, |
| size_t | buflen, | ||
| void * | pOptVal | ||
| ) |
| char* OptsFmtInt | ( | char * | buf, |
| size_t | buflen, | ||
| void * | pOptVal | ||
| ) |
Integer option value string formatter.
| [out] | buf | Output buffer. |
| buflen | Buffer length. | |
| pOptVal | Option value. |
Definition at line 1215 of file opts.c.
Referenced by OptsFmtLogLevel().
| char* OptsFmtLogLevel | ( | char * | buf, |
| size_t | buflen, | ||
| void * | pOptVal | ||
| ) |
Log Level option value string formatter.
| [out] | buf | Output buffer. |
| buflen | Buffer length. | |
| pOptVal | Option value. |
Definition at line 1289 of file opts.c.
References LOG_LEVEL_OFF, OptsFmtInt(), and OptsFmtStr().
| char* OptsFmtStr | ( | char * | buf, |
| size_t | buflen, | ||
| void * | pOptVal | ||
| ) |
String option value string formatter.
| [out] | buf | Output buffer. |
| buflen | Buffer length. | |
| pOptVal | Option value. |
Definition at line 1191 of file opts.c.
References NULL.
Referenced by OptsFmtLogLevel().
| char** OptsGet | ( | const char * | argv0, |
| const PkgInfo_T * | pPkgInfo, | ||
| OptsPgmInfo_T * | pPgmInfo, | ||
| OptsInfo_T * | pOptsInfo, | ||
| bool_t | bHasLogging, | ||
| int * | pargc, | ||
| char * | argv[] | ||
| ) |
Gets, validates, and sets all command line options.
| argv0 | (Massaged) program name. | |
| pPkgInfo | Package info. | |
| pPgmInfo | Program info. | |
| pOptsInfo | Program options info. | |
| bHasLogging | Do [not] include logging options. | |
| [out] | pargc | New non-option argument count. |
| argv | Command-line options and arguments. arguments. |
Definition at line 861 of file opts.c.
References EC_BAD_OPT, OptsInfo_T::fn_cvt, OptsInfo_T::has_arg, OptsInfo_T::long_opt, OptsCtl_T::m_pLongOpts, OptsCtl_T::m_sShortOpts, NULL, OK, OptsInfo_T::opt_addr, OPTS_LONGOPT_LOG, OPTS_LONGOPT_LOGFILE, OPTS_RESET_ARGCV, OPTS_RVAL_ERROR, OPTS_RVAL_HELP, OPTS_RVAL_LOG, OPTS_RVAL_LOGFILE, OPTS_RVAL_LOGNOCOLOR, OPTS_RVAL_LOGNOTIME, OPTS_RVAL_VERSION, OptsCvtArgStr(), OptsDelete(), OptsHelp(), OptsInvalid(), OptsLogDisableColor(), OptsLogDisableTimestamp(), OptsLogSetFile(), OptsLogSetLevel(), OptsNew(), OptsVersion(), and OptsInfo_T::pvt_retval.
Referenced by init().
|
static |
Prints command-line help.
This functions is called to act on –help option.
| argv0 | Program name. |
| pOptsInfo | Options info. |
| pPgmInfo | Program info. |
| bHasLogging | This program does [not] support the logging optoins. |
Definition at line 544 of file opts.c.
References OptsPgmInfo_T::diagnostics, OptsPgmInfo_T::long_desc, OptsInfo_T::long_opt, NULL, OPTS_RVAL_HELP, OPTS_RVAL_LOG, OPTS_RVAL_LOGFILE, OptsPrintf(), OptsPrintOpt(), OptsInfo_T::pvt_retval, OptsPgmInfo_T::synopsis, and OptsPgmInfo_T::usage_args.
Referenced by OptsGet().
| void OptsInvalid | ( | const char * | argv0, |
| const char * | sFmt, | ||
| ... | |||
| ) |
Invalid option or option argument print and exit.
| argv0 | Program name. |
| sFmt | Error format string. |
| ... | Variable arguments to format string. |
EC_BAD_OPT(2). Definition at line 1313 of file opts.c.
References EC_BAD_OPT, MAX_FMT_BUF, and NULL.
Referenced by OptsGet().
|
static |
Disable program logging in color.
This function is called to act on the –log-no-color option.
| argv0 | Program name. |
| sOptName | Long option name. |
| optarg | Long option argument (from getopt_long(3)). |
Definition at line 684 of file opts.c.
References LOG_SET_COLOR_ENABLE().
Referenced by OptsGet().
|
static |
Disable program logging with timestamps.
This function is called to act on the –log-no-timestamp option.
| argv0 | Program name. |
| sOptName | Long option name. |
| optarg | Long option argument (from getopt_long(3)). |
Definition at line 702 of file opts.c.
References LOG_SET_TIMESTAMP_ENABLE().
Referenced by OptsGet().
|
static |
Set program logging output file.
This function is called to act on the –logfile <file> option.
| argv0 | Program name. |
| sOptName | Long option name. |
| optarg | Long option argument (from getopt_long(3)). |
Definition at line 664 of file opts.c.
References LOG_SET_LOGFILE(), and OptsCvtArgStr().
Referenced by OptsGet().
|
static |
Set program logging level.
This function is called to act on the –log <level> option.
| argv0 | Program name. |
| sOptName | Long option name. |
| optarg | Long option argument (from getopt_long(3)). |
Definition at line 644 of file opts.c.
References LOG_SET_THRESHOLD(), and OptsCvtArgLogLevel().
Referenced by OptsGet().
|
static |
Allocate and build short and long options getopt_long() parameters from the provided option info.
| pOptsInfo | Options info table. |
| bHasLogging | This program does [not] support the logging optoins. |
Definition at line 718 of file opts.c.
References arraysize, OptsInfo_T::has_arg, OptsInfo_T::long_opt, OptsCtl_T::m_pLongOpts, OptsCtl_T::m_sShortOpts, NEW, NULL, OPTS_LONGOPT_LOG, OPTS_LONGOPT_LOGFILE, OPTS_NO_SHORT, OPTS_RVAL_USER, OptsInfo_T::pvt_retval, and OptsInfo_T::short_opt.
Referenced by OptsGet().
|
static |
Prints formatted description.
| col | Current column position. |
| colstart | Column to start. |
| sFmtDesc | Formatted description string. |
| sPgmName | Program name (argv0). |
| pOptsInfo | Options info. |
Definition at line 362 of file opts.c.
References OptsInfo_T::arg_name, HelpGetC(), HelpPutBuf(), HelpPutC(), HelpPutNL(), HelpPutStr(), LOGERROR, OptsInfo_T::long_opt, NULL, OPTS_NO_SHORT, OptsInfo_T::pvt_opt_dft, and OptsInfo_T::short_opt.
Referenced by OptsHelp(), and OptsPrintOpt().
|
static |
Prints option syntax and description in standard 'usage' format.
| argv0 | Program name. |
| pOptsInfo | Options info. |
Definition at line 456 of file opts.c.
References OptsInfo_T::arg_name, OptsInfo_T::fn_fmt, OptsInfo_T::has_arg, OptsInfo_T::has_default, HELP_OPT_OVER, HELP_OPT_OVER_2, HelpIndent(), OptsInfo_T::long_opt, NULL, OptsInfo_T::opt_addr, OptsInfo_T::opt_desc, OPTS_NO_SHORT, OptsPrintf(), OptsInfo_T::pvt_opt_dft, and OptsInfo_T::short_opt.
Referenced by OptsHelp().
|
static |
Prints command-line program version string(s).
This functions is called to act on –version option.
| argv0 | Program name. |
| pPkgInfo | Package info. |
Definition at line 621 of file opts.c.
References PkgInfo_T::m_sPkgAuthors, PkgInfo_T::m_sPkgDate, PkgInfo_T::m_sPkgDisclaimer, PkgInfo_T::m_sPkgName, PkgInfo_T::m_sPkgOwners, PkgInfo_T::m_sPkgTimeStamp, and PkgInfo_T::m_sPkgVersion.
Referenced by OptsGet().
|
static |
|
static |
Name-Value Pair table of names to log levels.