scrollpad: explicitely include iostream
[ncmpcpp.git] / src / utility / option_parser.cpp
blob21f4cbd6eeaa3998e672297cf3084ba788a06cbd
1 /*
2 * Copyright (c) 2014, Andrzej Rybczak <electricityispower@gmail.com>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
34 #include <iostream>
36 #include "utility/option_parser.h"
38 bool option_parser::run(std::istream &is)
40 // quoted value. leftmost and rightmost quotation marks are the delimiters.
41 boost::regex quoted("(\\w+)\\h*=\\h*\"(.*)\"[^\"]*");
42 // unquoted value. whitespaces get trimmed.
43 boost::regex unquoted("(\\w+)\\h*=\\h*(.*?)\\h*");
44 boost::smatch match;
45 std::string line;
46 while (std::getline(is, line))
48 if (boost::regex_match(line, match, quoted)
49 || boost::regex_match(line, match, unquoted))
51 std::string option = match[1];
52 auto it = m_parsers.find(option);
53 if (it != m_parsers.end())
55 try {
56 it->second.parse(match[2]);
57 } catch (std::exception &e) {
58 std::cerr << "Error while processing option \"" << option << "\": " << e.what() << "\n";
59 return false;
62 else
64 std::cerr << "Unknown option: " << option << "\n";
65 return false;
69 for (auto &p : m_parsers)
71 if (!p.second.defined())
73 try {
74 p.second.run_default();
75 } catch (std::exception &e) {
76 std::cerr << "Error while finalizing option \"" << p.first << "\": " << e.what() << "\n";
77 return false;
81 return true;
84 option_parser::worker yes_no(bool &arg, bool value)
86 return option_parser::worker([&arg](std::string &&v) {
87 if (v == "yes")
88 arg = true;
89 else if (v == "no")
90 arg = false;
91 else
92 throw std::runtime_error("invalid argument: " + v);
93 }, defaults_to(arg, value));