简体   繁体   English

Echo在Boost中输入了命令行参数

[英]Echo entered command line parameters in Boost

I am using boost::program_options to parse command line parameters. 我使用boost :: program_options来解析命令行参数。 I would like my program to echo the entered parameters back to the user for verification. 我希望我的程序将输入的参数回显给用户进行验证。 This sounds like a simple task but I was unable to find an elegant solution. 这听起来像一个简单的任务,但我无法找到一个优雅的解决方案。

The problem is that the user can enter all sorts of data types (strings, integer, boolean values, etc...). 问题是用户可以输入各种数据类型(字符串,整数,布尔值等)。 Usually this is handled nicely by boost. 通常这可以通过boost很好地处理。 But I have troubles casting the values back to strings in order to echo them back to the user. 但是我将这些值转换回字符串时遇到了麻烦,以便将它们反馈给用户。

Here is what I am currently doing 这是我目前正在做的事情

// set up program options
po::options_description optdesc("Allowed options");
optdesc.add_options()
    ("help", "Produces this help screen")
    ("opt1", po::value<string>(), "Option 1")
    ("opt2", po::value<int>(), "Option 2")
    ("opt3", po::value<bool>(), "Option 3);

// parse command line
try
{
    po::store(po::parse_command_line(argc, argv, optdesc), cmdline);
    po::notify(cmdline);
}

// do error handling ...

// echo parameters back to user
for (po::variables_map::iterator it = cmdline.begin(); it != cmdline.end(); ++it)
{
    boost::any arg = it->second.value();
    if (typeid(string) == arg.type())
    {
         cout << "  " << it->first << ":  " << boost::any_cast<string>(it->second.value()) << endl;
    }
    else if (typeid(int) == arg.type())
    {
         cout << "  " << it->first << ":  " << boost::any_cast<int>(it->second.value()) << endl;
    }
    // etc...

I really don't like this solution. 我真的不喜欢这个解决方案。 Since Boost is able to convert the user input from a string to the appropiate value it should also be able to convert the value back to a string representation without me explicitly testing for the data type. 由于Boost能够将用户输入从字符串转换为适当的值,因此它还应该能够将值转换回字符串表示,而无需我明确地测试数据类型。

Is this possible? 这可能吗? If yes, how can I do that. 如果是的话,我该怎么办呢。

Thanks 谢谢

You already have argv and argc , just echo them back to the user 您已经拥有argvargc ,只需将它们回显给用户即可

#include <iostream>
#include <iterator>

int
main(int argc, char* argv[])
{
    std::cout << "got " << argc << " args" << std::endl;
    std::copy(argv + 1, argv + argc, std::ostream_iterator<char*>(std::cout, " "));
    std::cout << std::endl;
}

produces 产生

samm:stackoverflow samm$ ./a.out asdf 1 2 3 hello world
got 7 args
asdf 1 2 3 hello world 
samm:stackoverflow samm$

There's no need to iterate through the variables_map and cast each entry. 没有必要遍历variables_map并转换每个条目。

I faced similar problem and this is my solution. 我遇到了类似的问题, 是我的解决方案。 In brief you have to write your own container for options based on boost::variant, not on boots::any (on which boost::po is based). 简而言之,你必须为boost :: variant创建自己的容器,而不是在boots :: any(boost :: po所基于的)上。 This container can be filled via notifiers of boost::po options. 可以通过boost :: po选项的通知程序填充此容器。 This will allow you to use the Visitor pattern for your options and to process them in a generic way. 这将允许您使用访问者模式作为选项并以通用方式处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM