a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
ArgumentParser.hpp
Go to the documentation of this file.
1/*
2 * ArgumentParser.hpp
3 *
4 * Created on: Sep 13, 2012
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 *
7 * This file is part of BOLlib, released under the
8 * GNU General Public License. Please see the accompanying
9 * README.BOLlib.txt file for a full list of files, brief documentation
10 * on how to use these classes, and further details on the license.
11 */
12
13#ifndef ARGUMENTPARSER_HPP_
14#define ARGUMENTPARSER_HPP_
15
16#include <string>
17#include <vector>
18#include <fstream>
19#include "AsciiXmlParser.hpp"
20#include "StringParser.hpp"
21
22namespace BOL
23{
24 // this is a class to search the command line arguments for given strings.
26 {
27 public:
28 ArgumentParser( int argumentCount,
29 char** argumentCharArrays,
30 std::string const inputTag = "",
31 std::string const fallbackInputFilename = "" );
33
34 std::string
35 fromLiteral( std::string const& argumentName,
36 std::string const defaultReturnString = "" ) const;
37 // this looks for argumentName as a substring (from the starting char) of
38 // any of the elements of argumentStrings, & returns the rest of the 1st
39 // matching string (or defaultReturnString if no match was found).
40 std::string
41 fromTag( std::string const& tagString,
42 std::string const defaultReturnString = "" );
43 // this calls fromLiteral( "--" + argumentName + "=" ), and if it gets an
44 // empty string, it looks for an element with argumentName as an XML tag
45 // from the input file (if the input file was readable). if there is still
46 // no string to return, it returns defaultReturnString.
47
48
49 protected:
50 std::vector< std::string > argumentStrings;
52 };
53
54
55
56
57
58 inline std::string
59 ArgumentParser::fromLiteral( std::string const& argumentName,
60 std::string const defaultReturnString ) const
61 // this looks for argumentName as a substring (from the starting char) of
62 // any of the elements of argumentStrings, & returns the rest of the 1st
63 // matching string (or defaultReturnValue if no match was found).
64 {
65 for( std::vector< std::string >::const_iterator
66 whichArgument( argumentStrings.begin() );
67 argumentStrings.end() > whichArgument;
68 ++whichArgument )
69 {
70 if( 0 == whichArgument->compare( 0,
71 argumentName.size(),
72 argumentName ) )
73 {
74 return whichArgument->substr( argumentName.size() );
75 }
76 }
77 return defaultReturnString;
78 }
79
80 inline std::string
81 ArgumentParser::fromTag( std::string const& tagString,
82 std::string const defaultReturnString )
83 // this calls fromLiteral( "--" + argumentName + "=" ), and if it gets an
84 // empty string, it looks for an element with argumentName as an XML tag
85 // from the input file (if the input file was readable). if there is still
86 // no string to return, it returns defaultReturnString.
87 {
88 std::string argumentString( "--" );
89 argumentString.append( tagString );
90 argumentString.append( "=" );
91 argumentString.assign( fromLiteral( argumentString ) );
92 if( !(argumentString.empty()) )
93 {
94 return argumentString;
95 }
98 {
100 {
104 }
105 }
106 return defaultReturnString;
107 }
108
109} /* namespace BOL */
110#endif /* ARGUMENTPARSER_HPP_ */
AsciiXmlParser inputXmlParser
std::string fromTag(std::string const &tagString, std::string const defaultReturnString="")
ArgumentParser(int argumentCount, char **argumentCharArrays, std::string const inputTag="", std::string const fallbackInputFilename="")
std::string fromLiteral(std::string const &argumentName, std::string const defaultReturnString="") const
std::vector< std::string > argumentStrings
std::string const & getCurrentElementContent() const
bool currentElementNameMatches(std::string const &comparisonString) const
static std::string const whitespaceAndNewlineChars
static std::string trimFromFrontAndBack(std::string const &stringToTrim, std::string const &charsToTrim=whitespaceAndNewlineChars)