Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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< char, 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.
 
 JParser (const std::string &message, const int debug=0)
 Constructor.
 
JParserjoin (const JParser &parser)
 Join parser.
 
void print (std::ostream &out) const
 Print the possible command line options.
 
virtual void terminate (const int status)
 Terminate.
 
JArgs operator() (const int argc, const char *const argv[])
 Parse the program's command line options.
 
JArgs operator() (const JArgs &args)
 Parse the program's command line options.
 
int read (const int argc, const char *const argv[])
 Parse the program's command line options.
 
int read (const JArgs &args)
 Parse the program's command line options.
 
std::ostream & write (std::ostream &out) const
 Print the current parameter values.
 

Static Public Attributes

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

Protected Member Functions

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

Protected Attributes

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

Friends

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

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 {
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;
}
}
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2140
int main(int argc, char *argv[])
Definition Main.cc:15
Utility class to parse command line options.
Definition JParser.hh:1697

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

Member Typedef Documentation

◆ JParserException

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

Definition at line 1700 of file JParser.hh.

◆ key_type

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

Definition at line 1701 of file JParser.hh.

◆ map_type

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

Definition at line 1702 of file JParser.hh.

◆ iterator

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

Definition at line 1704 of file JParser.hh.

◆ const_iterator

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

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

1715 :
1716 help ()
1717 {
1718 this->debug = debug;
1719 }
std::string help
help message
Definition JParser.hh:2062
static int debug
debug level (default is off).
Definition JMessage.hh:45

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

1729 :
1730 help (message)
1731 {
1732 this->debug = debug;
1733 }

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

1743 {
1744 this->insert(parser.begin(), parser.end());
1745
1746 return *this;
1747 }

◆ 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

Definition at line 1755 of file JParser.hh.

1756 {
1757 using namespace std;
1758 using namespace JPP;
1759
1760 if (help != "") {
1761 out << help << endl;
1762 }
1763
1764 out << "usage: " << getFilename(pid) << endl;
1765
1766 out << ' ' << START_OF_OPTION << HELP_OPTION << ' ' << " \"help\"" << endl;
1767 out << ' ' << START_OF_OPTION << HELP_OPTION << PRINT_OPTION << " \"help with print of default and possible values\"" << endl;
1768 out << ' ' << START_OF_OPTION << REVISION_OPTION << ' ' << " \"print revision\"" << endl;
1769 out << ' ' << START_OF_OPTION << END_OF_OPTIONS << ' ' << " \"end of options; remainder will be discarded\"" << endl;
1770 out << ' ' << START_OF_OPTION << END_OF_OPTIONS << PRINT_OPTION << " \"end of options with print of actual values\"" << endl;
1771
1772 for (const_iterator i = this->begin(); i != this->end(); ++i) {
1773
1774 out << ' ' << START_OF_OPTION << i->first << " ";
1775
1776 i->second.print(out);
1777
1778 out << endl;
1779 }
1780
1781 if (getURL() != "") {
1782 out << endl << "See also: " << getURL() << '#' << getFilename(pid) << endl;
1783 }
1784 }
std::string pid
process name
Definition JParser.hh:2063
map_type::const_iterator const_iterator
Definition JParser.hh:1705
std::string getURL()
Get URL of document pages.
static char END_OF_OPTIONS
end of all options
Definition JParser.hh:80
static char REVISION_OPTION
revision option
Definition JParser.hh:82
static char START_OF_OPTION
Parser options.
Definition JParser.hh:79
static char HELP_OPTION
help option
Definition JParser.hh:81
static char PRINT_OPTION
print option
Definition JParser.hh:83
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).

◆ terminate()

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

Terminate.

Parameters
statusexit status

Definition at line 1792 of file JParser.hh.

1793 {
1794 exit(status);
1795 }

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

1806 {
1807 return (*this)(JArgs(argc, argv));
1808 }
Data structure to store command line arguments.
Definition JArgs.hh:26

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

1818 {
1819 using namespace std;
1820 using namespace JLANG;
1821 using namespace JEEP;
1822
1823 pid = args.PID;
1824
1825 istringstream is;
1826
1827 // argument passing
1828
1829 for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) {
1830
1831 DEBUG("Processing option <" << *i << ">" << endl);
1832
1833 is.clear();
1834 is.str(*i);
1835
1836 for (int c; (c = is.get()) != EOF; ) {
1837
1838 if (c == START_OF_OPTION) {
1839
1840 if (is.peek() == EOF) { // end-of-file
1841
1842 THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));
1843
1844 } else if (isspace(is.peek())) { // white space
1845
1846 THROW(JParserException, "stray " << START_OF_OPTION << " in <" << is.str() << "> at " << JArgs("", i, args.end()));;
1847 }
1848
1849 while (is.peek() != EOF) { // read option(s)
1850
1851 if (is.peek() == HELP_OPTION) { // help
1852
1853 is.get();
1854
1855 setPrintOption(cout, SHORT_PRINT);
1856
1857 if (is.peek() != EOF) {
1858
1859 if (is.get() == PRINT_OPTION) {
1860
1861 setPrintOption(cout, LONG_PRINT);
1862
1863 } else {
1864
1865 THROW(JParserException, "invalid option at <" << is.str() << ">");
1866 }
1867 }
1868
1869 print(cout);
1870
1872
1873 return JArgs();
1874
1875 } else if (is.peek() == REVISION_OPTION) { // revision
1876
1877 cout << "source: " << getSource() << endl;
1878 cout << "version: " << getGITVersion() << endl;
1879 cout << "commit: " << getGITCommit() << endl;
1880 cout << "date: " << getGITDate() << endl;
1881 cout << "namespace: " << getNamespace() << endl;
1882
1884
1885 return JArgs();
1886
1887 } else if (is.peek() == END_OF_OPTIONS) { // end of options
1888
1889 is.get();
1890
1891 if (is.peek() != EOF) {
1892
1893 c = is.get();
1894
1895 if (c == PRINT_OPTION) {
1896
1897 setPrintOption(cout, MEDIUM_PRINT);
1898
1899 print(cout);
1900
1901 } else if (c == PID_OPTION) {
1902
1903 if (is.peek() == EOF && i + 1 != args.end()) {
1904 is.clear();
1905 is.str(*++i);
1906 }
1907
1908 string file_name;
1909
1910 getline(is, file_name);
1911
1912 ofstream out(file_name.c_str());
1913
1914 out << getpid() << endl;
1915
1916 if (!out) {
1917 THROW(JParserException, "invalid option at <" << is.str() << ">");
1918 }
1919
1920 out.close();
1921
1922 } else {
1923
1924 THROW(JParserException, "invalid option at <" << is.str() << ">");
1925 }
1926 }
1927
1928 check_status();
1929
1930 return JArgs(pid, ++i, args.end());
1931
1932 } else {
1933
1934 key_type option;
1935
1936 is >> option;
1937
1938 iterator p = this->find(option);
1939
1940 DEBUG("Processing option <" << option << "> " << (p != this->end()) << endl);
1941
1942 if (p != this->end()) {
1943
1944 if (p->second->gcount()) {
1945
1946 if (is.peek() == EOF && i + 1 != args.end()) {
1947 is.clear();
1948 is.str(*++i);
1949 }
1950 }
1951
1952 try {
1953 is >> p->second;
1954 }
1955 catch(const exception& error) {
1956 THROW(JParserException, "read error " << error.what() << " at <" << is.str() << ">");
1957 }
1958
1959 if (fail(is)) {
1960 THROW(JParserException, "read error at <" << is.str() << ">");
1961 }
1962
1963 } else {
1964
1965 THROW(JParserException, "unknown option <" << is.str() << "> at " << JArgs("", i, args.end()));
1966 }
1967 }
1968 }
1969
1970 } else {
1971
1972 THROW(JParserException, "illegal character <" << (char) c << "> at " << JArgs("", i, args.end()));
1973 }
1974 }
1975 }
1976
1977 check_status();
1978
1979 return JArgs();
1980 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
void setPrintOption(std::ostream &out, const int option)
Set print option.
Definition JManip.hh:63
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
Exception when parsing a value.
virtual void terminate(const int status)
Terminate.
Definition JParser.hh:1792
void print(std::ostream &out) const
Print the possible command line options.
Definition JParser.hh:1755
void check_status() const
Check if all required options have been set.
Definition JParser.hh:2048
General puprpose classes and methods.
std::string getGITVersion(const std::string &tag)
Get GIT version for given GIT tag.
Auxiliary classes and methods for language specific functionality.
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition JString.hh:478
const char * getGITDate()
Get GIT date.
Definition Jpp.cc:39
const char * getSource()
Get source.
Definition Jpp.hh:89
const char * getGITCommit()
Get GIT commit.
Definition Jpp.cc:24
bool fail(std::istream &in)
Check for stream state.
Definition JParser.hh:97
static int NORMAL_EXIT_CODE
exit code of normal end
Definition JParser.hh:85
static char PID_OPTION
print PID to file
Definition JParser.hh:84

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

1992 {
1993 (*this)(argc, argv);
1994
1995 return 0;
1996 }

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

2007 {
2008 (*this)(args);
2009
2010 return 0;
2011 }

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

2021 {
2022 for (const_iterator i = this->begin(); i != this->end(); ++i) {
2023 out << i->second->getName() << '=' << i->second << std::endl;
2024 }
2025
2026 return out;
2027 }

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

2049 {
2050 for (const_iterator p = this->begin(); p != this->end(); ++p) {
2051
2052 if (!p->second->getInitialisationStatus()) {
2053 THROW(JParserException, pid << " option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has no value");
2054 }
2055
2056 if (!p->second->getStatus()) {
2057 THROW(JParserException, pid << " option: " << START_OF_OPTION << p->first << " <" << p->second->getName() << ">" << " has illegal value");
2058 }
2059 }
2060 }
JLANG::JParserException JParserException
Definition JParser.hh:1700

Friends And Related Symbol 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 2037 of file JParser.hh.

2038 {
2039 return parser.write(out);
2040 }
std::ostream & write(std::ostream &out) const
Print the current parameter values.
Definition JParser.hh:2020

Member Data Documentation

◆ help

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

help message

Definition at line 2062 of file JParser.hh.

◆ pid

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

process name

Definition at line 2063 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: