载入中...
搜索中...
未找到
cmdline.cpp
浏览该文件的文档.
1#include "cmdline/cmdline.h"
2#include "common/config.h"
3
4#include <CLI11.hpp>
5#include <iostream>
6#include <rang.hpp>
7#include <simplesquirrel/simplesquirrel.hpp>
8
9using namespace std;
10
11namespace eve::cmd {
12
14
15std::vector<std::function<Handler*()>>& Cmdline::handers() {
16 static std::vector<std::function<Handler*()>> cmds = {};
17 return cmds;
18}
19
20void Cmdline::registerCmd(std::function<Handler*()> handler) { handers().push_back(handler); }
21
22class MyFormatter : public CLI::Formatter {
23public:
24 std::string make_usage(const CLI::App* app, std::string name) const override {
25 std::string usage = "Usage: ";
26 usage += name;
27 usage += " [Options] [Root Folder]";
28 return usage;
29 }
30};
31
32
33Cmdline::Cmdline() : argc(0), argv(nullptr) {}
34
35void Cmdline::expose(ssq::Table& table) {
36 auto cls = table.addClass(name, Cmdline::create, false);
37 expose(cls);
38}
39
40void Cmdline::expose(ssq::Class& cls) { cls.addFunc("getName", &Cmdline::getName); }
41
42int Cmdline::runArgs(unsigned argc, char** argv) {
43 CLI::App app{"EVEngine - A Modern Game Engine"};
44 bool version = false;
45 app.add_flag("-v,--version", version, "Print version string");
46 auto formatter = std::make_shared<MyFormatter>();
47
48 std::vector<Handler*> handles;
49 for (auto cmd : handers()) {
50 auto* p = cmd();
51 handles.push_back(p);
52 p->setup(app, formatter);
53 }
54
55 try {
56 this->argc = argc;
57 this->argv = argv;
58 app.parse(argc, argv);
59 } catch (const CLI::ParseError& e) {
60 for (auto* h : handles) delete h;
61 int res = app.exit(e);
62 return res;
63 }
64
65 int res = 0;
66 for (auto* h : handles) {
67 if (int r = h->parse(app, *this); r != -1) {
68 res = r;
69 break;
70 }
71 }
72 if (version) cout << EVENGINE_VERSION << endl;
73
74 for (auto* h : handles) delete h;
75 return res;
76}
77
78
79std::string Cmdline::get_remaining(CLI::App* sub, std::string default_value) {
80 auto paths = sub->remaining();
81 if (paths.size() > 1) {
82 cerr << rang::fg::red << "Unknown remaining arguments: " << rang::fg::reset << paths[1] << endl;
83 exit(1);
84 }
85 if (paths.size() == 0) {
86 return default_value;
87 }
88 return paths[0];
89}
90
91
92} // namespace eve::cmd
HSQOBJECT cls
Definition ECS.cpp:21
int h
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:24
glm::vec4 p[6]
const char * name
Definition RockMesh.cpp:21
virtual std::string getName() const =0
命令行模块(eve.cmd):run / build / package / test / zip / dev-server 等子命令入口。
Definition cmdline.h:30
static std::string get_remaining(CLI::App *sub, std::string default_path=".")
子命令剩余位置参数。
Definition cmdline.cpp:79
unsigned argc
Definition cmdline.h:85
static std::vector< std::function< Handler *()> > & handers()
Definition cmdline.cpp:15
int runArgs(unsigned argc, char **argv)
解析并执行传入的 argv。
Definition cmdline.cpp:42
static void registerCmd(std::function< Handler *()> handler)
Definition cmdline.cpp:20
std::string make_usage(const CLI::App *app, std::string name) const override
Definition cmdline.cpp:24
One eve <subcommand> handler: CLI setup + argument parsing.
Definition cmdline.h:21