@@ -26,92 +26,114 @@ void cmdlinet::clear()
26
26
27
27
bool cmdlinet::isset (char option) const
28
28
{
29
- int i=getoptnr (option);
30
- if (i<0 )
29
+ auto i=getoptnr (option);
30
+ if (i.has_value ())
31
+ return options[*i].isset ;
32
+ else
31
33
return false ;
32
- return options[i].isset ;
33
34
}
34
35
35
36
bool cmdlinet::isset (const char *option) const
36
37
{
37
- int i=getoptnr (option);
38
- if (i<0 )
38
+ auto i=getoptnr (option);
39
+ if (i.has_value ())
40
+ return options[*i].isset ;
41
+ else
39
42
return false ;
40
- return options[i].isset ;
41
43
}
42
44
43
45
std::string cmdlinet::get_value (char option) const
44
46
{
45
- int i=getoptnr (option);
46
- if (i<0 )
47
- return " " ;
48
- if (options[i].values .empty ())
47
+ auto i=getoptnr (option);
48
+
49
+ if (i.has_value ())
50
+ {
51
+ if (options[*i].values .empty ())
52
+ return " " ;
53
+ else
54
+ return options[*i].values .front ();
55
+ }
56
+ else
49
57
return " " ;
50
- return options[i].values .front ();
51
58
}
52
59
53
60
void cmdlinet::set (const std::string &option)
54
61
{
55
- int i=getoptnr (option);
56
- if (i<0 )
57
- return ; // ignore
58
- options[i].isset =true ;
62
+ auto i=getoptnr (option);
63
+
64
+ if (i.has_value ())
65
+ options[*i].isset =true ;
66
+
67
+ // otherwise ignore
59
68
}
60
69
61
70
void cmdlinet::set (const std::string &option, const std::string &value)
62
71
{
63
- int i=getoptnr (option);
64
- if (i<0 )
65
- return ; // ignore
66
- options[i].isset =true ;
67
- options[i].values .push_back (value);
72
+ auto i=getoptnr (option);
73
+
74
+ if (i.has_value ())
75
+ {
76
+ options[*i].isset =true ;
77
+ options[*i].values .push_back (value);
78
+ }
79
+
80
+ // otherwise ignore
68
81
}
69
82
70
83
static std::list<std::string> immutable_empty_list;
71
84
72
85
const std::list<std::string> &cmdlinet::get_values (char option) const
73
86
{
74
- int i=getoptnr (option);
75
- if (i<0 )
87
+ auto i=getoptnr (option);
88
+
89
+ if (i.has_value ())
90
+ return options[*i].values ;
91
+ else
76
92
return immutable_empty_list;
77
- return options[i].values ;
78
93
}
79
94
80
95
std::string cmdlinet::get_value (const char *option) const
81
96
{
82
- int i=getoptnr (option);
83
- if (i<0 )
84
- return " " ;
85
- if (options[i].values .empty ())
97
+ auto i=getoptnr (option);
98
+
99
+ if (i.has_value ())
100
+ {
101
+ if (options[*i].values .empty ())
102
+ return " " ;
103
+ else
104
+ return options[*i].values .front ();
105
+ }
106
+ else
86
107
return " " ;
87
- return options[i].values .front ();
88
108
}
89
109
90
110
const std::list<std::string> &cmdlinet::get_values (
91
111
const std::string &option) const
92
112
{
93
- int i=getoptnr (option);
94
- if (i<0 )
113
+ auto i=getoptnr (option);
114
+
115
+ if (i.has_value ())
116
+ return options[*i].values ;
117
+ else
95
118
return immutable_empty_list;
96
- return options[i].values ;
97
119
}
98
120
99
- int cmdlinet::getoptnr (char option) const
121
+ optionalt<std:: size_t > cmdlinet::getoptnr (char option) const
100
122
{
101
123
for (std::size_t i=0 ; i<options.size (); i++)
102
124
if (options[i].optchar ==option)
103
125
return i;
104
126
105
- return - 1 ;
127
+ return optionalt<std:: size_t >() ;
106
128
}
107
129
108
- int cmdlinet::getoptnr (const std::string &option) const
130
+ optionalt<std:: size_t > cmdlinet::getoptnr (const std::string &option) const
109
131
{
110
132
for (std::size_t i=0 ; i<options.size (); i++)
111
133
if (options[i].optstring ==option)
112
134
return i;
113
135
114
- return - 1 ;
136
+ return optionalt<std:: size_t >() ;
115
137
}
116
138
117
139
bool cmdlinet::parse (int argc, const char **argv, const char *optstring)
@@ -165,7 +187,7 @@ bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
165
187
args.push_back (argv[i]);
166
188
else
167
189
{
168
- int optnr;
190
+ optionalt<std:: size_t > optnr;
169
191
170
192
if (argv[i][1 ]!=0 && argv[i][2 ]==0 )
171
193
optnr=getoptnr (argv[i][1 ]); // single-letter option -X
@@ -177,29 +199,31 @@ bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
177
199
// We first try single-letter.
178
200
optnr=getoptnr (argv[i][1 ]);
179
201
180
- if (optnr< 0 ) // try multi-letter
202
+ if (! optnr. has_value () ) // try multi-letter
181
203
optnr=getoptnr (argv[i]+1 );
182
204
}
183
205
184
- if (optnr< 0 )
206
+ if (! optnr. has_value () )
185
207
{
186
208
unknown_arg=argv[i];
187
209
return true ;
188
210
}
189
- options[optnr].isset =true ;
190
- if (options[optnr].hasval )
211
+
212
+ options[*optnr].isset =true ;
213
+
214
+ if (options[*optnr].hasval )
191
215
{
192
- if (argv[i][2 ]==0 || options[optnr].islong )
216
+ if (argv[i][2 ]==0 || options[* optnr].islong )
193
217
{
194
218
i++;
195
219
if (i==argc)
196
220
return true ;
197
221
if (argv[i][0 ]==' -' && argv[i][1 ]!=0 )
198
222
return true ;
199
- options[optnr].values .push_back (argv[i]);
223
+ options[* optnr].values .push_back (argv[i]);
200
224
}
201
225
else
202
- options[optnr].values .push_back (argv[i]+2 );
226
+ options[* optnr].values .push_back (argv[i]+2 );
203
227
}
204
228
}
205
229
}
0 commit comments