Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Friends | List of all members
JPARSER::JParser< JKey_t > Class Template Reference

Utility class to parse command line options. More...

#include <JParser.hh>

Inheritance diagram for JPARSER::JParser< JKey_t >:
std::map< JKey_t, JParserElement > JEEP::JMessage< T >

Public Types

typedef JLANG::JParserException JParserException
 
typedef JKey_t key_type
 
typedef std::map< key_type,
JParserElement
map_type
 
typedef map_type::iterator iterator
 
typedef map_type::const_iterator const_iterator
 

Public Member Functions

 JParser (const int debug=0)
 Default constructor. More...
 
 JParser (const std::string &message, const int debug=0)
 Constructor. More...
 
void print (std::ostream &out) const
 Print the possible command line options. More...
 
JArgs operator() (const int argc, const char *const argv[])
 Parse the program's command line options. More...
 
JArgs operator() (const JArgs &args)
 Parse the program's command line options. More...
 
int read (const int argc, const char *const argv[])
 Parse the program's command line options. More...
 
int read (const JArgs &args)
 Parse the program's command line options. More...
 
std::ostream & write (std::ostream &out) const
 Print the current parameter values. More...
 

Static Public Attributes

static int debug = 0
 debug level (default is off). More...
 

Protected Member Functions

void check_status () const
 Check if all required options have been set. More...
 

Protected Attributes

std::string help
 help message More...
 
std::string pid
 process name More...
 

Friends

std::ostream & operator<< (std::ostream &out, const JParser< key_type > &parser)
 Stream output. More...
 

Detailed Description

template<class JKey_t = char>
class JPARSER::JParser< JKey_t >

Utility class to parse command line options.

The mapping between a parameter (of any type) and a unique option has to be defined in the user's application, e.g.

#include "Jeep/JParser.hh"
int main(int argc, char**argv)
{
int aap;
bool noot;
bool mies;
try {
JParser<> zap;
zap['i'] = make_field(aap) = 123; // set default value
zap['b'] = make_field(noot); // default is false
zap['B'] = make_field(mies);
zap(argc, argv);
}
catch(const std::exception& error) {
cerr << error.what() << endl;
return 1;
}
}

The behaviour of the parser is different for parameters of type bool. By default, its value is set to false; it is set to true when the corresponding option is parsed. This implies that no data are read and that several options can be parsed in sequence without repeating the '-' symbol.

The syntax for the command line is:

program [-<option> <value> [-<option> <value>]]
program -h 

will print the usage specification including all existing options.

program --!

will terminate the parsing of options and print the actual setting of all options.

After the command line has been parsed, it is checked whether each parameter has been assigned a value by default or at run time. If not, an exception is thrown.

Definition at line 1410 of file JParser.hh.

Member Typedef Documentation

template<class JKey_t = char>
typedef JLANG::JParserException JPARSER::JParser< JKey_t >::JParserException

Definition at line 1416 of file JParser.hh.

template<class JKey_t = char>
typedef JKey_t JPARSER::JParser< JKey_t >::key_type

Definition at line 1417 of file JParser.hh.

template<class JKey_t = char>
typedef std::map<key_type, JParserElement> JPARSER::JParser< JKey_t >::map_type

Definition at line 1418 of file JParser.hh.

template<class JKey_t = char>
typedef map_type::iterator JPARSER::JParser< JKey_t >::iterator

Definition at line 1420 of file JParser.hh.

template<class JKey_t = char>
typedef map_type::const_iterator JPARSER::JParser< JKey_t >::const_iterator

Definition at line 1421 of file JParser.hh.

Constructor & Destructor Documentation

template<class JKey_t = char>
JPARSER::JParser< JKey_t >::JParser ( const int  debug = 0)
inline

Default constructor.

Parameters
debugdebug level

Definition at line 1431 of file JParser.hh.

1431  :
1432  help ()
1433  {
1434  this->debug = debug;
1435  }
static int debug
debug level (default is off).
Definition: JMessage.hh:43
std::string help
help message
Definition: JParser.hh:1744
template<class JKey_t = char>
JPARSER::JParser< JKey_t >::JParser ( const std::string &  message,
const int  debug = 0 
)
inline

Constructor.

Parameters
messagemessage printed with option -h
debugdebug level

Definition at line 1444 of file JParser.hh.

1445  :
1446  help (message)
1447  {
1448  this->debug = debug;
1449  }
static int debug
debug level (default is off).
Definition: JMessage.hh:43
std::string help
help message
Definition: JParser.hh:1744

Member Function Documentation

template<class JKey_t = char>
void JPARSER::JParser< JKey_t >::print ( std::ostream &  out) const
inline

Print the possible command line options.

Parameters
outoutput stream
Returns
output stream

Definition at line 1458 of file JParser.hh.

1459  {
1460  using namespace std;
1461 
1462  if (help != "") {
1463  out << help << endl;
1464  }
1465 
1466  out << "usage: " << getFilename(pid) << endl;
1467 
1468  out << ' ' << START_OF_OPTION << HELP_OPTION << ' ' << " \"help\"" << endl;
1469  out << ' ' << START_OF_OPTION << HELP_OPTION << PRINT_OPTION << " \"help with print of default and possible values\"" << endl;
1470  out << ' ' << START_OF_OPTION << REVISION_OPTION << ' ' << " \"print revision\"" << endl;
1471  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << ' ' << " \"end of options; remainder will be discarded\"" << endl;
1472  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << PRINT_OPTION << " \"end of options with print of actual values\"" << endl;
1473 
1474  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1475 
1476  out << ' ' << START_OF_OPTION << i->first << ' ';
1477 
1478  i->second.print(out);
1479 
1480  out << endl;
1481  }
1482  }
static char PRINT_OPTION
print option
Definition: JParser.hh:80
std::string help
help message
Definition: JParser.hh:1744
static char REVISION_OPTION
revision option
Definition: JParser.hh:79
static char END_OF_OPTIONS
end of all options
Definition: JParser.hh:77
static char START_OF_OPTION
Parser options.
Definition: JParser.hh:76
std::string getFilename(const std::string &file_name)
Get file name part, i.e.
Definition: JeepToolkit.hh:85
map_type::const_iterator const_iterator
Definition: JParser.hh:1421
std::string pid
process name
Definition: JParser.hh:1745
static char HELP_OPTION
help option
Definition: JParser.hh:78
template<class JKey_t = char>
JArgs JPARSER::JParser< JKey_t >::operator() ( const int  argc,
const char *const  argv[] 
)
inline

Parse the program's command line options.

Parameters
argcnumber of arguments
argvargument list
Returns
argument list

Definition at line 1492 of file JParser.hh.

1493  {
1494  return (*this)(JArgs(argc, argv));
1495  }
Data structure to store command line arguments.
Definition: JArgs.hh:24
template<class JKey_t = char>
JArgs JPARSER::JParser< JKey_t >::operator() ( const JArgs args)
inline

Parse the program's command line options.

Parameters
argsargument list
Returns
argument list

Definition at line 1504 of file JParser.hh.

1505  {
1506  using namespace std;
1507  using namespace JLANG;
1508  using namespace JEEP;
1509 
1510  pid = args.PID;
1511 
1512  istringstream is;
1513 
1514  // argument passing
1515 
1516  for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) {
1517 
1518  DEBUG("Processing option <" << *i << ">" << endl);
1519 
1520  is.clear();
1521  is.str(*i);
1522 
1523  for (int c; (c = is.get()) != EOF; ) {
1524 
1525  if (c == START_OF_OPTION) {
1526 
1527  if (is.peek() == EOF) { // end-of-file
1528 
1529  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));
1530 
1531  } else if (isspace(is.peek())) { // white space
1532 
1533  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));;
1534  }
1535 
1536  while (is.peek() != EOF) { // read option(s)
1537 
1538  if (is.peek() == HELP_OPTION) { // help
1539 
1540  is.get();
1541 
1542  setPrintOption(cerr, SHORT_PRINT);
1543 
1544  if (is.peek() != EOF) {
1545 
1546  if (is.get() == PRINT_OPTION) {
1547 
1548  setPrintOption(cerr, LONG_PRINT);
1549 
1550  } else {
1551 
1552  THROW(JParserException, "invalid option at <" << is.str() << ">");
1553  }
1554  }
1555 
1556  print(cerr);
1557 
1558  exit(NORMAL_EXIT_CODE);
1559 
1560  } else if (is.peek() == REVISION_OPTION) { // revision
1561 
1562  cerr << "version: " << getGITVersion() << endl;
1563  cerr << "commit: " << getGITCommit() << endl;
1564  cerr << "date: " << getGITDate() << endl;
1565  cerr << "namespace: " << getNamespace() << endl;
1566 
1567  exit(NORMAL_EXIT_CODE);
1568 
1569  } else if (is.peek() == END_OF_OPTIONS) { // end of options
1570 
1571  is.get();
1572 
1573  if (is.peek() != EOF) {
1574 
1575  c = is.get();
1576 
1577  if (c == PRINT_OPTION) {
1578 
1580 
1581  print(cerr);
1582 
1583  } else if (c == PID_OPTION) {
1584 
1585  if (is.peek() == EOF && i + 1 != args.end()) {
1586  is.clear();
1587  is.str(*++i);
1588  }
1589 
1590  string file_name;
1591 
1592  getline(is, file_name);
1593 
1594  ofstream out(file_name.c_str());
1595 
1596  out << getpid() << endl;
1597 
1598  if (!out) {
1599  THROW(JParserException, "invalid option at <" << is.str() << ">");
1600  }
1601 
1602  out.close();
1603 
1604  } else {
1605 
1606  THROW(JParserException, "invalid option at <" << is.str() << ">");
1607  }
1608  }
1609 
1610  check_status();
1611 
1612  return JArgs(pid, ++i, args.end());
1613 
1614  } else {
1615 
1616  key_type option;
1617 
1618  is >> option;
1619 
1620  iterator p = this->find(option);
1621 
1622  DEBUG("Processing option <" << option << "> " << (p != this->end()) << endl);
1623 
1624  if (p != this->end()) {
1625 
1626  if (p->second->gcount()) {
1627 
1628  if (is.peek() == EOF && i + 1 != args.end()) {
1629  is.clear();
1630  is.str(*++i);
1631  }
1632  }
1633 
1634  try {
1635  is >> p->second;
1636  }
1637  catch(const exception& error) {
1638  THROW(JParserException, "read error " << error.what() << " at <" << is.str() << ">");
1639  }
1640 
1641  if (fail(is)) {
1642  THROW(JParserException, "read error at <" << is.str() << ">");
1643  }
1644 
1645  } else {
1646 
1647  THROW(JParserException, "unknown option <" << is.str() << "> at " << JArgs("", i, args.end()));
1648  }
1649  }
1650  }
1651 
1652  } else {
1653 
1654  THROW(JParserException, "illegal character <" << (char) c << "> at " << JArgs("", i, args.end()));
1655  }
1656  }
1657  }
1658 
1659  check_status();
1660 
1661  return JArgs();
1662  }
static char PID_OPTION
print PID to file
Definition: JParser.hh:81
const char * getGITCommit()
Get GIT commit.
Definition: Jpp.hh:68
static char PRINT_OPTION
print option
Definition: JParser.hh:80
const char * getGITDate()
Get GIT date.
Definition: Jpp.hh:83
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
long print
Definition: JPrint.hh:40
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition: JPrint.hh:80
std::string getNamespace(const std::string &type_name)
Get name space, i.e.
Definition: JeepToolkit.hh:207
static char REVISION_OPTION
revision option
Definition: JParser.hh:79
void print(std::ostream &out) const
Print the possible command line options.
Definition: JParser.hh:1458
short print
Definition: JPrint.hh:38
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
const char * getGITVersion()
Get GIT version.
Definition: Jpp.hh:53
void check_status() const
Check if all required options have been set.
Definition: JParser.hh:1730
Data structure to store command line arguments.
Definition: JArgs.hh:24
Exception when parsing a value.
Definition: JException.hh:504
bool fail(std::istream &in)
Check for stream state.
Definition: JParser.hh:93
std::string PID
Definition: JArgs.hh:167
static char END_OF_OPTIONS
end of all options
Definition: JParser.hh:77
medium print
Definition: JPrint.hh:39
static int NORMAL_EXIT_CODE
exit code of normal end
Definition: JParser.hh:82
static char START_OF_OPTION
Parser options.
Definition: JParser.hh:76
std::string pid
process name
Definition: JParser.hh:1745
virtual const char * what() const
Get error message.
Definition: JException.hh:65
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
static char HELP_OPTION
help option
Definition: JParser.hh:78
template<class JKey_t = char>
int JPARSER::JParser< JKey_t >::read ( const int  argc,
const char *const  argv[] 
)
inline

Parse the program's command line options.


This method is maintained for backward compatibility and will be deprecated.

Parameters
argcnumber of arguments
argvargument list
Returns
0

Definition at line 1673 of file JParser.hh.

1674  {
1675  (*this)(argc, argv);
1676 
1677  return 0;
1678  }
template<class JKey_t = char>
int JPARSER::JParser< JKey_t >::read ( const JArgs args)
inline

Parse the program's command line options.


This method is maintained for backward compatibility and will be deprecated.

Parameters
argsargument list
Returns
0

Definition at line 1688 of file JParser.hh.

1689  {
1690  (*this)(args);
1691 
1692  return 0;
1693  }
template<class JKey_t = char>
std::ostream& JPARSER::JParser< JKey_t >::write ( std::ostream &  out) const
inline

Print the current parameter values.

Parameters
outoutput stream
Returns
output stream

Definition at line 1702 of file JParser.hh.

1703  {
1704  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1705  out << i->second->getName() << '=' << i->second << std::endl;
1706  }
1707 
1708  return out;
1709  }
map_type::const_iterator const_iterator
Definition: JParser.hh:1421
template<class JKey_t = char>
void JPARSER::JParser< JKey_t >::check_status ( ) const
inlineprotected

Check if all required options have been set.


This method throws an exception in case of a non-compliance.

Definition at line 1730 of file JParser.hh.

1731  {
1732  for (const_iterator p = this->begin(); p != this->end(); ++p) {
1733 
1734  if (!p->second->getInitialisationStatus()) {
1735  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has no value");
1736  }
1737 
1738  if (!p->second->getStatus()) {
1739  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has illegal value");
1740  }
1741  }
1742  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:633
Exception when parsing a value.
Definition: JException.hh:504
static char START_OF_OPTION
Parser options.
Definition: JParser.hh:76
map_type::const_iterator const_iterator
Definition: JParser.hh:1421

Friends And Related Function Documentation

template<class JKey_t = char>
std::ostream& operator<< ( std::ostream &  out,
const JParser< key_type > &  parser 
)
friend

Stream output.

Parameters
outoutput stream
parserparser
Returns
output stream

Definition at line 1719 of file JParser.hh.

1720  {
1721  return parser.write(out);
1722  }
std::ostream & write(std::ostream &out) const
Print the current parameter values.
Definition: JParser.hh:1702

Member Data Documentation

template<class JKey_t = char>
std::string JPARSER::JParser< JKey_t >::help
protected

help message

Definition at line 1744 of file JParser.hh.

template<class JKey_t = char>
std::string JPARSER::JParser< JKey_t >::pid
protected

process name

Definition at line 1745 of file JParser.hh.

template<class T>
int JEEP::JMessage< T >::debug = 0
staticinherited

debug level (default is off).

Definition at line 43 of file JMessage.hh.


The documentation for this class was generated from the following file: