@@ -11,8 +11,14 @@ Author: Daniel Kroening
1111
1212#include " armcc_cmdline.h"
1313
14+ #include < util/optional.h>
15+ #include < util/prefix.h>
16+
17+ #include < algorithm>
1418#include < cstring>
1519#include < iostream>
20+ #include < string>
21+ #include < vector>
1622
1723// / parses the command line options into a cmdlinet
1824// / \par parameters: argument count, argument strings
@@ -195,7 +201,8 @@ static const char *options_no_arg[]=
195201 nullptr
196202};
197203
198- static const char *options_with_prefix[]=
204+ // clang-format off
205+ static const std::vector<std::string> options_with_prefix
199206{
200207 " --project=" ,
201208 " --workdir=" ,
@@ -241,11 +248,10 @@ static const char *options_with_prefix[]=
241248 " --configure_sysroot=" ,
242249 " --configure_cpp_headers=" ,
243250 " --configure_extra_includes=" ,
244- " --configure_extra_libraries=" ,
245- nullptr
251+ " --configure_extra_libraries="
246252};
247253
248- static const char * options_with_arg[]=
254+ static const std::vector<std::string> options_with_arg
249255{
250256 // goto-cc specific
251257 " --verbosity" ,
@@ -261,9 +267,21 @@ static const char *options_with_arg[]=
261267 " -Warmcc," ,
262268 " -o" ,
263269 " --cpu" ,
264- " --apcs" ,
265- nullptr
270+ " --apcs"
266271};
272+ // clang-format on
273+
274+ optionalt<std::string>
275+ prefix_in_list (const std::string &option, const std::vector<std::string> &list)
276+ {
277+ const auto found =
278+ std::find_if (list.cbegin (), list.cend (), [&](const std::string &argument) {
279+ return has_prefix (argument, option);
280+ });
281+ if (found == list.cend ())
282+ return {};
283+ return {*found};
284+ }
267285
268286bool armcc_cmdlinet::parse (int argc, const char **argv)
269287{
@@ -277,35 +295,34 @@ bool armcc_cmdlinet::parse(int argc, const char **argv)
277295 }
278296
279297 // it starts with - and it isn't "-"
280-
281- std::string prefix;
298+ optionalt<std::string> prefix;
282299
283300 if (in_list (argv[i], options_no_arg))
284301 {
285302 // options that don't have any arguments
286303 set (argv[i]);
287304 }
288- else if (prefix_in_list (argv[i], options_with_arg, prefix ))
305+ else if ((prefix = prefix_in_list (argv[i], options_with_arg) ))
289306 {
290307 // options that have a separated _or_ concatenated argument
291- if (strlen (argv[i])> prefix. size ()) // concatenated?
292- set (prefix, std::string (argv[i], prefix. size (), std::string::npos));
308+ if (strlen (argv[i]) > prefix-> size ()) // Concatenated.
309+ set (* prefix, std::string (argv[i], prefix-> size (), std::string::npos));
293310 else
294311 {
295312 // Separated.
296313 if (i!=argc-1 ) // Guard against end of command line.
297314 {
298- set (prefix, argv[i+ 1 ]);
315+ set (* prefix, argv[i + 1 ]);
299316 i++;
300317 }
301318 else
302- set (prefix, " " );
319+ set (* prefix, " " );
303320 }
304321 }
305- else if (prefix_in_list (argv[i], options_with_prefix, prefix ))
322+ else if ((prefix = prefix_in_list (argv[i], options_with_prefix) ))
306323 {
307324 // options that have a concatenated argument
308- set (prefix, std::string (argv[i], prefix. size (), std::string::npos));
325+ set (* prefix, std::string (argv[i], prefix-> size (), std::string::npos));
309326 }
310327 else
311328 { // unrecognized option
0 commit comments