-
Notifications
You must be signed in to change notification settings - Fork 26
/
subcommand.h
263 lines (220 loc) · 7.4 KB
/
subcommand.h
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright (c) 2013, Robert Escriva
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of this project nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#ifndef e_subcommand_h_
#define e_subcommand_h_
// C
#include <stdlib.h>
#include <string.h>
// POSIX
#include <errno.h>
#include <unistd.h>
// C++
#include <iostream>
#include <iomanip>
// e
#include <e/popt.h>
namespace e
{
struct subcommand
{
subcommand(const char* n, const char* d) : name(n), description(d) {}
const char* name;
const char* description;
};
namespace subcommands
{
int
help(const char* cmd, e::argparser* ap, subcommand* commands, size_t commands_sz)
{
ap->help();
size_t max_command_sz = 0;
for (size_t i = 0; i < commands_sz; ++i)
{
size_t command_sz = strlen(commands[i].name);
max_command_sz = std::max(max_command_sz, command_sz);
}
size_t pad = ((max_command_sz + 3ULL) & ~3ULL) + 4;
std::cout << std::setfill(' ') << "\nAvailable commands:\n";
for (size_t i = 0; i < commands_sz; ++i)
{
std::cout << " " << std::left << std::setw(pad)
<< commands[i].name
<< commands[i].description << "\n";
}
std::cout << "\nSee '" << cmd << " help <command>' for more information on a specific command." << std::endl;
return EXIT_FAILURE;
}
} // subcommands
int
dispatch_to_subcommands(int arg, const char* argv[],
const char* cmd,
const char* name,
const char* version,
const char* prefix,
const char* env_var,
const char* default_path,
subcommand* commands,
size_t commands_sz)
{
bool flag_help = false;
bool flag_usage = false;
bool flag_version = false;
bool flag_completion = false;
const char* arg_path = NULL;
const char* orig_argv0 = argv[0];
e::argparser help_ap;
help_ap.option_string("[COMMAND] [ARGS]");
help_ap.arg().name('?', "help")
.description("Show this help message")
.set_true(&flag_help);
help_ap.arg().long_name("usage")
.description("Display brief usage message")
.set_true(&flag_usage);
help_ap.arg().long_name("version")
.description("Print the version and exit")
.set_true(&flag_version);
e::argparser global_ap;
global_ap.arg().long_name("exec-path")
.description("Path to where the subcommands are installed")
.metavar("PATH")
.as_string(&arg_path);
e::argparser ap;
ap.option_string("[OPTIONS] <command> [<args>]");
ap.add("Help options:", help_ap);
ap.add("Global options:", global_ap);
ap.arg().long_name("dump-completion")
.set_true(&flag_completion)
.hidden();
if (!ap.parse(arg, argv))
{
return EXIT_FAILURE;
}
if (flag_help)
{
return subcommands::help(cmd, &ap, commands, commands_sz);
}
if (flag_usage)
{
ap.usage();
return EXIT_FAILURE;
}
if (flag_version)
{
std::cout << name << " version " << version << std::endl;
return EXIT_SUCCESS;
}
if (flag_completion)
{
std::cout << "Pretend this is a bash completion script\n"; // XXX
return EXIT_SUCCESS;
}
if (ap.args_sz() == 0)
{
return subcommands::help(cmd, &ap, commands, commands_sz);
}
// add the path where the binaries should be, preferring cmd-line over
// environment over default.
std::string path;
char* env_path = getenv(env_var);
if (arg_path)
{
path = arg_path;
}
else if (env_path)
{
path = env_path;
}
else
{
path = default_path;
}
// add the dir where "hyperdex" resides
path += ":" + std::string(po6::path::dirname(orig_argv0));
// add the existing PATH
char* old_path = getenv("PATH");
if (old_path)
{
path += ":" + std::string(old_path);
}
// set the path
if (setenv("PATH", path.c_str(), 1) < 0)
{
std::cerr << "could not set path: " << strerror(errno) << std::endl;
return EXIT_FAILURE;
}
// dispatch the command
if (strcmp(ap.args()[0], "help") == 0)
{
if (ap.args_sz() > 1)
{
std::string exec(prefix);
exec += ap.args()[1];
std::vector<const char*> args;
args.push_back("man");
args.push_back(exec.c_str());
args.push_back(NULL);
if (execvp(args[0], const_cast<char* const*>(&args.front())) < 0)
{
std::cerr << "failed to exec \"man\" to show help: " << strerror(errno) << std::endl;
return EXIT_FAILURE;
}
abort();
}
else
{
return subcommands::help(cmd, &ap, commands, commands_sz);
}
}
for (size_t i = 0; i < commands_sz; ++i)
{
subcommand* s = commands + i;
if (strcmp(s->name, ap.args()[0]) == 0)
{
std::string exec(prefix);
exec += s->name;
std::vector<const char*> args;
args.push_back(exec.c_str());
for (size_t j = 1; j < ap.args_sz(); ++j)
{
args.push_back(ap.args()[j]);
}
args.push_back(NULL);
if (execvp(args[0], const_cast<char* const*>(&args.front())) < 0)
{
std::cerr << "failed to exec " << s->name << ": " << strerror(errno) << std::endl;
std::cerr << "PATH=" << path << std::endl;
return EXIT_FAILURE;
}
abort();
}
}
std::cerr << "\"" << ap.args()[0] << "\" is not a " << name
<< " command. See \"" << cmd << " --help\"\n" << std::endl;
return subcommands::help(cmd, &ap, commands, commands_sz);
}
} // namespace e
#endif // e_subcommand_h_