-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathArgumentParsing.cpp
More file actions
108 lines (91 loc) · 3.02 KB
/
ArgumentParsing.cpp
File metadata and controls
108 lines (91 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* ArgumentParsing.cpp
*
* Created by Pete Willemsen on 10/6/09.
* Copyright 2009 Department of Computer Science, University of Minnesota-Duluth. All rights reserved.
*
* This file is part of libSIVELab library (libsivelab).
*
* libsivelab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsivelab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libsivelab. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cassert>
#include <array>
#include "ArgumentParsing.h"
ArgumentParsing::ArgumentParsing()
: m_optDesc("Allowed options"){};
void ArgumentParsing::reg(const std::string &argName, ArgTypes argTypes, const std::string &description, char shortArgName)
{
std::ostringstream argName_wShort;
argName_wShort << argName;
if ((int)shortArgName > 0) {
argName_wShort << "," << shortArgName;
}
if (argTypes == NONE) {
m_optDesc.add_options()(argName_wShort.str().c_str(), description.c_str());
} else if (argTypes == INT) {
m_optDesc.add_options()(argName_wShort.str().c_str(), po::value<int>(), description.c_str());
} else if (argTypes == FLOAT) {
m_optDesc.add_options()(argName_wShort.str().c_str(), po::value<float>(), description.c_str());
} else if (argTypes == CHAR) {
m_optDesc.add_options()(argName_wShort.str().c_str(), po::value<char>(), description.c_str());
} else if (argTypes == STRING) {
m_optDesc.add_options()(argName_wShort.str().c_str(), po::value<std::string>(), description.c_str());
}
}
void ArgumentParsing::printUsage() const
{
std::cout << m_optDesc << '\n';
}
bool ArgumentParsing::isSet(const std::string &argName)
{
return (m_varMap.count(argName) > 0);
}
bool ArgumentParsing::isSet(const std::string &argName, int &argValue)
{
if (m_varMap.count(argName) > 0) {
argValue = m_varMap[argName].as<int>();
return true;
}
return false;
}
bool ArgumentParsing::isSet(const std::string &argName, float &argValue)
{
if (m_varMap.count(argName) > 0) {
argValue = m_varMap[argName].as<float>();
return true;
}
return false;
}
bool ArgumentParsing::isSet(const std::string &argName, char &argValue)
{
if (m_varMap.count(argName) > 0) {
argValue = m_varMap[argName].as<char>();
return true;
}
return false;
}
bool ArgumentParsing::isSet(const std::string &argName, std::string &argValue)
{
if (m_varMap.count(argName) > 0) {
argValue = m_varMap[argName].as<std::string>();
return true;
}
return false;
}
int ArgumentParsing::process(int argc, char *argv[])
{
po::store(po::parse_command_line(argc, argv, m_optDesc), m_varMap);
po::notify(m_varMap);
return 1;
}