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;
 
  33 using SGCONF::PARSER_STATE;
 
  35 OPTION::OPTION(const std::string & shortName,
 
  36                const std::string & longName,
 
  38                const std::string & description)
 
  39     : m_shortName(shortName),
 
  42       m_description(description)
 
  46 OPTION::OPTION(const std::string & longName,
 
  48                const std::string & description)
 
  49     : m_longName(longName),
 
  51       m_description(description)
 
  55 OPTION::OPTION(const OPTION & rhs)
 
  56     : m_shortName(rhs.m_shortName),
 
  57       m_longName(rhs.m_longName),
 
  58       m_action(rhs.m_action),
 
  59       m_description(rhs.m_description)
 
  68 void OPTION::Help(size_t level) const
 
  71     throw ERROR("Option is not defined.");
 
  72 std::string indent(level, '\t');
 
  73 std::cout << indent << "\t";
 
  74 if (!m_shortName.empty())
 
  75     std::cout << "-" << m_shortName << ", ";
 
  76 std::cout << "--" << m_longName << " " << m_action->ParamDescription()
 
  77           << "\t" << m_description << m_action->DefaultDescription() << "\n";
 
  78 m_action->Suboptions().Help(level + 1);
 
  81 bool OPTION::Check(const char * arg) const
 
  90     return m_longName == arg + 1;
 
  92 return m_shortName == arg;
 
  95 PARSER_STATE OPTION::Parse(int argc, char ** argv)
 
  98     throw ERROR("Option is not defined.");
 
 101     return m_action->Parse(argc, argv);
 
 103 catch (const ACTION::ERROR & ex)
 
 105     throw ERROR(m_longName + ": " + ex.what());
 
 109 void OPTION_BLOCK::Add(const std::string & shortName,
 
 110                        const std::string & longName,
 
 112                        const std::string & description)
 
 114 m_options.push_back(OPTION(shortName, longName, action, description));
 
 117 void OPTION_BLOCK::Add(const std::string & longName,
 
 119                        const std::string & description)
 
 121 m_options.push_back(OPTION(longName, action, description));
 
 124 void OPTION_BLOCK::Help(size_t level) const
 
 126 std::for_each(m_options.begin(),
 
 128               std::bind2nd(std::mem_fun_ref(&OPTION::Help), level));
 
 131 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
 
 133 PARSER_STATE state(false, argc, argv);
 
 134 while (state.argc > 0 && !state.stop)
 
 136     std::vector<OPTION>::iterator it = std::find_if(m_options.begin(), m_options.end(), std::bind2nd(std::mem_fun_ref(&OPTION::Check), *state.argv));
 
 137     if (it != m_options.end())
 
 138         state = it->Parse(--state.argc, ++state.argv);