2 * Copyright (c) 2014-2016, Andrzej Rybczak <electricityispower@gmail.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <boost/regex.hpp>
36 #include "utility/option_parser.h"
38 bool yes_no(const std::string
&v
)
48 ////////////////////////////////////////////////////////////////////////////////
50 bool option_parser::run(std::istream
&is
, bool ignore_errors
)
52 // quoted value. leftmost and rightmost quotation marks are the delimiters.
53 boost::regex
quoted("(\\w+)\\h*=\\h*\"(.*)\"[^\"]*");
54 // unquoted value. whitespaces get trimmed.
55 boost::regex
unquoted("(\\w+)\\h*=\\h*(.*?)\\h*");
58 while (std::getline(is
, line
))
60 if (boost::regex_match(line
, match
, quoted
)
61 || boost::regex_match(line
, match
, unquoted
))
63 std::string option
= match
[1];
64 auto it
= m_parsers
.find(option
);
65 if (it
!= m_parsers
.end())
69 it
->second
.parse(match
[2]);
71 catch (std::exception
&e
)
73 std::cerr
<< "Error while processing option \"" << option
74 << "\": " << e
.what() << "\n";
81 std::cerr
<< "Unknown option: " << option
<< "\n";
90 bool option_parser::initialize_undefined(bool ignore_errors
)
92 for (auto &pp
: m_parsers
)
101 catch (std::exception
&e
)
103 std::cerr
<< "Error while initializing option \"" << pp
.first
104 << "\": " << e
.what() << "\n";