-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathastro_tools_console_app.hpp
144 lines (118 loc) · 4.95 KB
/
astro_tools_console_app.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*****************************************************************************
*
* AstroTools
*
* Copyright(C) 2015 Carsten Schmitt <c.schmitt51h@gmail.com>
*
* This program is free software ; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
****************************************************************************/
#ifndef _ASTRO_TOOLS_CONSOLE_APP_HPP_
#define _ASTRO_TOOLS_CONSOLE_APP_HPP_ _ASTRO_TOOLS_CONSOLE_APP_HPP_
#include "astro_tools_app.hpp"
#include "at_exception.hpp"
namespace AT {
DEF_Exception(ATConsoleApp);
DEF_Exception(UnknownCommand);
class AstroToolsConsoleAppT : public AstroToolsAppT<AstroToolsConsoleAppT> {
public:
static const po::options_description getExtendedGeneralOptions() {
po::options_description additionalGeneralOptions;
additionalGeneralOptions.add_options()
("command", po::value<string>(), "Execute command.")
;
return additionalGeneralOptions;
}
static void printHelpNotice(ostream & os) {
os << "Type --help for more info." << endl;
}
static void printHelp(ostream & os) {
// Print general help
po::options_description allDescr;
allDescr.add(AstroToolsAppT::getGeneralOptions());
// Add plugin commands
for (CmdOptionsMapT::const_iterator it = AstroToolsAppT::getConsoleCmdMap().begin(); it != AstroToolsAppT::getConsoleCmdMap().end(); ++it) {
allDescr.add(it->second.first);
}
os << allDescr << endl;
}
static void evaluateCmdLineArguments(int argc, char **argv, po::variables_map * outCmdLineOptionsMap) {
AT_ASSERT(ATConsoleApp, outCmdLineOptionsMap, "Expect outCmdLineOptionsMap to be valid!");
// Compose command options from loaded plugins
po::options_description genericExtDescr;
genericExtDescr.add(AstroToolsAppT::getGeneralOptions());
po::positional_options_description pos;
pos.add("command", 1).add("hidden", -1); // This is a hack to ignore options which are unknown at that time.
// Possible exceptions: invalid_command_line_syntax, multiple_occurrences, invalid_option_value.
// For now we handle all the same way.
try {
po::store(po::command_line_parser(argc, argv).options(genericExtDescr).allow_unregistered().positional(pos).run(), *outCmdLineOptionsMap);
po::notify(*outCmdLineOptionsMap);
} catch(std::exception & exc) {
cerr << "CASE1..." << endl;
printHelpNotice(cout);
throw;
}
const string & cmd = (outCmdLineOptionsMap->count("command") ? (*outCmdLineOptionsMap)["command"].as<string>() : "");
CmdOptionsMapT::const_iterator itCmd = AstroToolsAppT::getConsoleCmdMap().find(cmd);
po::options_description allowedDescr;
allowedDescr.add(AstroToolsAppT::getGeneralOptions());
if (! cmd.empty()) {
if (itCmd == AstroToolsAppT::getConsoleCmdMap().end()) {
const string exStr = "Command '" + cmd + "' is unknown.";
throw UnknownCommandExceptionT(exStr.c_str());
} else {
// Just allow the options for the selected command...
allowedDescr.add(itCmd->second.first);
}
}
sSelectedCmd = cmd;
// Special case - if user typed help,
if (outCmdLineOptionsMap->count("help"))
return;
try {
store(parse_command_line(argc, argv, allowedDescr), *outCmdLineOptionsMap);
notify(*outCmdLineOptionsMap);
} catch(std::exception & exc) {
cerr << "CASE2..." << endl;
printHelpNotice(cout);
throw;
}
}
static void init(int argc, char **argv) {
AstroToolsAppT::init(argc, argv);
}
static void execute(int argc, char **argv) {
CmdOptionsMapT::const_iterator itCmd = AstroToolsAppT::getConsoleCmdMap().find(sSelectedCmd);
// Handle help...
if (getCmdLineOptionsMap().count("help") || sSelectedCmd.empty()) {
if (sSelectedCmd.empty()) {
printHelp(cout);
} else {
cout << "Help for '" << sSelectedCmd << "'" << endl;
cout << itCmd->second.first << endl;
}
} else {
if (itCmd != AstroToolsAppT::getConsoleCmdMap().end()) {
AT_ASSERT(ATConsoleApp, itCmd->second.second, "Function pointer expected to be valid.");
LOG(info) << "Executing command " << sSelectedCmd << "... (function-ptr: " << hex << (void*)itCmd->second.second << ")" << flush;
itCmd->second.second();
LOG(info) << "DONE!" << endl;
}
}
}
};
}; // end namespace AT
#endif // _ASTRO_TOOLS_CONSOLE_APP_HPP_