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

标签 c++ boost boost-program-options

我正在使用 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/

相关文章:

c++ - 提升程序选项 : does custom validator require overloading operator>>?

c++ - 如果在 SQUARE 宏中传递前缀变量,为什么结果不同?

c++ - Node native 模块 - 链接静态库

c++ - "QuantLib::MulticurvesSensitivities:performCalculations() const' 的 quantlib 多重定义

c++ - Boost 程序选项语法

c++ - 我迷失在 boost 库中(特别是 boost_program_options)

c++ - 如何加快这个盒子堆叠变化?

c++ - OpenGL-从外部文件绘制点

c++ - jpeg 全尺寸解码速度最快的解码器是什么?

c++ - Linux 应用程序在 boost::thread::join 上卡住