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();
3838string GetAppBuildTime ();
3939void 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 ) {
0 commit comments