2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
24 #include "parser_state.h"
31 using SGCONF::OPTION_BLOCK;
32 using SGCONF::OPTION_BLOCKS;
34 using SGCONF::PARSER_STATE;
36 OPTION::OPTION(const std::string & shortName,
37 const std::string & longName,
39 const std::string & description)
40 : m_shortName(shortName),
43 m_description(description)
47 OPTION::OPTION(const std::string & longName,
49 const std::string & description)
50 : m_longName(longName),
52 m_description(description)
56 OPTION::OPTION(const OPTION & rhs)
57 : m_shortName(rhs.m_shortName),
58 m_longName(rhs.m_longName),
59 m_action(rhs.m_action->Clone()),
60 m_description(rhs.m_description)
69 OPTION & OPTION::operator=(const OPTION & rhs)
71 m_shortName = rhs.m_shortName;
72 m_longName = rhs.m_longName;
73 m_action = rhs.m_action->Clone();
74 m_description = rhs.m_description;
78 void OPTION::Help(size_t level) const
81 throw ERROR("Option is not defined.");
82 std::string indent(level, '\t');
83 std::cout << indent << "\t";
84 if (!m_shortName.empty())
85 std::cout << "-" << m_shortName << ", ";
86 std::cout << "--" << m_longName << " " << m_action->ParamDescription()
87 << "\t" << m_description << m_action->DefaultDescription() << "\n";
88 m_action->Suboptions().Help(level + 1);
91 bool OPTION::Check(const char * arg) const
100 return m_longName == arg + 1;
102 return m_shortName == arg;
105 PARSER_STATE OPTION::Parse(int argc, char ** argv)
108 throw ERROR("Option is not defined.");
111 return m_action->Parse(argc, argv);
113 catch (const ACTION::ERROR & ex)
115 if (m_longName.empty())
116 throw ERROR("-" + m_shortName + ": " + ex.what());
118 throw ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what());
122 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
123 const std::string & longName,
125 const std::string & description)
127 m_options.push_back(OPTION(shortName, longName, action, description));
131 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName,
133 const std::string & description)
135 m_options.push_back(OPTION(longName, action, description));
139 void OPTION_BLOCK::Help(size_t level) const
141 if (m_options.empty())
143 std::cout << m_description << ":\n";
144 std::for_each(m_options.begin(),
146 std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1));
149 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
151 PARSER_STATE state(false, argc, argv);
152 while (state.argc > 0 && !state.stop)
154 std::vector<OPTION>::iterator it = std::find_if(m_options.begin(), m_options.end(), std::bind2nd(std::mem_fun_ref(&OPTION::Check), *state.argv));
155 if (it != m_options.end())
156 state = it->Parse(--state.argc, ++state.argv);
164 void OPTION_BLOCKS::Help(size_t level) const
166 std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
167 while (it != m_blocks.end())
175 PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv)
177 PARSER_STATE state(false, argc, argv);
178 std::list<OPTION_BLOCK>::iterator it(m_blocks.begin());
179 while (!state.stop && it != m_blocks.end())
181 state = it->Parse(state.argc, state.argv);