]> git.stg.codes - stg.git/blob - projects/sgconf/options.h
More std::jthread
[stg.git] / projects / sgconf / options.h
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 #pragma once
22
23 #include <string>
24 #include <vector>
25 #include <utility>
26 #include <stdexcept>
27 #include <memory>
28 #include <cstddef> // size_t
29
30 namespace SGCONF
31 {
32
33 class ACTION;
34 struct PARSER_STATE;
35
36 class OPTION
37 {
38     public:
39         OPTION(const std::string & shortName,
40                const std::string & longName,
41                std::unique_ptr<ACTION> action,
42                const std::string & description);
43         OPTION(const std::string & longName,
44                std::unique_ptr<ACTION> action,
45                const std::string & description);
46
47         OPTION(OPTION&& rhs) = default;
48         OPTION& operator=(OPTION& rhs) = default;
49
50         void Help(size_t level = 0) const;
51         PARSER_STATE Parse(int argc, char ** argv, void * data);
52         void ParseValue(const std::string & value);
53         bool Check(const char * arg) const;
54         const std::string & Name() const { return m_longName; }
55
56         struct ERROR : std::runtime_error
57         {
58             explicit ERROR(const std::string & message)
59                 : std::runtime_error(message.c_str()) {}
60         };
61
62     private:
63         std::string m_shortName;
64         std::string m_longName;
65         std::unique_ptr<ACTION> m_action;
66         std::string m_description;
67 };
68
69 class OPTION_BLOCK
70 {
71     public:
72         OPTION_BLOCK() {}
73         explicit OPTION_BLOCK(const std::string & description)
74             : m_description(description) {}
75
76         OPTION_BLOCK(OPTION_BLOCK&&) = default;
77         OPTION_BLOCK& operator=(OPTION_BLOCK&&) = default;
78
79         OPTION_BLOCK & Add(const std::string & shortName,
80                            const std::string & longName,
81                            std::unique_ptr<ACTION> action,
82                            const std::string & description);
83         OPTION_BLOCK & Add(const std::string & longName,
84                            std::unique_ptr<ACTION> action,
85                            const std::string & description);
86
87         void Help(size_t level) const;
88
89         PARSER_STATE Parse(int argc, char ** argv, void * data = NULL);
90         void ParseFile(const std::string & filePath);
91
92         struct ERROR : std::runtime_error
93         {
94             explicit ERROR(const std::string & message)
95                 : std::runtime_error(message.c_str()) {}
96         };
97
98     private:
99         std::vector<OPTION> m_options;
100         std::string m_description;
101
102         void OptionCallback(const std::string & key, const std::string & value);
103 };
104
105 class OPTION_BLOCKS
106 {
107     public:
108         OPTION_BLOCK & Add(const std::string & description)
109         { m_blocks.push_back(OPTION_BLOCK(description)); return m_blocks.back(); }
110         void Add(OPTION_BLOCK&& block) { m_blocks.push_back(std::move(block)); }
111         void Help(size_t level) const;
112         PARSER_STATE Parse(int argc, char ** argv);
113
114     private:
115         std::vector<OPTION_BLOCK> m_blocks;
116 };
117
118 } // namespace SGCONF