88 #define OPTS_LONGOPT_LOG "log" 89 #define OPTS_LONGOPT_LOGFILE "logfile"
90 #define OPTS_LONGOPT_LOGNOCOLOR "log-no-color"
91 #define OPTS_LONGOPT_LOGNOTIME "log-no-timestamp"
120 .has_arg = required_argument,
124 .arg_name =
"<level>",
126 "Set logging threshold level. All logging events with priority " 128 "logged. All others will be ignored. Error events are always logged.\n" 130 " 'off' or 0 - Disable all logging.\n" 131 " 'error' or 1 - Enable error and warning logging.\n" 132 " 'diag1' or 2 - Enable diagnostics 1 logging.\n" 133 " 'diag2' or 3 - Enable diagnostics 2 logging.\n" 134 " 'diag3' or 4 - Enable diagnostics 3 logging.\n" 135 " 'diag4' or 5 - Enable diagnostics 4 logging.\n" 136 " 'diag5' or 6 - Enable diagnostics 5 logging.\n" 137 " >6 - Enable user-defined logging.",
145 .has_arg = required_argument,
149 .arg_name =
"<file>",
152 "Special %A names:\n" 162 .has_arg = no_argument,
168 "Disable logging with compiled ANSI color strings.",
176 .has_arg = no_argument,
182 "Disable logging with timestamps.",
191 .has_arg = no_argument,
192 .opt_desc =
"Display this help and exit.",
198 .long_opt =
"version",
200 .has_arg = no_argument,
201 .opt_desc =
"Output version information and exit.",
216 #define OPTS_RESET_ARGCV(optind, argc, argv) \ 217 { argc -= optind; argv += optind; } 219 #define MAX_FMT_BUF ((size_t)1024) 224 #define HELP_OPT_OVER 30 225 #define HELP_OPT_OVER_2 (HELP_OPT_OVER+2)
226 #define HELP_MAX_COL 79
232 inline int HelpIndent(
int over) {
return printf(
"%*s", over,
""); }
250 static int HelpPutBuf(
int col,
int colstart,
char buf[],
size_t *pbuflen)
256 if( (col > colstart) && (col >= (
HELP_MAX_COL - *pbuflen)) )
261 col += printf(
"%s", buf);
279 if( (s ==
NULL) || (*s == 0) )
288 col += printf(
"%s", s);
307 col += printf(
"%c", c);
324 static int HelpGetC(
int col,
int colstart,
char buf[],
size_t bufsiz,
325 size_t *pbuflen,
int c)
327 if( *pbuflen >= bufsiz )
329 *pbuflen = bufsiz - 1;
330 col =
HelpPutBuf(col, colstart, buf, pbuflen);
334 buf[*pbuflen] = (char)c;
364 const char *sFmtDesc,
365 const char *sPgmName,
372 for(s=sFmtDesc; s && *s; ++s)
377 col =
HelpPutBuf(col, colstart, buf, &buflen);
382 col =
HelpPutBuf(col, colstart, buf, &buflen);
390 if( sPgmName !=
NULL )
392 col =
HelpPutBuf(col, colstart, buf, &buflen);
399 col =
HelpPutBuf(col, colstart, buf, &buflen);
406 col =
HelpGetC(col, colstart, buf,
sizeof(buf), &buflen,
413 col =
HelpPutBuf(col, colstart, buf, &buflen);
420 col =
HelpPutBuf(col, colstart, buf, &buflen);
425 col =
HelpGetC(col, colstart, buf,
sizeof(buf), &buflen, *s);
429 LOGERROR(
"Invalid print directive: '%.2s'", s);
434 col =
HelpGetC(col, colstart, buf,
sizeof(buf), &buflen, *s);
442 col =
HelpPutBuf(col, colstart, buf, &buflen);
458 char buf[1028], buf2[256];
459 size_t buflen =
sizeof(buf), buflen2 =
sizeof(buf2);
463 col = snprintf(buf, buflen,
" ");
468 col += snprintf(buf+col, buflen-(
size_t)col,
"-%c", pOptsInfo->
short_opt);
472 col += snprintf(buf+col, buflen-(
size_t)col,
" ");
478 col += snprintf(buf+col, buflen-(
size_t)col,
"%s--%s",
484 if( (pOptsInfo->
has_arg != no_argument) &&
487 col += snprintf(buf+col, buflen-(
size_t)col,
"%s%s",
494 buf[
sizeof(buf)-1] = 0;
553 col = printf(
"Usage: %s [OPTIONS] ", argv0);
570 "Mandatory arguments to long options are also mandatory for short options.\n");
572 if( pOptsInfo !=
NULL )
607 printf(
"DIAGNOSTICS:\n");
624 "%s (package %s-%s %s)\n" 626 "Copyright (C) %s %s\n" 721 struct option *pLongOpts, *p;
723 char *sShortOpts, *s;
732 pLongOpts =
new(
sizeof(
struct option)*(n+1));
733 sShortOpts =
new(
sizeof(char)*(n*3+1));
738 for(p=pLongOpts, s=sShortOpts, q=OptsBuiltIn; q->
long_opt!=
NULL; ++q)
752 if( q->
has_arg != no_argument )
756 if( q->
has_arg == optional_argument )
765 "Warning: Built-in option pvt_retval 0x%x != short_opt '%c'\n",
792 if( q->
has_arg != no_argument )
796 if( q->
has_arg == optional_argument )
813 p->has_arg = no_argument;
834 if( pOptsCtl !=
NULL )
866 int *pargc,
char *argv[])
884 pOptsCtl =
OptsNew(pOptsInfo, bHasLogging);
908 OptsHelp(argv0, pOptsInfo, pPgmInfo, bHasLogging);
941 else if( p->
has_arg == no_argument )
954 fprintf(stderr,
"Warning: getopt_long() return character code 0x%x\n",
1023 if( !strcmp(optarg,
"true") || !strcmp(optarg,
"1") )
1025 *((
bool_t *)pOptVal) =
true;
1027 else if( !strcmp(optarg,
"false") || !strcmp(optarg,
"0") )
1029 *((
bool_t *)pOptVal) =
false;
1039 *((
bool_t *)pOptVal) =
true;
1073 optargInt = (int)strtol(optarg, &optargEnd, 0);
1074 if( *optargEnd != 0 )
1078 *((
int *)pOptVal) = optargInt;
1083 *((
int *)pOptVal) = 1;
1116 if( sscanf(optarg,
"%lf", &optargFloat) != 1 )
1120 *((
double *)pOptVal) = optargFloat;
1125 *((
double *)pOptVal) = 1.0;
1177 *((
int *)pOptVal) = logLevel;
1194 if( (pOptVal ==
NULL) || (*((
char **)pOptVal) ==
NULL) )
1200 strncpy(buf, *((
char **)pOptVal), buflen);
1217 snprintf(buf, buflen,
"%d", *((
int *)pOptVal));
1233 snprintf(buf, buflen,
"%f", *((
double *)pOptVal));
1251 if( *((
int *)pOptVal) ==
true )
1259 strncpy(buf, s, buflen);
1275 buf[0] = *((
char *)pOptVal);
1291 int level = *((
int *)pOptVal);
1318 if( (sFmt !=
NULL) && (*sFmt != 0) )
1320 snprintf(fmtbuf,
MAX_FMT_BUF,
"%s: %s\n", argv0, sFmt);
1323 vfprintf(stderr, fmtbuf, ap);
1326 fprintf(stderr,
"Try '%s --help' for more information.\n", argv0);
Simple associative map data and operator declarations.
char * OptsFmtBool(char *buf, size_t buflen, void *pOptVal)
Boolean option value string formatter.
void LOG_SET_TIMESTAMP_ENABLE(bool_t enable)
Enable/disable log timestamps.
static Nvp_T OptsLogLevelTbl[]
int OptsCvtArgInt(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options integer argument to integer.
void LOG_SET_COLOR_ENABLE(bool_t enable)
Enable/disable logging in color.
const char * diagnostics
Programs diagnostics subsection string.
static void OptsDelete(OptsCtl_T *pOptsCtl)
Delete an allocated options control structure.
#define LOG_LEVEL_DIAG2
diagnostic level 2
#define LOG_LEVEL_OFF
turn off all non-error logging
char * m_sShortOpts
short options string
#define OPTS_RVAL_LOGNOTIME
–log-no-timestamp rval
int short_opt
Short option character.
const char * m_sPkgVersion
package dotted version string
OptsFmtFunc_T fn_fmt
Option value string formatting function.
char * new_strdup(const char *s)
Duplicate a string.
static void OptsLogDisableTimestamp(const char *argv0, const char *sOptName, char *optarg)
Disable program logging with timestamps.
Standard command-line options options and parsing.
static int HelpPutC(int col, int colstart, int c)
Puts help character, returning new output column position.
Program Description Strings Info Structure.
int OptsCvtArgLogLevel(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to log threshold level.
#define OPTS_RVAL_USER
start of user available
#define LOG_LEVEL_DIAG5
diagnostic level 5
static bool_t OptsLogNoTimestamp
working log no timestamp value
int LOG_SET_THRESHOLD(int nLevel)
Set new logging threshold level.
void * opt_addr
Address of option variable.
int HelpIndent(int over)
Indents (spaces) over, returning number of added indentation.
#define OPTS_LONGOPT_LOGFILE
log file long option name
Memory allocation and deallocation declarations.
char * OptsFmtFloat(char *buf, size_t buflen, void *pOptVal)
Float option value string formatter.
static void OptsLogDisableColor(const char *argv0, const char *sOptName, char *optarg)
Disable program logging in color.
int HelpPutNL(int over)
Puts help newline, returning new output column position.
RoadNarrows Robotics standard package information.
const char * long_desc
Program full description string.
#define EC_BAD_OPT
bad command line option exit code
#define OPTS_RVAL_HELP
–help return value
#define HELP_MAX_COL
max help column
int LOG_SET_LOGFILE(const char *sLogFileName)
Set new logging output file.
const char * synopsis
Simple program synopsis string.
char * pvt_opt_dft
Option default value in string format.
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.
#define OPTS_LONGOPT_LOGNOTIME
log no timestamps option
#define arraysize(array)
array size, i.e. number of array entries
const char * m_sPkgTimeStamp
package build date
#define LOGERROR(fmt,...)
Standard Error logging.
static void OptsHelp(const char *argv0, OptsInfo_T *pOptsInfo, OptsPgmInfo_T *pPgmInfo, bool_t bHasLogging)
Prints command-line help.
static int OptsLogLevel
working log level value
const char * m_sPkgDate
package extended creation date string
static void OptsPrintOpt(const char *argv0, OptsInfo_T *pOptsInfo)
Prints option syntax and description in standard 'usage' format.
char * OptsFmtInt(char *buf, size_t buflen, void *pOptVal)
Integer option value string formatter.
static int OptsPrintf(int col, int colstart, const char *sFmtDesc, const char *sPgmName, OptsInfo_T *pOptsInfo)
Prints formatted description.
static int HelpPutStr(int col, int colstart, const char *s)
Puts help string, returning new output column position.
const char * m_sPkgName
package name string
static char * OptsLogFile
working log file value
const char * usage_args
Single line description string of non-option arguments.
#define LOG_FILENAME_STDOUT
'stdout' log filename
const char * arg_name
Option argument name string.
#define OPTS_LONGOPT_LOGNOCOLOR
log with no color option
#define NEW(T)
Allocate new type.
#define OPTS_NO_SHORT
no short option equivalent
#define LOG_LEVEL_DIAG3
diagnostic level 3
#define OPTS_RVAL_LOGFILE
–logfile return value
char * OptsFmtLogLevel(char *buf, size_t buflen, void *pOptVal)
Log Level option value string formatter.
OptsCvtFunc_T fn_cvt
Option conversion function.
static int HelpPutBuf(int col, int colstart, char buf[], size_t *pbuflen)
Puts help buffer, returning new output column position.
static void OptsLogSetFile(const char *argv0, const char *sOptName, char *optarg)
Set program logging output file.
#define LOG_LEVEL_DFT
default log level is off
int pvt_retval
Return value.
#define HELP_OPT_OVER
opt help desc 1st line indent
RoadNarrows Robotics common configuration file.
int OptsCvtArgFloat(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options float argument to double.
int OptsCvtArgBool(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options boolean argument to bool_t.
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...
bool_t has_default
Option does [not] have a default value.
#define OPTS_RVAL_LOGNOCOLOR
–log-no-color rval
struct option * m_pLongOpts
long options structure
#define LOG_LEVEL_DIAG1
diagnostic level 1
#define OPTS_RESET_ARGCV(optind, argc, argv)
Reset argc,argv to start of non-option command arguments.
const char * m_sPkgDisclaimer
package disclaimer string
#define HELP_OPT_OVER_2
opt help desc 2nd line+ indent
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. ...
static void OptsLogSetLevel(const char *argv0, const char *sOptName, char *optarg)
Set program logging level.
#define OPTS_RVAL_ERROR
options error
void OptsInvalid(const char *argv0, const char *sFmt,...)
Invalid option or option argument print and exit.
#define LOG_FILENAME_DFT
default log filename
int has_arg
Option does [not] have an argument of type.
char * OptsFmtStr(char *buf, size_t buflen, void *pOptVal)
String option value string formatter.
static bool_t OptsLogNoColor
working log no color value
#define OPTS_RVAL_VERSION
–version return value
static void OptsVersion(const char *argv0, const PkgInfo_T *pPkgInfo)
Prints command-line program version string(s).
const char * m_sPkgOwners
package owner(s) string
#define OPTS_RVAL_LOG
-l, –log return value
#define LOG_LEVEL_ERROR
errors
int OptsCvtArgStr(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to string.
const char * opt_desc
Option description string.
const char * long_opt
Long option string name.
#define LOG_FILENAME_STDERR
'stderr' log filename
const char * m_sPkgAuthors
package author(s) string
#define MAX_FMT_BUF
Format buffer size.
#define LOG_LEVEL_DIAG4
diagnostic level 4
#define OPTS_LONGOPT_LOG
log long option name
#define OPTSBADARG(argv0, opt, arg)
Standard bad option error message reporter.
Short and Long Options Info.
static OptsInfo_T OptsBuiltIn[]
Built-In Options.
char * OptsFmtChar(char *buf, size_t buflen, void *pOptVal)
Character option value string formatter.
int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName)
Get the value associated with the name.