-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (64 loc) · 2.26 KB
/
main.cpp
File metadata and controls
81 lines (64 loc) · 2.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
#include <pl/cli/cli.hpp>
#include <wolv/io/file.hpp>
#include <CLI/CLI.hpp>
#include <CLI/App.hpp>
#include <fmt/format.h>
// Available subcommands
namespace pl::cli {
namespace sub {
void addFormatSubcommand(CLI::App *app);
void addRunSubcommand(CLI::App *app);
void addDocsSubcommand(CLI::App *app);
void addInfoSubcommand(CLI::App *app);
void addLintSubcommand(CLI::App *app);
}
// Run the pattern language CLI
// first argument (args[0]) is the subcommand, not the executable name
int executeCommandLineInterface(std::vector<std::string> args) {
CLI::App app("Pattern Language CLI");
app.require_subcommand();
// Add subcommands
sub::addFormatSubcommand(&app);
sub::addRunSubcommand(&app);
sub::addDocsSubcommand(&app);
sub::addInfoSubcommand(&app);
sub::addLintSubcommand(&app);
// Print help message if not enough arguments were provided
if (args.size() == 0) {
fmt::print("{}", app.help());
return EXIT_FAILURE;
} else if (args.size() == 1) {
std::string subcommand = args[0];
if (subcommand == "-h" || subcommand == "--help") {
fmt::print("{}", app.help());
return EXIT_FAILURE;
} else if (subcommand == "--version") {
fmt::print("{}", "v1.0.0");
return EXIT_SUCCESS;
}
try {
fmt::print("{}\n", app.get_subcommand(subcommand)->help());
} catch (CLI::OptionNotFound &) {
fmt::print("Invalid subcommand '{}'\n", subcommand);
}
return EXIT_FAILURE;
}
// Parse command line input
try {
std::reverse(args.begin(), args.end()); // wanted by CLI11
app.parse(args);
} catch(const CLI::ParseError &e) {
return app.exit(e, std::cout, std::cout);
}
return EXIT_SUCCESS;
}
}
#if defined (LIBPL_CLI_AS_EXECUTABLE)
int main(int argc, char** argv) {
std::vector<std::string> args;
for (int i = 1; i < argc; i++) {
args.push_back(argv[i]);
}
return pl::cli::executeCommandLineInterface(args);
}
#endif