gpt4 book ai didi

c++ - 使用 Boost Program Options 解析任意字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:35 27 4
gpt4 key购买 nike

我想在我的程序中实现类似命令行的界面。所以我收到的字符串遵循正常的命令行语法(例如“-G foo -dp bar --help”)。由于我不想再次实现解析器,所以我想使用 Boost。

问题是:如何将字符串传递给 Boost 程序选项而不是 argCount 和 argValues 的组合。我是否需要先将文本转换为数字 (argCount) 和 char* 数组 (argValues) 才能完成?如果是……有没有简单的方法来做到这一点?

提前致谢。

最佳答案

一种方法是标记化 std::string进入 std::vector<std::string> ,然后将结果传递给 Boost.ProgramOption 的 command_line_parser . Boost.ProgramOption 的 documentation简要介绍了这种方法。此外,我在 this 的一部分中使用了类似的方法回答。

这是一个最小的完整示例:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

#include <boost/bind.hpp>
#include <boost/program_options.hpp>
#include <boost/tokenizer.hpp>

// copy_if was left out of the C++03 standard, so mimic the C++11
// behavior to support all predicate types. The alternative is to
// use remove_copy_if, but it only works for adaptable functors.
template <typename InputIterator,
typename OutputIterator,
typename Predicate>
OutputIterator
copy_if(InputIterator first,
InputIterator last,
OutputIterator result,
Predicate pred)
{
while(first != last)
{
if(pred(*first))
*result++ = *first;
++first;
}
return result;
}

/// @brief Tokenize a string. The tokens will be separated by each non-quoted
/// space or equal character. Empty tokens are removed.
///
/// @param input The string to tokenize.
///
/// @return Vector of tokens.
std::vector<std::string> tokenize(const std::string& input)
{
typedef boost::escaped_list_separator<char> separator_type;
separator_type separator("\\", // The escape characters.
"= ", // The separator characters.
"\"\'"); // The quote characters.

// Tokenize the intput.
boost::tokenizer<separator_type> tokens(input, separator);

// Copy non-empty tokens from the tokenizer into the result.
std::vector<std::string> result;
copy_if(tokens.begin(), tokens.end(), std::back_inserter(result),
!boost::bind(&std::string::empty, _1));
return result;
}

int main()
{
// Variables that will store parsed values.
std::string address;
unsigned int port;

// Setup options.
namespace po = boost::program_options;
po::options_description desc("Options");
desc.add_options()
("address", po::value<std::string>(&address))
("port", po::value<unsigned int>(&port))
;

// Mock up input.
std::string input = "--address 127.0.0.1 --port 12345";

// Parse mocked up input.
po::variables_map vm;
po::store(po::command_line_parser(tokenize(input))
.options(desc).run(), vm);
po::notify(vm);

// Output.
std::cout << "address = " << address << "\n"
"port = " << port << std::endl;
}

产生以下输出:

address = 127.0.0.1
port = 12345

关于c++ - 使用 Boost Program Options 解析任意字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378798/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com