]> git.stg.codes - stg.git/blob - projects/sgconf/options.cpp
Implemented command line options parsing.
[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 void OPTION::Help(size_t level) const
70 {
71 if (!m_action)
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);
80 }
81
82 bool OPTION::Check(const char * arg) const
83 {
84 if (arg == NULL)
85     return false;
86
87 if (*arg++ != '-')
88     return false;
89
90 if (*arg == '-')
91     return m_longName == arg + 1;
92
93 return m_shortName == arg;
94 }
95
96 PARSER_STATE OPTION::Parse(int argc, char ** argv)
97 {
98 if (!m_action)
99     throw ERROR("Option is not defined.");
100 try
101     {
102     return m_action->Parse(argc, argv);
103     }
104 catch (const ACTION::ERROR & ex)
105     {
106     throw ERROR(m_longName + ": " + ex.what());
107     }
108 }
109
110 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName,
111                                  const std::string & longName,
112                                  ACTION * action,
113                                  const std::string & description)
114 {
115 m_options.push_back(OPTION(shortName, longName, action, description));
116 return *this;
117 }
118
119 OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName,
120                                  ACTION * action,
121                                  const std::string & description)
122 {
123 m_options.push_back(OPTION(longName, action, description));
124 return *this;
125 }
126
127 void OPTION_BLOCK::Help(size_t level) const
128 {
129 if (m_options.empty())
130     return;
131 std::cout << m_description << ":\n";
132 std::for_each(m_options.begin(),
133               m_options.end(),
134               std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1));
135 }
136
137 PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv)
138 {
139 PARSER_STATE state(false, argc, argv);
140 while (state.argc > 0 && !state.stop)
141     {
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);
145     else
146         break;
147     ++it;
148     }
149 return state;
150 }
151
152 void OPTION_BLOCKS::Help(size_t level) const
153 {
154 std::list<OPTION_BLOCK>::const_iterator it(m_blocks.begin());
155 while (it != m_blocks.end())
156     {
157     it->Help(level);
158     std::cout << "\n";
159     ++it;
160     }
161 }
162
163 PARSER_STATE OPTION_BLOCKS::Parse(int argc, char ** argv)
164 {
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())
168     {
169     state = it->Parse(state.argc, state.argv);
170     ++it;
171     }
172 return state;
173 }