99
1010#include < vector>
1111#include < string>
12- #include < regex>
1312#include < cstring>
1413#include < cassert>
1514#include < cstdlib>
@@ -35,18 +34,30 @@ string Args::mergeArgs(int argc, char **argv) {
3534vector<KeyVal> Args::splitArgLine (const string& inputLine) {
3635 vector<KeyVal> ret;
3736
38- // The line must be ended with at least one space for the regex to function correctly.
39- string line = inputLine + ' ' ;
40- std::regex rx (" \\ s*(-+\\ w+)\\ s+([^-]\\ S*)?\\ s*([^-]*)" );
41- for (std::sregex_iterator it (line.begin (), line.end (), rx); it != std::sregex_iterator (); ++it) {
42- smatch m = *it;
43- // printf("'%s' '%s' '%s'\n", m.str(1).c_str(), m.str(2).c_str(), m.str(3).c_str());
44- string prefix = m.prefix ().str ();
45- string suffix = m.str (3 );
46- if (!prefix.empty ()) { log (" Args: unexpected '%s' before '%s'\n " , prefix.c_str (), m.str (0 ).c_str ()); }
47- if (!suffix.empty ()) { log (" Args: unexpected '%s' in '%s'\n " , suffix.c_str (), m.str (0 ).c_str ()); }
48- if (!prefix.empty () || !suffix.empty ()) { throw " Argument syntax" ; }
49- ret.push_back ({m.str (1 ), m.str (2 )});
37+ string prev;
38+ for (const string& s : split (inputLine, ' ' )) {
39+ if (s.empty ()) { continue ; }
40+
41+ if (prev.empty ()) {
42+ if (s[0 ] != ' -' ) {
43+ log (" Args: expected '-' before '%s'\n " , s.c_str ());
44+ throw " Argument syntax" ;
45+ }
46+
47+ prev = s;
48+ } else {
49+ if (s[0 ] == ' -' ) {
50+ ret.push_back ({prev, {}});
51+ prev = s;
52+ } else {
53+ ret.push_back ({prev, s});
54+ prev.clear ();
55+ }
56+ }
57+ }
58+ if (!prev.empty ()) {
59+ assert (prev[0 ] == ' -' );
60+ ret.push_back ({prev, {}});
5061 }
5162 return ret;
5263}
0 commit comments