Jpp
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, JParserElementmap_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...
 
JParserjoin (const JParser &parser)
 Join parser. More...
 
void print (std::ostream &out) const
 Print the possible command line options. More...
 
virtual void terminate (const int status)
 Terminate. 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 1493 of file JParser.hh.

Member Typedef Documentation

◆ JParserException

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

Definition at line 1499 of file JParser.hh.

◆ key_type

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

Definition at line 1500 of file JParser.hh.

◆ map_type

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

Definition at line 1501 of file JParser.hh.

◆ iterator

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

Definition at line 1503 of file JParser.hh.

◆ const_iterator

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

Definition at line 1504 of file JParser.hh.

Constructor & Destructor Documentation

◆ JParser() [1/2]

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

Default constructor.

Parameters
debugdebug level

Definition at line 1514 of file JParser.hh.

1514  :
1515  help ()
1516  {
1517  this->debug = debug;
1518  }

◆ JParser() [2/2]

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 1527 of file JParser.hh.

1528  :
1529  help (message)
1530  {
1531  this->debug = debug;
1532  }

Member Function Documentation

◆ join()

template<class JKey_t = char>
JParser& JPARSER::JParser< JKey_t >::join ( const JParser< JKey_t > &  parser)
inline

Join parser.

Parameters
parserparser
Returns
this parser

Definition at line 1541 of file JParser.hh.

1542  {
1543  this->insert(parser.begin(), parser.end());
1544 
1545  return *this;
1546  }

◆ print()

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 1555 of file JParser.hh.

1556  {
1557  using namespace std;
1558  using namespace JPP;
1559 
1560  if (help != "") {
1561  out << help << endl;
1562  }
1563 
1564  out << "usage: " << getFilename(pid) << endl;
1565 
1566  out << ' ' << START_OF_OPTION << HELP_OPTION << ' ' << " \"help\"" << endl;
1567  out << ' ' << START_OF_OPTION << HELP_OPTION << PRINT_OPTION << " \"help with print of default and possible values\"" << endl;
1568  out << ' ' << START_OF_OPTION << REVISION_OPTION << ' ' << " \"print revision\"" << endl;
1569  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << ' ' << " \"end of options; remainder will be discarded\"" << endl;
1570  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << PRINT_OPTION << " \"end of options with print of actual values\"" << endl;
1571 
1572  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1573 
1574  out << ' ' << START_OF_OPTION << i->first << " ";
1575 
1576  i->second.print(out);
1577 
1578  out << endl;
1579  }
1580 
1581  if (getURL() != "") {
1582  out << endl << "See also: " << getURL() << '#' << getFilename(pid) << endl;
1583  }
1584  }

◆ terminate()

template<class JKey_t = char>
virtual void JPARSER::JParser< JKey_t >::terminate ( const int  status)
inlinevirtual

Terminate.

Parameters
statusexit status

Definition at line 1592 of file JParser.hh.

1593  {
1594  exit(status);
1595  }

◆ operator()() [1/2]

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 1605 of file JParser.hh.

1606  {
1607  return (*this)(JArgs(argc, argv));
1608  }

◆ operator()() [2/2]

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 1617 of file JParser.hh.

1618  {
1619  using namespace std;
1620  using namespace JLANG;
1621  using namespace JEEP;
1622 
1623  pid = args.PID;
1624 
1625  istringstream is;
1626 
1627  // argument passing
1628 
1629  for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) {
1630 
1631  DEBUG("Processing option <" << *i << ">" << endl);
1632 
1633  is.clear();
1634  is.str(*i);
1635 
1636  for (int c; (c = is.get()) != EOF; ) {
1637 
1638  if (c == START_OF_OPTION) {
1639 
1640  if (is.peek() == EOF) { // end-of-file
1641 
1642  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));
1643 
1644  } else if (isspace(is.peek())) { // white space
1645 
1646  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));;
1647  }
1648 
1649  while (is.peek() != EOF) { // read option(s)
1650 
1651  if (is.peek() == HELP_OPTION) { // help
1652 
1653  is.get();
1654 
1655  setPrintOption(cout, SHORT_PRINT);
1656 
1657  if (is.peek() != EOF) {
1658 
1659  if (is.get() == PRINT_OPTION) {
1660 
1661  setPrintOption(cout, LONG_PRINT);
1662 
1663  } else {
1664 
1665  THROW(JParserException, "invalid option at <" << is.str() << ">");
1666  }
1667  }
1668 
1669  print(cout);
1670 
1672 
1673  return JArgs();
1674 
1675  } else if (is.peek() == REVISION_OPTION) { // revision
1676 
1677  cout << "source: " << getSource() << endl;
1678  cout << "version: " << getGITVersion() << endl;
1679  cout << "commit: " << getGITCommit() << endl;
1680  cout << "date: " << getGITDate() << endl;
1681  cout << "namespace: " << getNamespace() << endl;
1682 
1684 
1685  return JArgs();
1686 
1687  } else if (is.peek() == END_OF_OPTIONS) { // end of options
1688 
1689  is.get();
1690 
1691  if (is.peek() != EOF) {
1692 
1693  c = is.get();
1694 
1695  if (c == PRINT_OPTION) {
1696 
1698 
1699  print(cout);
1700 
1701  } else if (c == PID_OPTION) {
1702 
1703  if (is.peek() == EOF && i + 1 != args.end()) {
1704  is.clear();
1705  is.str(*++i);
1706  }
1707 
1708  string file_name;
1709 
1710  getline(is, file_name);
1711 
1712  ofstream out(file_name.c_str());
1713 
1714  out << getpid() << endl;
1715 
1716  if (!out) {
1717  THROW(JParserException, "invalid option at <" << is.str() << ">");
1718  }
1719 
1720  out.close();
1721 
1722  } else {
1723 
1724  THROW(JParserException, "invalid option at <" << is.str() << ">");
1725  }
1726  }
1727 
1728  check_status();
1729 
1730  return JArgs(pid, ++i, args.end());
1731 
1732  } else {
1733 
1734  key_type option;
1735 
1736  is >> option;
1737 
1738  iterator p = this->find(option);
1739 
1740  DEBUG("Processing option <" << option << "> " << (p != this->end()) << endl);
1741 
1742  if (p != this->end()) {
1743 
1744  if (p->second->gcount()) {
1745 
1746  if (is.peek() == EOF && i + 1 != args.end()) {
1747  is.clear();
1748  is.str(*++i);
1749  }
1750  }
1751 
1752  try {
1753  is >> p->second;
1754  }
1755  catch(const exception& error) {
1756  THROW(JParserException, "read error " << error.what() << " at <" << is.str() << ">");
1757  }
1758 
1759  if (fail(is)) {
1760  THROW(JParserException, "read error at <" << is.str() << ">");
1761  }
1762 
1763  } else {
1764 
1765  THROW(JParserException, "unknown option <" << is.str() << "> at " << JArgs("", i, args.end()));
1766  }
1767  }
1768  }
1769 
1770  } else {
1771 
1772  THROW(JParserException, "illegal character <" << (char) c << "> at " << JArgs("", i, args.end()));
1773  }
1774  }
1775  }
1776 
1777  check_status();
1778 
1779  return JArgs();
1780  }

◆ read() [1/2]

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 1791 of file JParser.hh.

1792  {
1793  (*this)(argc, argv);
1794 
1795  return 0;
1796  }

◆ read() [2/2]

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 1806 of file JParser.hh.

1807  {
1808  (*this)(args);
1809 
1810  return 0;
1811  }

◆ write()

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 1820 of file JParser.hh.

1821  {
1822  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1823  out << i->second->getName() << '=' << i->second << std::endl;
1824  }
1825 
1826  return out;
1827  }

◆ check_status()

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 1848 of file JParser.hh.

1849  {
1850  for (const_iterator p = this->begin(); p != this->end(); ++p) {
1851 
1852  if (!p->second->getInitialisationStatus()) {
1853  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has no value");
1854  }
1855 
1856  if (!p->second->getStatus()) {
1857  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has illegal value");
1858  }
1859  }
1860  }

Friends And Related Function Documentation

◆ operator<<

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 1837 of file JParser.hh.

1838  {
1839  return parser.write(out);
1840  }

Member Data Documentation

◆ help

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

help message

Definition at line 1862 of file JParser.hh.

◆ pid

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

process name

Definition at line 1863 of file JParser.hh.

◆ debug

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

debug level (default is off).

Definition at line 45 of file JMessage.hh.


The documentation for this class was generated from the following file:
std::iterator
Definition: JSTDTypes.hh:18
JPARSER::JParser::key_type
JKey_t key_type
Definition: JParser.hh:1500
JEEP::JArgs
Data structure to store command line arguments.
Definition: JArgs.hh:24
JPARSER::JParser::help
std::string help
help message
Definition: JParser.hh:1862
JLANG::getGITCommit
const char * getGITCommit()
Get GIT commit.
Definition: Jpp.hh:73
JEEP
General puprpose classes and methods.
Definition: JArgs.hh:15
JEEP::JArgs::PID
std::string PID
Definition: JArgs.hh:167
setPrintOption
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition: JPrint.hh:99
JPARSER::JParser::check_status
void check_status() const
Check if all required options have been set.
Definition: JParser.hh:1848
JPARSER::JParser::terminate
virtual void terminate(const int status)
Terminate.
Definition: JParser.hh:1592
JPARSER::JParser::const_iterator
map_type::const_iterator const_iterator
Definition: JParser.hh:1504
JPARSER::JParser::write
std::ostream & write(std::ostream &out) const
Print the current parameter values.
Definition: JParser.hh:1820
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JEEP::LONG_PRINT
long print
Definition: JPrint.hh:41
main
int main(int argc, char *argv[])
Definition: Main.cpp:15
JEEP::getNamespace
std::string getNamespace(const std::string &type_name)
Get name space, i.e.
Definition: JeepToolkit.hh:223
JLANG::JParserException
Exception when parsing a value.
Definition: JException.hh:522
JPARSER::REVISION_OPTION
static char REVISION_OPTION
revision option
Definition: JParser.hh:78
THROW
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:669
JEEP::getFilename
std::string getFilename(const std::string &file_name)
Get file name part, i.e.
Definition: JeepToolkit.hh:86
JLANG::getGITVersion
const char * getGITVersion()
Get GIT version.
Definition: Jpp.hh:58
JEEP::MEDIUM_PRINT
medium print
Definition: JPrint.hh:40
JPARSER::END_OF_OPTIONS
static char END_OF_OPTIONS
end of all options
Definition: JParser.hh:76
JParser.hh
JEEP::SHORT_PRINT
short print
Definition: JPrint.hh:39
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JPARSER::fail
bool fail(std::istream &in)
Check for stream state.
Definition: JParser.hh:93
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
JPARSER::NORMAL_EXIT_CODE
static int NORMAL_EXIT_CODE
exit code of normal end
Definition: JParser.hh:81
JPARSER::START_OF_OPTION
static char START_OF_OPTION
Parser options.
Definition: JParser.hh:75
JLANG::JException::what
virtual const char * what() const
Get error message.
Definition: JException.hh:65
JEEP::JMessage::debug
static int debug
debug level (default is off).
Definition: JMessage.hh:45
JLANG
Auxiliary classes and methods for language specific functionality.
Definition: JAbstractClass.hh:10
JEEP::getURL
std::string getURL()
Get URL of document pages.
Definition: JeepToolkit.hh:279
JLANG::getline
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
JLANG::getSource
const char * getSource()
Get source.
Definition: Jpp.hh:103
JPARSER::JParser::pid
std::string pid
process name
Definition: JParser.hh:1863
JPARSER::PRINT_OPTION
static char PRINT_OPTION
print option
Definition: JParser.hh:79
JPARSER::HELP_OPTION
static char HELP_OPTION
help option
Definition: JParser.hh:77
JPARSER::JParser::print
void print(std::ostream &out) const
Print the possible command line options.
Definition: JParser.hh:1555
JPARSER::PID_OPTION
static char PID_OPTION
print PID to file
Definition: JParser.hh:80
JLANG::getGITDate
const char * getGITDate()
Get GIT date.
Definition: Jpp.hh:88