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 void OPTION::Help(size_t level) const
72 throw ERROR("Option is not defined.");
73 std::string indent(level, '\t');
74 std::cout << indent << "\t";
75 if (!m_shortName.empty())
76 std::cout << "-" << m_shortName << ", ";
77 std::cout << "--" << m_longName << " " << m_action->ParamDescription()
78 << "\t" << m_description << m_action->DefaultDescription() << "\n";
79 m_action->Suboptions().Help(level + 1);
82 bool OPTION::Check(const char * arg) const
91 return m_longName == arg + 1;
93 return m_shortName == arg;
96 PARSER_STATE OPTION::Parse(int argc, char ** argv)
99 throw ERROR("Option is not defined.");
102 return m_action->Parse(argc, argv);
104 catch (const ACTION::ERROR & ex)
106 throw ERROR(m_longName + ": " + ex.what());
110 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
111 const std::string & longName,
113 const std::string & description)
115 m_options.push_back(OPTION(shortName, longName, action, description));
119 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName,
121 const std::string & description)
123 m_options.push_back(OPTION(longName, action, description));
127 void OPTION_BLOCK::Help(size_t level) const
129 if (m_options.empty())
131 std::cout << m_description << ":\n";
132 std::for_each(m_options.begin(),
134 std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1));
137 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
139 PARSER_STATE state(false, argc, argv);
140 while (state.argc > 0 && !state.stop)
142 std::vector<OPTION>::iterator it = std::find_if(m_options.begin(), m_options.end(), std::bind2nd(std::mem_fun_ref(&OPTION::Check), *state.argv));
143 if (it != m_options.end())
144 state = it->Parse(--state.argc, ++state.argv);
152 void OPTION_BLOCKS::Help(size_t level) const
154 std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
155 while (it != m_blocks.end())
163 PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv)
165 PARSER_STATE state(false, argc, argv);
166 std::list<OPTION_BLOCK>::iterator it(m_blocks.begin());
167 while (!state.stop && it != m_blocks.end())
169 state = it->Parse(state.argc, state.argv);