librnr  1.14.5
RoadNarrows Robotics Common Library 1
opts.h File Reference

Standard command-line options options and parsing. More...

#include <limits.h>
#include <getopt.h>
#include "rnr/rnrconfig.h"
#include "rnr/pkg.h"

Go to the source code of this file.

Classes

struct  OptsInfo_T
 Short and Long Options Info. More...
 
struct  OptsPgmInfo_T
 Program Description Strings Info Structure. More...
 

Macros

#define OPTS_NO_SHORT   0
 no short option equivalent
 
#define OPTS_RVAL_ERROR   '?'
 options error
 
#define OPTS_RVAL_LOG   'l'
 -l, –log return value
 
#define OPTS_RVAL_LONG   (CHAR_MAX + 1)
 long return value start
 
#define OPTS_RVAL_HELP   (OPTS_RVAL_LONG)
 –help return value
 
#define OPTS_RVAL_VERSION   (OPTS_RVAL_LONG + 1)
 –version return value
 
#define OPTS_RVAL_LOGFILE   (OPTS_RVAL_LONG + 2)
 –logfile return value
 
#define OPTS_RVAL_LOGNOCOLOR   (OPTS_RVAL_LONG + 3)
 –log-no-color rval
 
#define OPTS_RVAL_LOGNOTIME   (OPTS_RVAL_LONG + 4)
 –log-no-timestamp rval
 
#define OPTS_RVAL_USER   (OPTS_RVAL_LONG + 5)
 start of user available
 
#define OPTSBADARG(argv0, opt, arg)   OptsInvalid(argv0, "Invalid '%s' argument to '%s' option.", arg, opt)
 Standard bad option error message reporter. More...
 

Typedefs

typedef int(* OptsCvtFunc_T) (const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
 Option Argument Conversion Function Type.
 
typedef char *(* OptsFmtFunc_T) (char *buf, size_t buflen, void *pOptVal)
 Option Value String Formatter Type.
 

Functions

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...
 

Detailed Description

Standard command-line options options and parsing.

Every application that uses librnr options facilities are provided with a set of built-in options (described below) to standardize RoadNarrows application interfaces.

Built-In Options
-l, –log=level

Set logging threshold level. All logging events with priority ≤ level will be logged. All others will be ignored. The level argument is one of:

'off' or 0 Disable all logging.
'error' or 1 Enable error and warning logging.
'diag1' or 2 Enable diagnostics 1 logging.
'diag2' or 3 Enable diagnostics 2 logging.
'diag3' or 4 Enable diagnostics 3 logging.
'diag4' or 5 Enable diagnostics 4 logging.
'diag5' or 6 Enable diagnostics 5 logging.
> 6 Enable user-defined logging.

DEFAULT: off

–logfile=file

Set log file file. Special file names:

'stderr' log to standard error.
'stdout' log to standard output.

DEFAULT: stderr

–log-no-color

Disable logging with compiled ANSI color strings.
DEFAULT: false

–log-no-timestamp

Disable logging with timestamps.
DEFAULT: false

–help

Display this help and exit.

–version

Output version information and exit.

See also
Page example_log under "Related Pages" for an example usage of options.
Package
RoadNarrows Robotics Common Library 1
Library
librnr
File
rnr/opts.h
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
License
MIT
EULA
See the README and EULA files for any copyright and licensing information.

Definition in file opts.h.

Macro Definition Documentation

#define OPTSBADARG (   argv0,
  opt,
  arg 
)    OptsInvalid(argv0, "Invalid '%s' argument to '%s' option.", arg, opt)

Standard bad option error message reporter.

Parameters
argv0Command name string.
optOption name string.
argOption argument name string.

Definition at line 263 of file opts.h.

Referenced by OptsCvtArgBool(), OptsCvtArgFloat(), and OptsCvtArgInt().

Function Documentation

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.

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
true
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1018 of file opts.c.

References OK, and OPTSBADARG.

1020 {
1021  if( optarg )
1022  {
1023  if( !strcmp(optarg, "true") || !strcmp(optarg, "1") )
1024  {
1025  *((bool_t *)pOptVal) = true;
1026  }
1027  else if( !strcmp(optarg, "false") || !strcmp(optarg, "0") )
1028  {
1029  *((bool_t *)pOptVal) = false;
1030  }
1031  else
1032  {
1033  OPTSBADARG(argv0, sOptName, optarg);
1034  }
1035  }
1036  // optional argument default
1037  else
1038  {
1039  *((bool_t *)pOptVal) = true;
1040  }
1041 
1042  return OK;
1043 }
#define OK
Okay.
Definition: rnrconfig.h:301
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
#define OPTSBADARG(argv0, opt, arg)
Standard bad option error message reporter.
Definition: opts.h:263
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.

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
1.0
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1109 of file opts.c.

References OK, and OPTSBADARG.

1111 {
1112  double optargFloat;
1113 
1114  if( optarg )
1115  {
1116  if( sscanf(optarg, "%lf", &optargFloat) != 1 )
1117  {
1118  OPTSBADARG(argv0, sOptName, optarg);
1119  }
1120  *((double *)pOptVal) = optargFloat;
1121  }
1122  // optional argument default
1123  else
1124  {
1125  *((double *)pOptVal) = 1.0;
1126  }
1127 
1128  return OK;
1129 }
#define OK
Okay.
Definition: rnrconfig.h:301
#define OPTSBADARG(argv0, opt, arg)
Standard bad option error message reporter.
Definition: opts.h:263
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>).

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
1
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1065 of file opts.c.

References OK, and OPTSBADARG.

Referenced by OptsCvtArgLogLevel().

1067 {
1068  int optargInt;
1069  char *optargEnd;
1070 
1071  if( optarg )
1072  {
1073  optargInt = (int)strtol(optarg, &optargEnd, 0);
1074  if( *optargEnd != 0 )
1075  {
1076  OPTSBADARG(argv0, sOptName, optarg);
1077  }
1078  *((int *)pOptVal) = optargInt;
1079  }
1080  // optional argument default
1081  else
1082  {
1083  *((int *)pOptVal) = 1;
1084  }
1085 
1086  return OK;
1087 }
#define OK
Okay.
Definition: rnrconfig.h:301
#define OPTSBADARG(argv0, opt, arg)
Standard bad option error message reporter.
Definition: opts.h:263
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

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
LOG_LEVEL_DFT
Exceptions
OPTSBADARG()
Returns
If returns, then returns OK.

Definition at line 1151 of file opts.c.

References arraysize, LOG_LEVEL_DFT, LOG_LEVEL_OFF, NvpName2Val(), OK, and OptsCvtArgInt().

Referenced by OptsLogSetLevel().

1153 {
1154  int logLevel;
1155 
1156  if( optarg )
1157  {
1158  logLevel = NvpName2Val(OptsLogLevelTbl, arraysize(OptsLogLevelTbl), optarg);
1159 
1160  // assume integer format
1161  if( logLevel < LOG_LEVEL_OFF )
1162  {
1163  OptsCvtArgInt(argv0, sOptName, optarg, &logLevel);
1164  }
1165  }
1166  // optional argument default
1167  else
1168  {
1169  logLevel = LOG_LEVEL_DFT;
1170  }
1171 
1172  if( logLevel < LOG_LEVEL_OFF )
1173  {
1174  logLevel = LOG_LEVEL_OFF;
1175  }
1176 
1177  *((int *)pOptVal) = logLevel;
1178 
1179  return OK;
1180 }
static Nvp_T OptsLogLevelTbl[]
Definition: opts.c:72
int OptsCvtArgInt(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options integer argument to integer.
Definition: opts.c:1065
#define LOG_LEVEL_OFF
turn off all non-error logging
Definition: log.h:179
#define OK
Okay.
Definition: rnrconfig.h:301
#define arraysize(array)
array size, i.e. number of array entries
Definition: rnrconfig.h:259
#define LOG_LEVEL_DFT
default log level is off
Definition: log.h:187
int NvpName2Val(Nvp_T tbl[], size_t nTblEntries, const char *sName)
Get the value associated with the name.
Definition: assoc.c:64
int OptsCvtArgStr ( const char *  argv0,
const char *  sOptName,
char *  optarg,
void *  pOptVal 
)

Convert options string argument to string.

Note
Function returns integer for derived applications. librnr Opts ignores the return value.
Parameters
argv0Command name.
sOptNameOption name.
optargParsed option argument to convert (optional).
[out]pOptValPointer to converted option value.
Default:
"" (zero-length string)
Returns
If returns, then returns OK.

Definition at line 987 of file opts.c.

References new_strdup(), and OK.

Referenced by OptsGet(), and OptsLogSetFile().

989 {
990  *((char **)pOptVal) = new_strdup(optarg);
991  return OK;
992 }
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
#define OK
Okay.
Definition: rnrconfig.h:301
char* OptsFmtBool ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Boolean option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1247 of file opts.c.

1248 {
1249  char *s;
1250 
1251  if( *((int *)pOptVal) == true )
1252  {
1253  s = "true";
1254  }
1255  else
1256  {
1257  s = "false";
1258  }
1259  strncpy(buf, s, buflen);
1260  buf[buflen-1] = 0;
1261  return buf;
1262 }
char* OptsFmtChar ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Character option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1273 of file opts.c.

1274 {
1275  buf[0] = *((char *)pOptVal);
1276  buf[1] = 0;
1277  return buf;
1278 }
char* OptsFmtFloat ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Float option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1231 of file opts.c.

1232 {
1233  snprintf(buf, buflen, "%f", *((double *)pOptVal));
1234  buf[buflen-1] = 0;
1235  return buf;
1236 }
char* OptsFmtInt ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Integer option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1215 of file opts.c.

Referenced by OptsFmtLogLevel().

1216 {
1217  snprintf(buf, buflen, "%d", *((int *)pOptVal));
1218  buf[buflen-1] = 0;
1219  return buf;
1220 }
char* OptsFmtLogLevel ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

Log Level option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1289 of file opts.c.

References LOG_LEVEL_OFF, OptsFmtInt(), and OptsFmtStr().

1290 {
1291  int level = *((int *)pOptVal);
1292  char *off = "off";
1293 
1294  if( level == LOG_LEVEL_OFF )
1295  {
1296  return OptsFmtStr(buf, buflen, &off);
1297  }
1298  else
1299  {
1300  return OptsFmtInt(buf, buflen, pOptVal);
1301  }
1302 }
#define LOG_LEVEL_OFF
turn off all non-error logging
Definition: log.h:179
char * OptsFmtInt(char *buf, size_t buflen, void *pOptVal)
Integer option value string formatter.
Definition: opts.c:1215
char * OptsFmtStr(char *buf, size_t buflen, void *pOptVal)
String option value string formatter.
Definition: opts.c:1191
char* OptsFmtStr ( char *  buf,
size_t  buflen,
void *  pOptVal 
)

String option value string formatter.

Parameters
[out]bufOutput buffer.
buflenBuffer length.
pOptValOption value.
Returns
Pointer to start of buf.

Definition at line 1191 of file opts.c.

References NULL.

Referenced by OptsFmtLogLevel().

1192 {
1193 
1194  if( (pOptVal == NULL) || (*((char **)pOptVal) == NULL) )
1195  {
1196  buf[0] = 0;
1197  }
1198  else
1199  {
1200  strncpy(buf, *((char **)pOptVal), buflen);
1201  buf[buflen-1] = 0;
1202  }
1203  return buf;
1204 }
#define NULL
null pointer
Definition: rnrconfig.h:199
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.

Parameters
argv0(Massaged) program name.
pPkgInfoPackage info.
pPgmInfoProgram info.
pOptsInfoProgram options info.
bHasLoggingDo [not] include logging options.
[out]pargcNew non-option argument count.
argvCommand-line options and arguments. arguments.
Returns
Reposition command-line arguments to start of first non-option comand argument.

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().

867 {
868  OptsCtl_T *pOptsCtl;
869  OptsInfo_T *p;
870  int argc = *pargc;
871  int c;
872  int option_index;
873  bool_t bHasOpt;
874 
875  //
876  // Set external getopt_long() variables. N.B. makes OptsGet() non-reentrant.
877  //
878  optarg = NULL; // current argument to option
879  optind = 0; // next index in argv to look for options
880  opterr = 1; // allow getopt_long() to print error messages
881  optopt = 0; // current parsed option character
882 
883  // build option arguments from info
884  pOptsCtl = OptsNew(pOptsInfo, bHasLogging);
885 
886  // parse and convert options
887  while(true)
888  {
889  option_index = 0;
890 
891  // parse next long/short option
892  c = getopt_long(argc, argv, pOptsCtl->m_sShortOpts, pOptsCtl->m_pLongOpts,
893  &option_index);
894 
895  // end of option list
896  if( c == -1 )
897  {
898  break;
899  }
900 
901  // process option
902  switch(c)
903  {
904  case OPTS_RVAL_ERROR: // -? options error
905  OptsInvalid(argv0, "");
906  break;
907  case OPTS_RVAL_HELP: // --help
908  OptsHelp(argv0, pOptsInfo, pPgmInfo, bHasLogging);
909  exit(OK);
910  break;
911  case OPTS_RVAL_VERSION: // --version
912  OptsVersion(argv0, pPkgInfo);
913  exit(OK);
914  break;
915  case OPTS_RVAL_LOG: // -l, --log <level>
916  OptsLogSetLevel(argv0, OPTS_LONGOPT_LOG, optarg);
917  break;
918  case OPTS_RVAL_LOGFILE: // --logfile <file>
919  OptsLogSetFile(argv0, OPTS_LONGOPT_LOGFILE, optarg);
920  break;
921  case OPTS_RVAL_LOGNOCOLOR: // --log-no-color
922  OptsLogDisableColor(argv0, OPTS_LONGOPT_LOG, optarg);
923  break;
924  case OPTS_RVAL_LOGNOTIME: // --log-no-timestamp
926  break;
927  default: // user application option
928  bHasOpt = false;
929  for(p=pOptsInfo; (p!=NULL) && (p->long_opt!=NULL); ++p)
930  {
931  if( p->pvt_retval == c )
932  {
933  bHasOpt = true;
934 
935  // user supplied option conversion function
936  if( p->fn_cvt != NULL )
937  {
938  p->fn_cvt(argv0, p->long_opt, optarg, p->opt_addr);
939  }
940  // default for option w/o argument is boolean true value
941  else if( p->has_arg == no_argument )
942  {
943  *((int *)p->opt_addr) = true;
944  }
945  // default for option w/ argument is string value
946  else
947  {
948  OptsCvtArgStr(argv0, p->long_opt, optarg, p->opt_addr);
949  }
950  }
951  }
952  if( !bHasOpt )
953  {
954  fprintf(stderr, "Warning: getopt_long() return character code 0x%x\n",
955  c);
956  exit(EC_BAD_OPT);
957  }
958  break;
959  }
960  }
961 
962  OptsDelete(pOptsCtl);
963 
964  OPTS_RESET_ARGCV(optind, argc, argv);
965 
966  *pargc = argc;
967 
968  return argv;
969 }
static void OptsDelete(OptsCtl_T *pOptsCtl)
Delete an allocated options control structure.
Definition: opts.c:832
char * m_sShortOpts
short options string
Definition: opts.c:99
#define OPTS_RVAL_LOGNOTIME
–log-no-timestamp rval
Definition: opts.h:111
static void OptsLogDisableTimestamp(const char *argv0, const char *sOptName, char *optarg)
Disable program logging with timestamps.
Definition: opts.c:702
#define OK
Okay.
Definition: rnrconfig.h:301
#define NULL
null pointer
Definition: rnrconfig.h:199
void * opt_addr
Address of option variable.
Definition: opts.h:162
#define OPTS_LONGOPT_LOGFILE
log file long option name
Definition: opts.c:89
static void OptsLogDisableColor(const char *argv0, const char *sOptName, char *optarg)
Disable program logging in color.
Definition: opts.c:684
#define EC_BAD_OPT
bad command line option exit code
Definition: rnrconfig.h:311
#define OPTS_RVAL_HELP
–help return value
Definition: opts.h:107
static void OptsHelp(const char *argv0, OptsInfo_T *pOptsInfo, OptsPgmInfo_T *pPgmInfo, bool_t bHasLogging)
Prints command-line help.
Definition: opts.c:544
#define OPTS_RVAL_LOGFILE
–logfile return value
Definition: opts.h:109
OptsCvtFunc_T fn_cvt
Option conversion function.
Definition: opts.h:170
static void OptsLogSetFile(const char *argv0, const char *sOptName, char *optarg)
Set program logging output file.
Definition: opts.c:664
int pvt_retval
Return value.
Definition: opts.h:204
int bool_t
"boolean" T/F
Definition: rnrconfig.h:187
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...
Definition: opts.c:718
#define OPTS_RVAL_LOGNOCOLOR
–log-no-color rval
Definition: opts.h:110
struct option * m_pLongOpts
long options structure
Definition: opts.c:98
#define OPTS_RESET_ARGCV(optind, argc, argv)
Reset argc,argv to start of non-option command arguments.
Definition: opts.c:216
static void OptsLogSetLevel(const char *argv0, const char *sOptName, char *optarg)
Set program logging level.
Definition: opts.c:644
#define OPTS_RVAL_ERROR
options error
Definition: opts.h:104
void OptsInvalid(const char *argv0, const char *sFmt,...)
Invalid option or option argument print and exit.
Definition: opts.c:1313
int has_arg
Option does [not] have an argument of type.
Definition: opts.h:149
#define OPTS_RVAL_VERSION
–version return value
Definition: opts.h:108
static void OptsVersion(const char *argv0, const PkgInfo_T *pPkgInfo)
Prints command-line program version string(s).
Definition: opts.c:621
#define OPTS_RVAL_LOG
-l, –log return value
Definition: opts.h:105
int OptsCvtArgStr(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options string argument to string.
Definition: opts.c:987
const char * long_opt
Long option string name.
Definition: opts.h:137
#define OPTS_LONGOPT_LOG
log long option name
Definition: opts.c:88
Short and Long Options Info.
Definition: opts.h:134
Definition: opts.c:96
void OptsInvalid ( const char *  argv0,
const char *  sFmt,
  ... 
)

Invalid option or option argument print and exit.

Parameters
argv0Program name.
sFmtError format string.
...Variable arguments to format string.
Note
The function terminates process with exit status EC_BAD_OPT(2).

Definition at line 1313 of file opts.c.

References EC_BAD_OPT, MAX_FMT_BUF, and NULL.

Referenced by OptsGet().

1314 {
1315  va_list ap;
1316  char fmtbuf[MAX_FMT_BUF];
1317 
1318  if( (sFmt != NULL) && (*sFmt != 0) )
1319  {
1320  snprintf(fmtbuf, MAX_FMT_BUF, "%s: %s\n", argv0, sFmt);
1321  fmtbuf[MAX_FMT_BUF-1] = 0;
1322  va_start(ap, sFmt);
1323  vfprintf(stderr, fmtbuf, ap);
1324  va_end(ap);
1325  }
1326  fprintf(stderr, "Try '%s --help' for more information.\n", argv0);
1327  exit(EC_BAD_OPT);
1328 }
#define NULL
null pointer
Definition: rnrconfig.h:199
#define EC_BAD_OPT
bad command line option exit code
Definition: rnrconfig.h:311
#define MAX_FMT_BUF
Format buffer size.
Definition: opts.c:219