-
Notifications
You must be signed in to change notification settings - Fork 0
/
subcommands.cpp
60 lines (51 loc) · 1.75 KB
/
subcommands.cpp
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
#include <iostream>
// #define CLIPP_DEBUG
#include "clipp.hpp"
struct ParentArgs : public clipp::ArgsBase {
std::optional<std::string> device;
std::string command;
void args()
{
flag(device, "device", 'd').help("Which device to start the system on");
positional(command, "command").choices({ "start", "stop" }).halt();
}
};
struct StartArgs : public clipp::ArgsBase {
std::optional<std::string> power;
std::string system;
void args()
{
flag(power, "power", 'p').help("With how much power to start the system");
positional(system, "system").help("The system to start");
}
};
struct StopArgs : public clipp::ArgsBase {
bool force;
std::string system;
void args()
{
flag(force, "force", 'f').help("Force stopping of system");
positional(system, "system").help("The system to stop");
}
};
int main(int argc, char** argv)
{
auto parser = clipp::Parser(argv[0]);
const auto args = parser.parse<ParentArgs>(argc, argv).value();
if (args.device) {
std::cout << "Device: " << *args.device << std::endl;
}
auto subParser = clipp::Parser(std::string(argv[0]) + " " + args.command);
if (args.command == "start") {
const auto subArgs = subParser.parse<StartArgs>(args.remaining()).value();
if (subArgs.power) {
std::cout << "power: " << *subArgs.power << std::endl;
}
std::cout << "Starting system: " << subArgs.system << std::endl;
} else if (args.command == "stop") {
const auto subArgs = subParser.parse<StopArgs>(args.remaining()).value();
std::cout << "force: " << subArgs.force << std::endl;
std::cout << "Stopping system: " << subArgs.system << std::endl;
}
return 0;
}