OpenM++ runtime library (libopenm)
argReader.h
Go to the documentation of this file.
1
5// Copyright (c) 2013-2015 OpenM++
6// This code is licensed under the MIT license (see LICENSE.txt for details)
7
8#ifndef OM_H_ARG_READER_H
9#define OM_H_ARG_READER_H
10
11#include "omHelper.h"
12
13using namespace std;
14
15namespace openm
16{
18 struct ArgKey
19 {
21 static constexpr const char * iniFile = "OpenM.IniFile";
22
24 static constexpr const char * iniAnyKey = "OpenM.IniAnyKey";
25
27 static constexpr const char * runStamp = "OpenM.RunStamp";
28
30 static constexpr const char * logToConsole = "OpenM.LogToConsole";
31
33 static constexpr const char * logToFile = "OpenM.LogToFile";
34
36 static constexpr const char * logFilePath = "OpenM.LogFilePath";
37
39 static constexpr const char * logToStamped = "OpenM.LogToStampedFile";
40
42 static constexpr const char * logUseTs = "OpenM.LogUseTimeStamp";
43
45 static constexpr const char * logUsePid = "OpenM.LogUsePidStamp";
46
48 static constexpr const char * logNoMsgTime = "OpenM.LogNoMsgTime";
49
51 static constexpr const char * logSql = "OpenM.LogSql";
52 };
53
55 struct ArgReader
56 {
58 ArgReader(void) { }
59
62 int argc,
63 char ** argv,
64 const size_t i_keyArrSize,
65 const char ** i_keyArr,
66 const size_t i_shortArrSize = 0,
67 const pair<const char *, const char *> * i_shortPairArr = nullptr
68 );
69
71 ~ArgReader(void) noexcept { }
72
75
77 bool isOptionExist(const char * i_key) const noexcept;
78
80 string strOption(const char * i_key, const string & i_default = "") const noexcept;
81
83 bool boolOption(const char * i_key) const noexcept;
84
91 int boolOptionToInt(const char * i_key) const noexcept;
92
94 int intOption(const char * i_key, int i_default) const noexcept { return (int)longOption(i_key, i_default); }
95
97 long long longOption(const char * i_key, long long i_default) const noexcept;
98
100 double doubleOption(const char * i_key, double i_default) const noexcept;
101
103 void parseCommandLine(
104 int argc,
105 char ** argv,
106 bool i_isThrowUnknown,
107 bool i_isStoreUnknown,
108 const size_t i_keyArrSize,
109 const char ** i_keyArr,
110 const size_t i_shortArrSize = 0,
111 const pair<const char *, const char *> * i_shortPairArr = nullptr,
112 const size_t i_prefixSize = 0,
113 const char ** i_prefixArr = nullptr,
114 const size_t i_suffixSize = 0,
115 const char ** i_suffixArr = nullptr
116 );
117
119 void loadIniFile(
120 const char * i_filePath,
121 const size_t i_keyArrSize,
122 const char ** i_keyArr,
123 bool i_isStoreUnknown = false,
124 const size_t i_sectionToMergeSize = 0,
125 const char ** i_sectionToMergeArr = nullptr,
126 const size_t i_multiKeySize = 0,
127 const char ** i_multiKeyArr = nullptr
128 );
129
131 void adjustLogSettings(int argc, char ** argv);
132 };
133}
134
135#endif // OM_H_ARG_READER_H
openM++ namespace
Definition: log.h:32
std::map< std::string, std::string, LessNoCase > NoCaseMap
map of key-value strings with case neutral key search
Definition: omHelper.h:224
OpenM++ common helper utilities.
common openM++ keys for runtime arguments
Definition: argReader.h:19
static constexpr const char * logSql
log sql
Definition: argReader.h:51
static constexpr const char * logNoMsgTime
do not prefix log messages with date-time
Definition: argReader.h:48
static constexpr const char * iniFile
run ini file name, ie: model.ini
Definition: argReader.h:21
static constexpr const char * iniAnyKey
if true then allow any key in the ini-file, read all ini-file entries and pass it to the model
Definition: argReader.h:24
static constexpr const char * logUseTs
use time-stamp in log "stamped" file name
Definition: argReader.h:42
static constexpr const char * logFilePath
log file path
Definition: argReader.h:36
static constexpr const char * runStamp
process run stamp, usually time stamp
Definition: argReader.h:27
static constexpr const char * logToStamped
log to "stamped" file
Definition: argReader.h:39
static constexpr const char * logToConsole
log to console
Definition: argReader.h:30
static constexpr const char * logUsePid
use pid-stamp in log "stamped" file name
Definition: argReader.h:45
static constexpr const char * logToFile
log to file
Definition: argReader.h:33
arguments reader to get runtime arguments from command line and ini-file.
Definition: argReader.h:56
void adjustLogSettings(int argc, char **argv)
adjust log file settings, ie: make default log file path if required.
Definition: argReader.cpp:312
void loadIniFile(const char *i_filePath, const size_t i_keyArrSize, const char **i_keyArr, bool i_isStoreUnknown=false, const size_t i_sectionToMergeSize=0, const char **i_sectionToMergeArr=nullptr, const size_t i_multiKeySize=0, const char **i_multiKeyArr=nullptr)
read ini-file and merge command line arguments.
Definition: argReader.cpp:249
int boolOptionToInt(const char *i_key) const noexcept
return 1 if key found and value is one of: "yes", "1", "true" or empty value, return 0 if key found a...
Definition: argReader.cpp:89
string strOption(const char *i_key, const string &i_default="") const noexcept
return string value by key or default value if not found.
Definition: argReader.cpp:58
ArgReader(void)
initialize empty list of run arguments.
Definition: argReader.h:58
bool isOptionExist(const char *i_key) const noexcept
return true if option with this key exist.
Definition: argReader.cpp:47
long long longOption(const char *i_key, long long i_default) const noexcept
return long value by key or default if not found or can not be converted to long.
Definition: argReader.cpp:100
bool boolOption(const char *i_key) const noexcept
return boolean value by key or false if not found or value not "yes", "1", "true" (case insensitive).
Definition: argReader.cpp:73
void parseCommandLine(int argc, char **argv, bool i_isThrowUnknown, bool i_isStoreUnknown, const size_t i_keyArrSize, const char **i_keyArr, const size_t i_shortArrSize=0, const pair< const char *, const char * > *i_shortPairArr=nullptr, const size_t i_prefixSize=0, const char **i_prefixArr=nullptr, const size_t i_suffixSize=0, const char **i_suffixArr=nullptr)
get run options from command line, ie: number of cases.
Definition: argReader.cpp:141
NoCaseMap args
arguments as key-value string pairs with case-neutral search
Definition: argReader.h:74
int intOption(const char *i_key, int i_default) const noexcept
return int value by key or default if not found or can not be converted to int.
Definition: argReader.h:94
~ArgReader(void) noexcept
cleanup argument holder resources
Definition: argReader.h:71
double doubleOption(const char *i_key, double i_default) const noexcept
return double value by key or default if not found or can not be converted to double.
Definition: argReader.cpp:113