-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.cpp
More file actions
120 lines (104 loc) · 4.26 KB
/
cli.cpp
File metadata and controls
120 lines (104 loc) · 4.26 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <dancli/cli.h>
#include <iostream>
#include <string>
#include <memory>
namespace po = boost::program_options;
dancli::CLI::CLI(std::string program_name, std::string program_description)
{
this->program_name = program_name;
this->program_description = program_description;
this->global_options = std::make_unique<po::options_description>("Global options");
this->global_options->add_options()
("debug", "Turn on debug output")
("help,h", "show help menu")
("command", po::value<std::string>(), "command to execute")
("args", po::value<std::vector<std::string>>()->default_value(std::vector<std::string>(), ""), "Arguments for command");
}
int dancli::CLI::run(int argc, char *argv[]) {
po::positional_options_description pos;
pos.add("command", 1).
add("args", -1);
this->global_parsed = std::make_unique<boost::program_options::parsed_options>(po::command_line_parser(argc, argv).
options(*this->global_options).
positional(pos).
allow_unregistered().
run());
po::store(*this->global_parsed, this->global_vm);
return this->process();
}
void dancli::CLI::usage() {
std::cout << this->program_name << " - " << this->program_description << std::endl;
std::cout << std::endl;
std::cout << "usage:" << std::endl;
std::cout << this->program_name << " ";
for (auto& i : this->global_options->options()) {
std::string option_name = i->long_name();
std::string option_format_name = i->format_name();
std::string option_format_parameter = i->format_parameter();
if (option_name != "command" && option_name != "args") {
std::cout << "[" << option_format_name;
if (!option_format_parameter.empty()) {
std::cout << " " << option_format_parameter;
}
std::cout << "] ";
}
}
std::cout << "<command> [<command-options>] [<args>]" << std::endl;
std::cout << std::endl;
std::cout << "commands:" << std::endl;
std::cout << "help - show help menu" << std::endl;
for (auto& i : this->commands) {
std::cout << i->command_name << " - " << i->command_description << std::endl;
}
std::cout << std::endl;
std::cout << "options:" << std::endl;
for (auto& i : this->global_options->options()) {
std::string option_name = i->long_name();
std::string option_format_name = i->format_name();
std::string option_format_parameter = i->format_parameter();
if (option_name != "command" && option_name != "args") {
std::cout << option_format_name;
if (!option_format_parameter.empty()) {
std::cout << " " << option_format_parameter;
}
std::cout << " - " << i->description() << std::endl;
}
}
}
int dancli::CLI::process() {
bool help = this->global_vm.count("help") > 0;
std::string command;
if (this->global_vm.count("command") > 0) {
command = this->global_vm["command"].as<std::string>();
}
if (!command.empty()) {
if (command == "help") {
this->usage();
return 0;
}
for (auto& i : this->commands) {
if (i->command_name == command) {
if (help) {
i->usage();
return 0;
} else {
i->parse(*this->global_parsed, this->global_vm);
i->run();
return 0;
}
}
}
// If we get to this point, no valid command was found
this->usage();
std::cerr << std::endl << "Error: Unknown command '" << command << "'" << std::endl;
return 1;
}
if (help) {
this->usage();
} else {
// If we get to this point, no command was entered
// TODO Allow user to set the default action that occurs when no command was entered
this->usage();
}
return 0;
}