Repository navigation
Expand file tree
/
Copy pathbatch-learn.cpp
More file actions
104 lines (77 loc) · 2.54 KB
/
Copy pathbatch-learn.cpp
File metadata and controls
104 lines (77 loc) · 2.54 KB
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
#include "commands/convert.hpp"
#include "commands/ffm.hpp"
#include "commands/nn.hpp"
#include <unordered_map>
#include <iostream>
#include <string>
#include <memory>
static std::unordered_map<std::string, std::unique_ptr<command>> commands;
class help_command : public command {
protected:
std::string command_name;
public:
help_command() {
using namespace boost::program_options;
options_desc.add_options()
("command", value<std::string>(&command_name), "command name to get help about");
positional_options_desc.add("command", -1);
}
virtual std::string name() { return "help"; }
virtual std::string description() { return "get the help"; }
virtual int run() {
using namespace std;
if (commands.count(command_name) == 0) {
cout << "Supported commands:" << endl;
for (auto it = commands.begin(); it != commands.end(); ++ it) {
cout << " ";
cout.width(10);
cout << left << it->first << it->second->description() << endl;
}
} else {
commands[command_name]->print_help();
}
return 0;
}
};
int main(int ac, char* av[]) {
using namespace std;
// Prepare commands
commands.insert(make_pair("help", unique_ptr<command>(new help_command())));
commands.insert(make_pair("convert", unique_ptr<command>(new convert_command())));
commands.insert(make_pair("ffm", unique_ptr<command>(new ffm_command())));
commands.insert(make_pair("nn", unique_ptr<command>(new nn_command())));
// Check if command specified
if (ac <= 1) {
cout << "No command specified" << endl;
commands["help"]->run();
return -1;
}
// Extract command and check if it's supported
string cmd_name(av[1]);
if (commands.count(cmd_name) == 0) {
cout << "Unknown command " << cmd_name << " specified" << endl;
commands["help"]->run();
return -2;
}
auto & cmd = commands[cmd_name];
// Try to parse command options
try {
cmd->parse_options(ac - 1, av + 1);
} catch (const std::exception & e) {
cout << "Error: " << e.what() << endl;
cmd->print_help();
return -3;
}
// Print help if required
if (cmd->options_vm.count("help") > 0) {
cmd->print_help();
return 0;
}
// Run command
try {
return cmd->run();
} catch (std::exception & ex) {
cerr << endl << "Error: " << ex.what() << endl;
return -4;
}
}