apps/elftosb/common/options.h
// ****************************************************************************
// ^FILE: options.h - option parsing classes
//
// ^DESCRIPTION:
// This file defines classes used to parse command-line options.
// Options may be parsed from an array of strings, or from any structure
// for which a corresponding option-iterator exists.
//
// ^HISTORY:
// 03/06/92 Brad Appleton <bradapp@enteract.com> Created
//
// 03/23/93 Brad Appleton <bradapp@enteract.com>
// - Added OptIstreamIter class
//
// 03/08/94 Brad Appleton <bradapp@enteract.com>
// - Added Options::reset() member function
//
// 07/31/97 Brad Appleton <bradapp@enteract.com>
// - Added PARSE_POS control flag and POSITIONAL return value
//
// 04/30/06 Chris Reed
// - Updated to modern C++ and STL
// - Converted comments to doxygen style
// ^^**************************************************************************
#ifndef _options_h
#define _options_h
#ifdef USE_STDIO
#include <stdio.h>
#else
#include <iostream>
#endif
class OptIter
{
public:
OptIter(void) {}
virtual ~OptIter(void);
virtual const char *curr(void) = 0;
virtual void next(void) = 0;
virtual const char *operator()(void);
};
class OptIterRwd : public OptIter
{
public:
OptIterRwd(void);
virtual ~OptIterRwd(void);
virtual const char *curr(void) = 0;
virtual void next(void) = 0;
virtual const char *operator()(void) = 0;
virtual void rewind(void) = 0;
};
class OptArgvIter : public OptIterRwd
{
private:
int ndx; // index of current arg
int ac; // arg count
const char *const *av; // arg vector
public:
OptArgvIter(const char *const argv[])
: av(argv)
, ac(-1)
, ndx(0)
{
}
OptArgvIter(int argc, const char *const argv[])
: av(argv)
, ac(argc)
, ndx(0)
{
}
virtual ~OptArgvIter(void);
virtual const char *curr(void);
virtual void next(void);
virtual const char *operator()(void);
virtual void rewind(void);
int index(void) { return ndx; }
};
class OptStrTokIter : public OptIterRwd
{
private:
unsigned len; // length of token-string
const char *str; // the token-string
const char *seps; // delimiter-set (separator-characters)
const char *cur; // current token
char *tokstr; // our copy of the token-string
static const char *default_delims; // default delimiters = whitespace
public:
OptStrTokIter(const char *tokens, const char *delimiters = 0);
virtual ~OptStrTokIter(void);
virtual const char *curr(void);
virtual void next(void);
virtual const char *operator()(void);
virtual void rewind(void);
const char *delimiters(void) { return seps; }
void delimiters(const char *delims) { seps = (delims) ? delims : default_delims; }
};
class OptIstreamIter : public OptIter
{
private:
std::istream &is;
OptStrTokIter *tok_iter;
void fill(void);
public:
static const unsigned MAX_LINE_LEN;
OptIstreamIter(std::istream &input);
virtual ~OptIstreamIter(void);
virtual const char *curr(void);
virtual void next(void);
virtual const char *operator()(void);
};
class Options
{
private:
unsigned explicit_end : 1;
unsigned optctrls : 7;
const char *const *optvec;
const char *nextchar;
const char *listopt;
const char *cmdname;
void check_syntax(void) const;
const char *match_opt(char opt, int ignore_case = 0) const;
const char *match_longopt(const char *opt, int len, int &ambiguous) const;
int parse_opt(OptIter &iter, const char *&optarg);
int parse_longopt(OptIter &iter, const char *&optarg);
public:
enum OptCtrl
{
DEFAULT = 0x00,
ANYCASE = 0x01,
QUIET = 0x02,
PLUS = 0x04,
SHORT_ONLY = 0x08,
LONG_ONLY = 0x10,
NOGUESSING = 0x20,
PARSE_POS = 0x40
};
enum OptRC
{
ENDOPTS = 0,
BADCHAR = -1,
BADKWD = -2,
AMBIGUOUS = -3,
POSITIONAL = -4
};
Options(const char *name, const char *const optv[]);
virtual ~Options(void);
const char *name(void) const { return cmdname; }
unsigned ctrls(void) const { return optctrls; }
void ctrls(unsigned newctrls) { optctrls = newctrls; }
void reset(void) { nextchar = listopt = NULL; }
void usage(std::ostream &os, const char *positionals) const;
int operator()(OptIter &iter, const char *&optarg);
int explicit_endopts() const { return explicit_end; }
};
#endif /* _options_h */
OptStrTokIter
Definition: apps/elftosb/common/options.h:117
Options
parse command-line options
Definition: apps/elftosb/common/options.h:346
OptArgvIter
Definition: apps/elftosb/common/options.h:79
OptStrTokIter::delimiters
const char * delimiters(void)
Definition: apps/elftosb/common/options.h:143
OptIstreamIter
Definition: apps/elftosb/common/options.h:162
Options::name
const char * name(void) const
name() returns the command name
Definition: apps/elftosb/common/options.h:408
OptArgvIter::index
int index(void)
index returns the current index to use for argv[]
Definition: apps/elftosb/common/options.h:112
OptIter::operator()
virtual const char * operator()(void)
Definition: apps/elftosb/common/options.cpp:108
OptIter::next
virtual void next(void)=0
next() advances to the next item.
Options::OptRC
OptRC
Definition: apps/elftosb/common/options.h:394
OptIterRwd
Definition: apps/elftosb/common/options.h:59
Options::explicit_endopts
int explicit_endopts() const
Definition: apps/elftosb/common/options.h:453
Options::ctrls
unsigned ctrls(void) const
ctrls() (with no arguments) returns the existing control settings
Definition: apps/elftosb/common/options.h:410
Options::ctrls
void ctrls(unsigned newctrls)
ctrls() (with 1 argument) sets new control settings
Definition: apps/elftosb/common/options.h:412
OptIter::curr
virtual const char * curr(void)=0
Options::OptCtrl
OptCtrl
Definition: apps/elftosb/common/options.h:367
Options::reset
void reset(void)
reset for another pass to parse for options
Definition: apps/elftosb/common/options.h:414
OptIter
Definition: apps/elftosb/common/options.h:37