gpt4 book ai didi

c++ - boost::program_options - 验证失败时在错误消息中显示用户输入的值

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

我正在使用 boost program_options,但找不到指定异常消息以包含用户输入的值的方法,例如:

error: the argument for option '--ipc' is invalid: "shm"

我将用户输入的值传递给下面的调用,但这没有任何效果:

throw po::validation_error(po::validation_error::invalid_option_value, enteredValue);

错误信息仍然是这个:

error: the argument for option '--ipc' is invalid

这里是我的完整代码:

#include <iostream>
#include <vector>
#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>

using namespace std;

namespace po_style = boost::program_options::command_line_style;
namespace po = boost::program_options;

enum class Ipc : int8_t
{
TCP = 0,
UDP,
};

// Convert a log severity level to its name formatted as a string
// Enum cannot contain methods but you can add external methods like this one
static const char* to_string(const Ipc& id)
{
switch (id)
{
case Ipc::TCP: return "TCP";
case Ipc::UDP: return "UDP";
default: return "TCP";
}
}

// This function is called for arguments of the type Ipc
void validate(boost::any& v, const vector<string>& values, Ipc*, int)
{
po::validators::check_first_occurrence(v);
const string& enteredValue = po::validators::get_single_string(values);
const string& enteredValueUpper = boost::to_upper_copy(enteredValue);

if (enteredValueUpper == "TCP")
{
v = boost::any(Ipc::TCP);
}
else if (enteredValueUpper == "UPD")
{
v = boost::any(Ipc::UDP);
}
else
{
throw po::validation_error(po::validation_error::invalid_option_value, enteredValue);
}
}

int main(int argc, char* argv[])
{
try
{
// Declare all allowed options using the options_description class.
po::options_description desc("Usage");
desc.add_options()
("help,h", "produce help message")
("ipc,i", po::value<Ipc>()->default_value(Ipc::TCP, "TCP"), "set the inter-process communication");

po::variables_map cmdline;
po::store(po::command_line_parser(argc, argv)
.options(desc)
.style(po_style::unix_style | po_style::case_insensitive)
.run(), cmdline);
po::notify(cmdline);

// -i[--ipc] set the inter-process communication
if (cmdline.count("ipc"))
{
Ipc level = cmdline["ipc"].as<Ipc>();
cout << "ipc=" << to_string(level) << endl;
}

// -h[--help] produce help message
if (cmdline.count("help"))
{
cout << desc << "\n";
return 0;
}
}
catch (exception& e)
{
cerr << "error: " << e.what() << "\n";
return 1;
}
catch (...)
{
cerr << "Exception of unknown type!\n";
}

return 0;
}

最佳答案

我刚刚找到了一种解决方案,可以在验证失败时显示用户输入的值:

...
else
{
// ***************************************************
// SOLUTION FOUND BY Dan Mašek
// ***************************************************
throw po::invalid_option_value(enteredValue);



// This line does not display the enteredValue
//throw po::validation_error(po::validation_error::invalid_option_value, enteredValue);

// This works but hardcode the option name --ipc
//std::ostringstream ss;
//ss << "the argument for option '--ipc' is invalid: " << enteredValue;
//throw std::invalid_argument(ss.str());
}

它给出如下输出:

error: the argument for option '--ipc' is invalid: shm

我们可以做得更好吗(即不重复错误消息)?

关于c++ - boost::program_options - 验证失败时在错误消息中显示用户输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45821314/

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