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 if (m_longName.empty())
107 throw ERROR("-" + m_shortName + ": " + ex.what());
109 throw ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what());
113 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
114 const std::string & longName,
116 const std::string & description)
118 m_options.push_back(OPTION(shortName, longName, action, description));
122 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName,
124 const std::string & description)
126 m_options.push_back(OPTION(longName, action, description));
130 void OPTION_BLOCK::Help(size_t level) const
132 if (m_options.empty())
134 std::cout << m_description << ":\n";
135 std::for_each(m_options.begin(),
137 std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1));
140 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
142 PARSER_STATE state(false, argc, argv);
143 while (state.argc > 0 && !state.stop)
145 std::vector<OPTION>::iterator it = std::find_if(m_options.begin(), m_options.end(), std::bind2nd(std::mem_fun_ref(&OPTION::Check), *state.argv));
146 if (it != m_options.end())
147 state = it->Parse(--state.argc, ++state.argv);
155 void OPTION_BLOCKS::Help(size_t level) const
157 std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
158 while (it != m_blocks.end())
166 PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv)
168 PARSER_STATE state(false, argc, argv);
169 std::list<OPTION_BLOCK>::iterator it(m_blocks.begin());
170 while (!state.stop && it != m_blocks.end())
172 state = it->Parse(state.argc, state.argv);