]> git.stg.codes - stg.git/blob - projects/sgconf/options.cpp
Merge branch 'master' of gitorious.org:stg/stg
[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::ACTION;
33 using SGCONF::PARSER_STATE;
34
35 OPTION::OPTION(const std::string & shortName,
36                const std::string & longName,
37                ACTION * action,
38                const std::string & description)
39     : m_shortName(shortName),
40       m_longName(longName),
41       m_action(action),
42       m_description(description)
43 {
44 }
45
46 OPTION::OPTION(const std::string & longName,
47                ACTION * action,
48                const std::string & description)
49     : m_longName(longName),
50       m_action(action),
51       m_description(description)
52 {
53 }
54
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)
60 {
61 }
62
63 OPTION::~OPTION()
64 {
65 delete m_action;
66 }
67
68 void OPTION::Help(size_t level) const
69 {
70 if (!m_action)
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);
79 }
80
81 bool OPTION::Check(const char * arg) const
82 {
83 if (arg == NULL)
84     return false;
85
86 if (*arg++ != '-')
87     return false;
88
89 if (*arg == '-')
90     return m_longName == arg + 1;
91
92 return m_shortName == arg;
93 }
94
95 PARSER_STATE OPTION::Parse(int argc, char ** argv)
96 {
97 if (!m_action)
98     throw ERROR("Option is not defined.");
99 try
100     {
101     return m_action->Parse(argc, argv);
102     }
103 catch (const ACTION::ERROR & ex)
104     {
105     throw ERROR(m_longName + ": " + ex.what());
106     }
107 }
108
109 void OPTION_BLOCK::Add(const std::string & shortName,
110                        const std::string & longName,
111                        ACTION * action,
112                        const std::string & description)
113 {
114 m_options.push_back(OPTION(shortName, longName, action, description));
115 }
116
117 void OPTION_BLOCK::Add(const std::string & longName,
118                        ACTION * action,
119                        const std::string & description)
120 {
121 m_options.push_back(OPTION(longName, action, description));
122 }
123
124 void OPTION_BLOCK::Help(size_t level) const
125 {
126 std::for_each(m_options.begin(),
127               m_options.end(),
128               std::bind2nd(std::mem_fun_ref(&OPTION::Help), level));
129 }
130
131 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
132 {
133 PARSER_STATE state(false, argc, argv);
134 while (state.argc > 0 && !state.stop)
135     {
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);
139     else
140         break;
141     }
142 return state;
143 }