-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetOpt.cpp
165 lines (145 loc) · 4.93 KB
/
GetOpt.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* GetOpt - getopt as a c++ class.
*
* The MIT License (MIT)
*
* Copyright (c) 2015 Klaus Beyer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h> // EOF const
#include <iostream>
#include <fstream>
#include <algorithm>
#include "GetOpt.h"
static std::string removeLeftWhiteSpaces( std::string text )
{
std::string newtext;
int pos = text.find_first_not_of( " \t\n" );
if( pos != std::string::npos ) {
newtext = text.substr( pos, text.length() - pos );
} else {
newtext = text;
}
return newtext;
}
GetOpt::GetOpt( int argc, char* argv[], const std::string optstring, const std::string filename )
: index( 1 )
, optionString( optstring )
{
// Put cmd line options into front, they win over the file options.
for(int i = 0; i < argc; ++i) {
argStrings.push_back(argv[i]);
}
if( !filename.empty() ) {
std::ifstream file;
file.open( filename );
std::string line;
while( getline( file, line ) ) {
if( line[0] == '-' ) {
// Remove spaces between option and its argument.
if( line[2] == ' ' ) {
auto arg = line.substr( 2 );
line = line.substr( 0, 2 ) + removeLeftWhiteSpaces( arg );
}
}
auto found = std::count_if(std::begin(argStrings), std::end(argStrings), [line](auto s) { return line == s; });
if(found == 0) {
argStrings.push_back(line);
}
}
}
argCount = argStrings.size();
}
char GetOpt::operator()()
{
optionArgument.clear();
errorText.clear();
// Is first character of option string a ':'
if( optionString[0] == ':' ) {
errorText = argStrings[0] + std::string( ": missing option argument in " ) + optionString + "\n";
optionString.erase( 0, 1 );
return ':';
}
// Is end of argument list?
if( index >= argCount ) {
return EOF;
}
// Is a non option argument reached?
if( argStrings[index][0] != '-' ) {
if( argStrings[index][1] == '\0' )
return EOF;
if( index == argCount - 1 ) {
return EOF;
}
std::rotate(argStrings.begin()+index, argStrings.begin()+index+1, argStrings.end());
--argCount;
return this->operator()();
}
// Is end of argument list reached?
if( argStrings[index][0] == '-' && argStrings[index][1] == '-' ) {
index++;
return EOF;
}
auto scan = argStrings[index];
index++;
// Skip '-'
// Is current character in the option string
char c = scan[1];
auto place = optionString.find_first_of( c );
if( place == std::string::npos || c == ':' ) {
errorText = argStrings[0] + std::string( ": unknown option -" ) + c + "\n";
return '?';
}
// Check if an additional argument is needed.
place++;
if( optionString[place] == ':' ) {
place++;
bool argIsOptional = optionString[place] == ':';
// Check if no space is between option and its argument.
if( scan[2] != '\0' ) {
optionArgument = scan.substr( 2 );
} else if( index < argCount ) {
if( argStrings[index][0] != '-' ) {
optionArgument = argStrings[index];
index++;
} else if( !argIsOptional ) {
errorText = argStrings[0] + std::string( ": option requires argument -" ) + c + "\n";
return ':';
}
} else if( !argIsOptional ) {
errorText = argStrings[0] + std::string( ": option requires argument -" ) + c + "\n";
return ':';
}
}
return c;
}
std::vector<std::string> GetOpt::getRemainingArguments()
{
std::vector<std::string> args(argStrings.begin() + index, argStrings.end());
return args;
}
char GetOpt::iterator::operator*()
{
auto ret = getopt->operator()();
if( ret == EOF ) {
position = getopt->argCount - 1; // Set iterator to the end
} else {
position = getopt->index - 1; // In case index has advanced more than one position.
}
return ret;
}