Skip to content

Commit bb09532

Browse files
committed
feat(main):1.13.4, 支持--args参数
1 parent fecafdd commit bb09532

File tree

10 files changed

+75
-32
lines changed

10 files changed

+75
-32
lines changed

modules/main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ set(TBOX_MAIN_SOURCES
4141
error_signals.cpp
4242
terminate.cpp
4343
misc.cpp
44-
args.cpp
44+
args_parser.cpp
4545
module.cpp
4646
log.cpp
4747
trace.cpp)

modules/main/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ CPP_SRC_FILES = \
3636
error_signals.cpp \
3737
terminate.cpp \
3838
misc.cpp \
39-
args.cpp \
39+
args_parser.cpp \
4040
module.cpp \
4141
log.cpp \
4242
trace.cpp \

modules/main/args.cpp renamed to modules/main/args_parser.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* project authors may be found in the CONTRIBUTORS.md file in the root
1818
* of the source tree.
1919
*/
20-
#include "args.h"
20+
#include "args_parser.h"
2121

2222
#include <iostream>
2323
#include <fstream>
@@ -38,11 +38,12 @@ string GetAppDescribe();
3838
string GetAppBuildTime();
3939
void GetAppVersion(int &major, int &minor, int &rev, int &build);
4040

41-
Args::Args(Json &conf) :
42-
conf_(conf)
41+
ArgsParser::ArgsParser(Json &conf, Args &args)
42+
: conf_(conf)
43+
, args_(args)
4344
{ }
4445

45-
bool Args::parse(int argc, const char * const * const argv)
46+
bool ArgsParser::parse(int argc, const char * const * const argv)
4647
{
4748
bool run = true; //!< 是否需要正常运行
4849
bool print_help = false; //!< 是否需要打印帮助
@@ -97,7 +98,20 @@ bool Args::parse(int argc, const char * const * const argv)
9798
}
9899
);
99100

100-
parser.parse(argc, argv);
101+
//! 将 --args 后面的参数全部复制到 args_ 中去
102+
int args_pos = 0;
103+
for (int i = 1; i < argc; ++i) {
104+
const char* str = argv[i];
105+
if (args_pos != 0) {
106+
args_.push_back(str);
107+
} else if (::strcmp(str, "--args") == 0) {
108+
args_pos = i;
109+
args_.push_back(proc_name);
110+
}
111+
}
112+
113+
auto tbox_argc = (args_pos != 0) ? args_pos : argc;
114+
parser.parse(tbox_argc, argv);
101115

102116
if (print_tips)
103117
printTips(proc_name);
@@ -114,12 +128,12 @@ bool Args::parse(int argc, const char * const * const argv)
114128
return run;
115129
}
116130

117-
void Args::printTips(const std::string &proc_name)
131+
void ArgsParser::printTips(const std::string &proc_name)
118132
{
119133
cout << "Try '" << proc_name << " --help' for more information." << endl;
120134
}
121135

122-
void Args::printHelp(const std::string &proc_name)
136+
void ArgsParser::printHelp(const std::string &proc_name)
123137
{
124138
cout << "Usage: " << proc_name << " [OPTION]" << endl
125139
<< GetAppDescribe() << endl << endl
@@ -142,7 +156,7 @@ void Args::printHelp(const std::string &proc_name)
142156
<< endl;
143157
}
144158

145-
void Args::printVersion()
159+
void ArgsParser::printVersion()
146160
{
147161
int major, minor, rev, build;
148162
GetTboxVersion(major, minor, rev);
@@ -154,7 +168,7 @@ void Args::printVersion()
154168

155169
}
156170

157-
bool Args::load(const std::string &config_filename)
171+
bool ArgsParser::load(const std::string &config_filename)
158172
{
159173
try {
160174
auto js_patch = util::json::LoadDeeply(config_filename);
@@ -195,7 +209,7 @@ Json& BuildJsonByKey(const std::string &key, Json &js_root)
195209
return *p_node;
196210
}
197211

198-
bool Args::set(const std::string &set_string)
212+
bool ArgsParser::set(const std::string &set_string)
199213
{
200214
vector<string> str_vec;
201215
if (util::string::Split(set_string, "=", str_vec) != 2) {

modules/main/args.h renamed to modules/main/args_parser.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
* of the source tree.
1919
*/
2020
/**
21-
* 本文件定义 Args
21+
* 本文件定义 ArgsParser
2222
*
23-
* Args 类用于解释程序的命令参数,输出程序运行的配置Json数据。
23+
* ArgsParser 类用于解释程序的命令参数,输出程序运行的配置Json数据。
2424
*
2525
* 参数:
2626
*
@@ -41,17 +41,18 @@
4141
* myproc -v
4242
* myproc --version
4343
*/
44-
#ifndef TBOX_MAIN_ARGS_H_20211229
45-
#define TBOX_MAIN_ARGS_H_20211229
44+
#ifndef TBOX_MAIN_ARGS_PARSER_H_20211229
45+
#define TBOX_MAIN_ARGS_PARSER_H_20211229
4646

4747
#include <tbox/base/json_fwd.h>
48+
#include "context.h"
4849

4950
namespace tbox {
5051
namespace main {
5152

52-
class Args {
53+
class ArgsParser {
5354
public:
54-
Args(Json &conf);
55+
ArgsParser(Json &conf, Args &args);
5556

5657
/**
5758
* 解析参数
@@ -74,9 +75,10 @@ class Args {
7475

7576
private:
7677
Json &conf_;
78+
Args &args_;
7779
};
7880

7981
}
8082
}
8183

82-
#endif //TBOX_MAIN_ARGS_H_20211229
84+
#endif //TBOX_MAIN_ARGS_PARSER_H_20211229

modules/main/context.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
namespace tbox {
3333
namespace main {
3434

35+
using Args = std::vector<std::string>;
36+
3537
//! 进程上下文
3638
class Context {
3739
public:
@@ -44,6 +46,8 @@ class Context {
4446

4547
virtual std::chrono::milliseconds running_time() const = 0;
4648
virtual std::chrono::system_clock::time_point start_time_point() const = 0;
49+
50+
virtual const Args& args() const = 0;
4751
};
4852

4953
}

modules/main/context_imp.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ void ContextImp::fillDefaultConfig(Json &cfg) const
111111
cfg["thread_pool"] = R"({"min":0, "max":1})"_json;
112112
}
113113

114-
bool ContextImp::initialize(const char* proc_name, const Json &cfg, Module *module)
114+
bool ContextImp::initialize(const char* proc_name, const Json &cfg, const Args &args, Module *module)
115115
{
116116
TBOX_ASSERT(module != nullptr);
117117
module_ = module;
118118
js_conf_ = &cfg;
119+
wp_args_ = &args;
119120

120121
if (util::json::HasObjectField(cfg, "loop")) {
121122
auto &js_loop = cfg["loop"];
@@ -484,9 +485,9 @@ void ContextImp::initShell()
484485
}
485486
}
486487

488+
auto dump_node = wp_nodes->createDirNode("dump directory");
489+
wp_nodes->mountNode(wp_nodes->rootNode(), dump_node, "dump");
487490
{
488-
auto dump_node = wp_nodes->createDirNode("dump directory");
489-
wp_nodes->mountNode(wp_nodes->rootNode(), dump_node, "dump");
490491
auto func_node = wp_nodes->createFuncNode(
491492
[this] (const Session &s, const Args &a) {
492493
Json js;
@@ -506,6 +507,21 @@ void ContextImp::initShell()
506507
wp_nodes->mountNode(dump_node, func_node, "apps");
507508
wp_nodes->mountNode(dump_node, func_node, "conf");
508509
}
510+
{
511+
auto func_node = wp_nodes->createFuncNode(
512+
[this] (const Session &s, const Args &) {
513+
std::ostringstream oss;
514+
auto &args = *wp_args_;
515+
for (size_t i = 0; i < args.size(); ++i)
516+
oss << '[' << i << "]:" << args.at(i) << "\r\n";
517+
oss << "\r\n";
518+
s.send(oss.str());
519+
},
520+
"Dump args"
521+
);
522+
523+
wp_nodes->mountNode(dump_node, func_node, "args");
524+
}
509525
}
510526

511527
}

modules/main/context_imp.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ContextImp : public Context {
4040

4141
void fillDefaultConfig(Json &cfg) const;
4242

43-
bool initialize(const char *proc_name, const Json &cfg, Module *module);
43+
bool initialize(const char *proc_name, const Json &cfg, const Args &args, Module *module);
4444
bool start();
4545
void stop();
4646
void cleanup();
@@ -56,6 +56,8 @@ class ContextImp : public Context {
5656
virtual std::chrono::milliseconds running_time() const override;
5757
virtual std::chrono::system_clock::time_point start_time_point() const override;
5858

59+
virtual const Args& args() const override { return *wp_args_; }
60+
5961
protected:
6062
bool initLoop(const Json &js);
6163
bool initThreadPool(const Json &js);
@@ -81,6 +83,7 @@ class ContextImp : public Context {
8183

8284
const Module *module_ = nullptr;
8385
const Json *js_conf_ = nullptr;
86+
const Args *wp_args_ = nullptr;
8487
};
8588

8689
}

modules/main/run_in_backend.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#include "module.h"
3434
#include "context_imp.h"
35-
#include "args.h"
35+
#include "args_parser.h"
3636
#include "log.h"
3737
#include "trace.h"
3838

@@ -58,6 +58,7 @@ struct Runtime {
5858
ContextImp ctx;
5959
Module apps;
6060
Json js_conf;
61+
Args args;
6162

6263
util::PidFile pid_file;
6364
int exit_wait_sec = 1;
@@ -127,8 +128,9 @@ bool Start(int argc, char **argv)
127128
auto &ctx = _runtime->ctx;
128129
auto &apps = _runtime->apps;
129130
auto &js_conf = _runtime->js_conf;
131+
auto &args = _runtime->args;
130132

131-
Args args(js_conf);
133+
ArgsParser args_parser(js_conf, args);
132134
Trace trace;
133135

134136
log.fillDefaultConfig(js_conf);
@@ -138,7 +140,7 @@ bool Start(int argc, char **argv)
138140
FillAppDefaultConfig(js_conf);
139141
apps.fillDefaultConfig(js_conf);
140142

141-
if (!args.parse(argc, argv))
143+
if (!args_parser.parse(argc, argv))
142144
return false;
143145

144146
std::string pid_filename;
@@ -171,7 +173,7 @@ bool Start(int argc, char **argv)
171173
std::this_thread::sleep_for(std::chrono::seconds(1));
172174
};
173175

174-
if (ctx.initialize(argv[0], js_conf, &apps)) {
176+
if (ctx.initialize(argv[0], js_conf, args, &apps)) {
175177
if (apps.initialize(js_conf)) {
176178
if (ctx.start()) { //! 启动所有应用
177179
if (apps.start()) {

modules/main/run_in_frontend.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#include "module.h"
3434
#include "context_imp.h"
35-
#include "args.h"
35+
#include "args_parser.h"
3636
#include "log.h"
3737
#include "trace.h"
3838

@@ -110,8 +110,10 @@ int Main(int argc, char **argv)
110110
Module apps("", ctx);
111111
RegisterApps(apps, ctx);
112112

113+
Args args;
113114
Json js_conf;
114-
Args args(js_conf);
115+
ArgsParser args_parser(js_conf, args);
116+
115117
Trace trace;
116118

117119
log.fillDefaultConfig(js_conf);
@@ -121,7 +123,7 @@ int Main(int argc, char **argv)
121123
FillAppDefaultConfig(js_conf);
122124
apps.fillDefaultConfig(js_conf);
123125

124-
if (!args.parse(argc, argv))
126+
if (!args_parser.parse(argc, argv))
125127
return 0;
126128

127129
std::string pid_filename;
@@ -158,7 +160,7 @@ int Main(int argc, char **argv)
158160
std::this_thread::sleep_for(std::chrono::seconds(1));
159161
};
160162

161-
if (ctx.initialize(argv[0], js_conf, &apps)) {
163+
if (ctx.initialize(argv[0], js_conf, args, &apps)) {
162164
if (apps.initialize(js_conf)) {
163165
if (ctx.start() && apps.start()) { //! 启动所有应用
164166
RunInFrontend(ctx, apps, exit_wait_sec);

version.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
# TBOX版本号
2222
TBOX_VERSION_MAJOR := 1
2323
TBOX_VERSION_MINOR := 13
24-
TBOX_VERSION_REVISION := 3
24+
TBOX_VERSION_REVISION := 4

0 commit comments

Comments
 (0)