]> git.stg.codes - stg.git/blob - projects/sgconf/options.cpp
Added assignment operator for SGCONF::OPTION.
[stg.git] / projects / sgconf / options.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "options.h"
22
23 #include "action.h"
24 #include "parser_state.h"
25
26 #include <iostream>
27 #include <functional>
28 #include <algorithm>
29
30 using SGCONF::OPTION;
31 using SGCONF::OPTION_BLOCK;
32 using SGCONF::OPTION_BLOCKS;
33 using SGCONF::ACTION;
34 using SGCONF::PARSER_STATE;
35
36 OPTION::OPTION(const std::string & shortName,
37                const std::string & longName,
38                ACTION * action,
39                const std::string & description)
40     : m_shortName(shortName),
41       m_longName(longName),
42       m_action(action),
43       m_description(description)
44 {
45 }
46
47 OPTION::OPTION(const std::string & longName,
48                ACTION * action,
49                const std::string & description)
50     : m_longName(longName),
51       m_action(action),
52       m_description(description)
53 {
54 }
55
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)
61 {
62 }
63
64 OPTION::~OPTION()
65 {
66 delete m_action;
67 }
68
69 OPTION & OPTION::operator=(const OPTION & rhs)
70 {
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;
75 return *this;
76 }
77
78 void OPTION::Help(size_t level) const
79 {
80 if (!m_action)
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);
89 }
90
91 bool OPTION::Check(const char * arg) const
92 {
93 if (arg == NULL)
94     return false;
95
96 if (*arg++ != '-')
97     return false;
98
99 if (*arg == '-')
100     return m_longName == arg + 1;
101
102 return m_shortName == arg;
103 }
104
105 PARSER_STATE OPTION::Parse(int argc, char ** argv)
106 {
107 if (!m_action)
108     throw ERROR("Option is not defined.");
109 try
110     {
111     return m_action->Parse(argc, argv);
112     }
113 catch (const ACTION::ERROR & ex)
114     {
115     if (m_longName.empty())
116         throw ERROR("-" + m_shortName + ": " + ex.what());
117     else
118         throw ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what());
119     }
120 }
121
122 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
123                                  const std::string & longName,
124                                  ACTION * action,
125                                  const std::string & description)
126 {
127 m_options.push_back(OPTION(shortName, longName, action, description));
128 return *this;
129 }
130
131 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName,
132                                  ACTION * action,
133                                  const std::string & description)
134 {
135 m_options.push_back(OPTION(longName, action, description));
136 return *this;
137 }
138
139 void OPTION_BLOCK::Help(size_t level) const
140 {
141 if (m_options.empty())
142     return;
143 std::cout << m_description << ":\n";
144 std::for_each(m_options.begin(),
145               m_options.end(),
146               std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1));
147 }
148
149 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
150 {
151 PARSER_STATE state(false, argc, argv);
152 while (state.argc > 0 && !state.stop)
153     {
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);
157     else
158         break;
159     ++it;
160     }
161 return state;
162 }
163
164 void OPTION_BLOCKS::Help(size_t level) const
165 {
166 std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
167 while (it != m_blocks.end())
168     {
169     it->Help(level);
170     std::cout << "\n";
171     ++it;
172     }
173 }
174
175 PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv)
176 {
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())
180     {
181     state = it->Parse(state.argc, state.argv);
182     ++it;
183     }
184 return state;
185 }