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