Skip to content

Commit 6321bbc

Browse files
hannes-steffenhagen-diffblueHannes Steffenhagen
authored and
Hannes Steffenhagen
committed
Move optstring parsing into its own separate function
1 parent 0f57d77 commit 6321bbc

File tree

2 files changed

+46
-40
lines changed

2 files changed

+46
-40
lines changed

src/util/cmdline.cpp

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -155,46 +155,7 @@ bool cmdlinet::parse(int argc, const char **argv, const char *optstring)
155155
{
156156
clear();
157157

158-
while(optstring[0]!=0)
159-
{
160-
optiont option;
161-
162-
DATA_INVARIANT(
163-
optstring[0] != ':', "cmdlinet::parse: Invalid option string\n");
164-
165-
if(optstring[0]=='(')
166-
{
167-
option.islong=true;
168-
option.optchar=0;
169-
option.isset=false;
170-
option.optstring.clear();
171-
172-
for(optstring++; optstring[0]!=')' && optstring[0]!=0; optstring++)
173-
option.optstring+=optstring[0];
174-
175-
if(optstring[0]==')')
176-
optstring++;
177-
}
178-
else
179-
{
180-
option.islong=false;
181-
option.optchar=optstring[0];
182-
option.optstring.clear();
183-
option.isset=false;
184-
185-
optstring++;
186-
}
187-
188-
if(optstring[0]==':')
189-
{
190-
option.hasval=true;
191-
optstring++;
192-
}
193-
else
194-
option.hasval=false;
195-
196-
options.push_back(option);
197-
}
158+
parse_optstring(optstring);
198159

199160
for(int i=1; i<argc; i++)
200161
{
@@ -250,6 +211,49 @@ cmdlinet::option_namest cmdlinet::option_names() const
250211
{
251212
return option_namest{*this};
252213
}
214+
void cmdlinet::parse_optstring(const char *optstring)
215+
{
216+
while(optstring[0] != 0)
217+
{
218+
optiont option;
219+
220+
DATA_INVARIANT(
221+
optstring[0] != ':', "cmdlinet::parse: Invalid option string\n");
222+
223+
if(optstring[0] == '(')
224+
{
225+
option.islong = true;
226+
option.optchar = 0;
227+
option.isset = false;
228+
option.optstring.clear();
229+
230+
for(optstring++; optstring[0] != ')' && optstring[0] != 0; optstring++)
231+
option.optstring += optstring[0];
232+
233+
if(optstring[0] == ')')
234+
optstring++;
235+
}
236+
else
237+
{
238+
option.islong = false;
239+
option.optchar = optstring[0];
240+
option.optstring.clear();
241+
option.isset = false;
242+
243+
optstring++;
244+
}
245+
246+
if(optstring[0] == ':')
247+
{
248+
option.hasval = true;
249+
optstring++;
250+
}
251+
else
252+
option.hasval = false;
253+
254+
options.push_back(option);
255+
}
256+
}
253257

254258
std::vector<std::string>
255259
cmdlinet::get_argument_suggestions(const std::string &unknown_argument)

src/util/cmdline.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class cmdlinet
115115
{}
116116
};
117117

118+
void parse_optstring(const char *optstring);
119+
118120
std::vector<optiont> options;
119121

120122
optionalt<std::size_t> getoptnr(char option) const;

0 commit comments

Comments
 (0)