Jpp  17.1.1
the software that should make you happy
 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...
 
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.

For a comparison between the parsing of command line options with JParser and boost, see internal note which is accessible for anyone with a KM3NeT account.

Definition at line 1517 of file JParser.hh.

Member Typedef Documentation

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

Definition at line 1523 of file JParser.hh.

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

Definition at line 1524 of file JParser.hh.

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

Definition at line 1525 of file JParser.hh.

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

Definition at line 1527 of file JParser.hh.

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

Definition at line 1528 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 1538 of file JParser.hh.

1538  :
1539  help ()
1540  {
1541  this->debug = debug;
1542  }
static int debug
debug level (default is off).
Definition: JMessage.hh:45
std::string help
help message
Definition: JParser.hh:1886
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 1551 of file JParser.hh.

1552  :
1553  help (message)
1554  {
1555  this->debug = debug;
1556  }
static int debug
debug level (default is off).
Definition: JMessage.hh:45
std::string help
help message
Definition: JParser.hh:1886

Member Function Documentation

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

1566  {
1567  this->insert(parser.begin(), parser.end());
1568 
1569  return *this;
1570  }
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 1579 of file JParser.hh.

1580  {
1581  using namespace std;
1582  using namespace JPP;
1583 
1584  if (help != "") {
1585  out << help << endl;
1586  }
1587 
1588  out << "usage: " << getFilename(pid) << endl;
1589 
1590  out << ' ' << START_OF_OPTION << HELP_OPTION << ' ' << " \"help\"" << endl;
1591  out << ' ' << START_OF_OPTION << HELP_OPTION << PRINT_OPTION << " \"help with print of default and possible values\"" << endl;
1592  out << ' ' << START_OF_OPTION << REVISION_OPTION << ' ' << " \"print revision\"" << endl;
1593  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << ' ' << " \"end of options; remainder will be discarded\"" << endl;
1594  out << ' ' << START_OF_OPTION << END_OF_OPTIONS << PRINT_OPTION << " \"end of options with print of actual values\"" << endl;
1595 
1596  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1597 
1598  out << ' ' << START_OF_OPTION << i->first << " ";
1599 
1600  i->second.print(out);
1601 
1602  out << endl;
1603  }
1604 
1605  if (getURL() != "") {
1606  out << endl << "See also: " << getURL() << '#' << getFilename(pid) << endl;
1607  }
1608  }
static char PRINT_OPTION
print option
Definition: JParser.hh:99
std::string help
help message
Definition: JParser.hh:1886
static char REVISION_OPTION
revision option
Definition: JParser.hh:98
std::string getURL()
Get URL of document pages.
Definition: JeepToolkit.hh:321
static char END_OF_OPTIONS
end of all options
Definition: JParser.hh:96
static char START_OF_OPTION
Parser options.
Definition: JParser.hh:95
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:128
map_type::const_iterator const_iterator
Definition: JParser.hh:1528
std::string pid
process name
Definition: JParser.hh:1887
static char HELP_OPTION
help option
Definition: JParser.hh:97
template<class JKey_t = char>
virtual void JPARSER::JParser< JKey_t >::terminate ( const int  status)
inlinevirtual

Terminate.

Parameters
statusexit status

Definition at line 1616 of file JParser.hh.

1617  {
1618  exit(status);
1619  }
exit
Definition: JPizza.sh:36
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 1629 of file JParser.hh.

1630  {
1631  return (*this)(JArgs(argc, argv));
1632  }
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 1641 of file JParser.hh.

1642  {
1643  using namespace std;
1644  using namespace JLANG;
1645  using namespace JEEP;
1646 
1647  pid = args.PID;
1648 
1649  istringstream is;
1650 
1651  // argument passing
1652 
1653  for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) {
1654 
1655  DEBUG("Processing option <" << *i << ">" << endl);
1656 
1657  is.clear();
1658  is.str(*i);
1659 
1660  for (int c; (c = is.get()) != EOF; ) {
1661 
1662  if (c == START_OF_OPTION) {
1663 
1664  if (is.peek() == EOF) { // end-of-file
1665 
1666  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));
1667 
1668  } else if (isspace(is.peek())) { // white space
1669 
1670  THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));;
1671  }
1672 
1673  while (is.peek() != EOF) { // read option(s)
1674 
1675  if (is.peek() == HELP_OPTION) { // help
1676 
1677  is.get();
1678 
1679  setPrintOption(cout, SHORT_PRINT);
1680 
1681  if (is.peek() != EOF) {
1682 
1683  if (is.get() == PRINT_OPTION) {
1684 
1685  setPrintOption(cout, LONG_PRINT);
1686 
1687  } else {
1688 
1689  THROW(JParserException, "invalid option at <" << is.str() << ">");
1690  }
1691  }
1692 
1693  print(cout);
1694 
1696 
1697  return JArgs();
1698 
1699  } else if (is.peek() == REVISION_OPTION) { // revision
1700 
1701  cout << "source: " << getSource() << endl;
1702  cout << "version: " << getGITVersion() << endl;
1703  cout << "commit: " << getGITCommit() << endl;
1704  cout << "date: " << getGITDate() << endl;
1705  cout << "namespace: " << getNamespace() << endl;
1706 
1708 
1709  return JArgs();
1710 
1711  } else if (is.peek() == END_OF_OPTIONS) { // end of options
1712 
1713  is.get();
1714 
1715  if (is.peek() != EOF) {
1716 
1717  c = is.get();
1718 
1719  if (c == PRINT_OPTION) {
1720 
1722 
1723  print(cout);
1724 
1725  } else if (c == PID_OPTION) {
1726 
1727  if (is.peek() == EOF && i + 1 != args.end()) {
1728  is.clear();
1729  is.str(*++i);
1730  }
1731 
1732  string file_name;
1733 
1734  getline(is, file_name);
1735 
1736  ofstream out(file_name.c_str());
1737 
1738  out << getpid() << endl;
1739 
1740  if (!out) {
1741  THROW(JParserException, "invalid option at <" << is.str() << ">");
1742  }
1743 
1744  out.close();
1745 
1746  } else {
1747 
1748  THROW(JParserException, "invalid option at <" << is.str() << ">");
1749  }
1750  }
1751 
1752  check_status();
1753 
1754  return JArgs(pid, ++i, args.end());
1755 
1756  } else {
1757 
1758  key_type option;
1759 
1760  is >> option;
1761 
1762  iterator p = this->find(option);
1763 
1764  DEBUG("Processing option <" << option << "> " << (p != this->end()) << endl);
1765 
1766  if (p != this->end()) {
1767 
1768  if (p->second->gcount()) {
1769 
1770  if (is.peek() == EOF && i + 1 != args.end()) {
1771  is.clear();
1772  is.str(*++i);
1773  }
1774  }
1775 
1776  try {
1777  is >> p->second;
1778  }
1779  catch(const exception& error) {
1780  THROW(JParserException, "read error " << error.what() << " at <" << is.str() << ">");
1781  }
1782 
1783  if (fail(is)) {
1784  THROW(JParserException, "read error at <" << is.str() << ">");
1785  }
1786 
1787  } else {
1788 
1789  THROW(JParserException, "unknown option <" << is.str() << "> at " << JArgs("", i, args.end()));
1790  }
1791  }
1792  }
1793 
1794  } else {
1795 
1796  THROW(JParserException, "illegal character <" << (char) c << "> at " << JArgs("", i, args.end()));
1797  }
1798  }
1799  }
1800 
1801  check_status();
1802 
1803  return JArgs();
1804  }
static char PID_OPTION
print PID to file
Definition: JParser.hh:100
medium print
Definition: JManip.hh:39
const char * getGITCommit()
Get GIT commit.
Definition: Jpp.hh:79
static char PRINT_OPTION
print option
Definition: JParser.hh:99
const char * getGITDate()
Get GIT date.
Definition: Jpp.hh:94
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
std::string getNamespace(const std::string &type_name)
Get name space, i.e. part before JEEP::TYPENAME_SEPARATOR.
Definition: JeepToolkit.hh:265
is
Definition: JDAQCHSM.chsm:167
static char REVISION_OPTION
revision option
Definition: JParser.hh:98
void print(std::ostream &out) const
Print the possible command line options.
Definition: JParser.hh:1579
std::string getGITVersion(const std::string &tag)
Get GIT version for given GIT tag.
long print
Definition: JManip.hh:40
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
void check_status() const
Check if all required options have been set.
Definition: JParser.hh:1872
Data structure to store command line arguments.
Definition: JArgs.hh:24
Exception when parsing a value.
Definition: JException.hh:540
bool fail(std::istream &in)
Check for stream state.
Definition: JParser.hh:113
short print
Definition: JManip.hh:38
virtual void terminate(const int status)
Terminate.
Definition: JParser.hh:1616
std::string PID
Definition: JArgs.hh:167
virtual const char * what() const override
Get error message.
Definition: JException.hh:48
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition: JManip.hh:63
const char * getSource()
Get source.
Definition: Jpp.hh:109
static char END_OF_OPTIONS
end of all options
Definition: JParser.hh:96
static int NORMAL_EXIT_CODE
exit code of normal end
Definition: JParser.hh:101
static char START_OF_OPTION
Parser options.
Definition: JParser.hh:95
$WORKDIR ev_configure_domsimulator txt echo process $DOM_SIMULATOR $i $SOURCE_HOST[$index] csh c(setenv ROOTSYS $ROOTSYS &&source $JPP_DIR/setenv.csh $JPP_DIR &&($DOM_SIMULATOR\-u\$NAME\$\-H\$SERVER\$\-M\$LOGGER\$\-d $DEBUG</dev/null > &/dev/null &))'
std::string pid
process name
Definition: JParser.hh:1887
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
static char HELP_OPTION
help option
Definition: JParser.hh:97
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 1815 of file JParser.hh.

1816  {
1817  (*this)(argc, argv);
1818 
1819  return 0;
1820  }
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 1830 of file JParser.hh.

1831  {
1832  (*this)(args);
1833 
1834  return 0;
1835  }
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 1844 of file JParser.hh.

1845  {
1846  for (const_iterator i = this->begin(); i != this->end(); ++i) {
1847  out << i->second->getName() << '=' << i->second << std::endl;
1848  }
1849 
1850  return out;
1851  }
map_type::const_iterator const_iterator
Definition: JParser.hh:1528
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 1872 of file JParser.hh.

1873  {
1874  for (const_iterator p = this->begin(); p != this->end(); ++p) {
1875 
1876  if (!p->second->getInitialisationStatus()) {
1877  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has no value");
1878  }
1879 
1880  if (!p->second->getStatus()) {
1881  THROW(JParserException, "option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has illegal value");
1882  }
1883  }
1884  }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
Exception when parsing a value.
Definition: JException.hh:540
static char START_OF_OPTION
Parser options.
Definition: JParser.hh:95
map_type::const_iterator const_iterator
Definition: JParser.hh:1528

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

1862  {
1863  return parser.write(out);
1864  }
std::ostream & write(std::ostream &out) const
Print the current parameter values.
Definition: JParser.hh:1844

Member Data Documentation

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

help message

Definition at line 1886 of file JParser.hh.

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

process name

Definition at line 1887 of file JParser.hh.

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: