我正在使用boost::program_options解析我的命令行。现在,我添加了对批处理执行的支持,通过表示文件的--script参数,在每一行都包含命令行选项,例如:
--src="\"z:\dev\veds\sqlexpress\Run 1.ved\"" --src-kind=bla --yz
--src=z:\dev\veds\sqlexpress\db.ebf
--src=z:\dev\veds\sqlexpress\db2.mdf
--src=db3
--src="\"z:\dev\veds\sqlite\Run 41 (Run 23).ved\""
--src=z:\dev\veds\sqlite\ws_results_db_2012_01_15_18_37_03.db3
--src=z:\dev\veds\mysql\10.ved
--src=z:\dev\veds\mysql\db文件中的每一行都表示我的工具的一次执行,并列出了用于此特定执行的命令行选项。
问题是,读取脚本文件会生成完整的行,这些行没有分解为单独的命令行选项。但是,必须有argc和argv才能使用boost::program_options,即需要有人将命令行拆分为不同的选项。
我不能简单地用空格分隔,因为有些值包含空格,因此它们用双引号括起来,甚至嵌套双引号。
另一方面,我不想从操作系统命令提示符为每一组命令行选项运行该工具,因为启动成本很高-这就是我首先引入脚本功能的原因。
有没有一种简单的方法可以像操作系统一样把行分成命令行参数?
谢谢。
发布于 2012-01-23 06:21:43
好的,我已经想好了。下面是我的代码:
string script;
{
ifstream file(scriptPath.c_str());
file.seekg(0, ios::end);
script.resize(file.tellg());
file.seekg(0, ios::beg);
file.read(const_cast<char *>(script.c_str()), script.size());
}
boost::replace_all(script, "\\", "\\\\"); // Escape the backslashes
boost::replace_all(script, "\\\\\"", "\\\""); // Except for those escaping the quotes
boost::trim_right_if(script, is_space_or_zero); // There are extra '\0' in the string, because the file is read as text, but its length was computed as binary
vector<string> lines;
boost::split(lines, script, boost::is_any_of("\n")); // I prefer getting a string line iterator here, the question is how?
escaped_list_separator<char> sep('\\', ' ', '"');
int res = 0;
BOOST_FOREACH (const string& line, lines)
{
// reset the command line variables here, since this is like a new execution
// Tokenize the command line, respecting escapes and quotes
tokenizer<escaped_list_separator<char>> tok(line, sep);
vector<string> args(tok.begin(), tok.end());
po::variables_map vm;
po::store(po::command_line_parser(args).options(options).run(), vm);
res += run(vm);
}我正在使用http://www.boost.org/doc/libs/1_48_0/libs/tokenizer/来打破这些限制。效果非常好。
发布于 2012-01-23 04:33:43
Boost文档介绍了Response Files,包括一个使用它们的简单示例。这将接近您想要的,除了他们还说它“有一些限制”,包括对空格的解析。
他们也有parse_config_file(),可以从文件中加载选项。在这里,您将放弃在文件中使用与命令行相同的语法,并且它们包含的实现将只(很容易地)支持每个程序调用一个“命令调用”。但我敢打赌,你可以看看他们是如何做到这一点的,并复制一些代码。如果我是你,我可能会调整它以支持.ini语法,如下所示:
[job1]
src=z:\dev\veds\sqlexpress\Run 1.ved
src-kind=bla
y=
z=
[job2]
src=z:\dev\veds\sqlexpress\db.ebf
[another_job]
src=z:\dev\veds\sqlexpress\db2.mdf我敢打赌,这不是一个可怕的额外工作量,而且它提供了一个额外的好处,即为您运行的每个作业都有一个明确的名称。Boost自己的parse_config_file()使用节名(在括号中)作为选项名称的前缀,但这不是必需的,因此为了保持简单的.ini语法,您也可以重新使用它们。
编辑:你想要更简单的东西吗?好的。放弃在文件中使用与命令行中引号和空格相同的语法的想法。如果必须在参数中支持空格,请决定在选项之间使用适当的分隔符,如;。使用std::string::find()或Boost Tokenizer将这样的代码转换为argc/argv对应该很容易:
--src=z:\dev\veds\sqlexpress\Run 1.ved; --src-kind=bla; --yz
--src=z:\dev\veds\sqlexpress\db.ebf
--src=z:\dev\veds\sqlexpress\db2.mdf在第一个示例中拆分;,生成argv1、2、3,并将程序自己的argv复制到“伪”argv中,然后使用Boost解析选项。
发布于 2012-01-23 06:04:30
看看这个::C++ Cookbook拆分字符串(菜谱4.6)
例4-10。拆分分隔字符串
#include <string>
#include <vector>
#include <functional>
#include <iostream>
using namespace std;
void split(const string& s, char c,
vector<string>& v) {
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length( )));
}
}
int main( ) {
vector<string> v;
string s = "Account Name|Address 1|Address 2|City";
split(s, '|', v);
for (int i = 0; i < v.size( ); ++i) {
cout << v[i] << '\n';
}
}--
template<typename T>
void split(const basic_string<T>& s, T c,
vector<basic_string<T> >& v) {
basic_string<T>::size_type i = 0;
basic_string<T>::size_type j = s.find(c);
while (j != basic_string<T>::npos) {
v.push_back(s.substr(i, j-i));
i = ++j;
j = s.find(c, j);
if (j == basic_string<T>::npos)
v.push_back(s.substr(i, s.length( )));
}
}例4-11。使用Boost拆分字符串
#include <iostream>
#include <string>
#include <list>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost;
int main( ) {
string s = "one,two,three,four";
list<string> results;
split(results, s, is_any_of(",")); // Note this is boost::split
for (list<string>::const_iterator p = results.begin( );
p != results.end( ); ++p) {
cout << *p << endl;
}
}--
template<typename Seq,
typename Coll,
typename Pred>
Seq& split(Seq& s, Coll& c, Pred p,
token_compress_mode_type e = token_compress_off);我不能随意分享文本(从书中复制/粘贴是非法的),但这些例子是相当有说服力的。如果你想看课文,你需要参考这本书。
这两个例子取自
C++食谱
作者: Jeff Cogswell,Christopher Diggins,Ryan Stephen,Jonathan Turkanis
出版商:O‘’Reilly
发布日期:2005年11月
ISBN: 0-596-00761-2
https://stackoverflow.com/questions/8964307
复制相似问题