7

我正在使用 boost::program_options 来处理程序的命令行参数。在下面的程序中,我希望将算法、交换和 admin_port 组合在一起,以便它们都应该提供,否则会引发异常(即,除非它们在一起,否则没有意义)。

我还想以一种可以明显看出它们是一组的方式将它们打印出来。

如何最好地实现这一目标?

#include <boost/program_options.hpp>
#include <cassert>
#include <iostream>
#include <string>

namespace prog_opts = boost::program_options;

int main(int argc, char *argv[])
{
    int rc = 0;

    prog_opts::options_description desc("Usage");
    desc.add_options()
        ("algo", prog_opts::value<std::string>(), "Name of the algo to run")
        ("exchanges", prog_opts::value< std::vector<std::string> >(), "Name(s) of the exchanges which will be available for use")
        ("admin_port", prog_opts::value<unsigned>(), "Admin port on which admin requests will be listened for")
        ("version", "Show version information")
        ("help", "Show help information");

    prog_opts::variables_map args;

    try
    {
        prog_opts::store(prog_opts::parse_command_line(argc, argv, desc), args);
        prog_opts::notify(args);

        if(args.count("algo") && args.count("exchanges") && args.count("admin_port"))
        {
            //TODO:
        }
        else if(args.count("version"))
        {
            //TODO:
        }
        else if(args.count("help"))
        {
            std::cout << desc << std::endl;
        }
        else
        {
            std::cerr << desc << std::endl;
            rc = 1;
        }  
    }
    catch(const prog_opts::error& e)
    {
        std::cerr << "Failed start with given command line arguments: " << e.what() << std::endl;
        rc = 1;
    }

    return rc;
}  
4

2 回答 2

1

要创建强制组,您可以使用

prog_opts::value<std::string>()->required()

在程序的选项描述部分。这看起来像这样:

#include <boost/program_options.hpp>
#include <cassert>
#include <iostream>
#include <string>

namespace prog_opts = boost::program_options;

int main(int argc, char *argv[])
{
int rc = 0;

prog_opts::options_description desc("Usage");
desc.add_options()
    ("algo", prog_opts::value<std::string>()->required(), "Name of the algo to run")
    ("exchanges", prog_opts::value< std::vector<std::string> >()->required(), "Name(s) of the exchanges which will be available for use")
    ("admin_port", prog_opts::value<unsigned>()->required(), "Admin port on which admin requests will be listened for")
    ("version", "Show version information")
    ("help", "Show help information");

prog_opts::variables_map args;

try
{
    prog_opts::store(prog_opts::parse_command_line(argc, argv, desc), args);
    prog_opts::notify(args);

    if(args.count("algo") && args.count("exchanges") && args.count("admin_port"))
    {
        //TODO:
    }
    else if(args.count("version"))
    {
        //TODO:
    }
    else if(args.count("help"))
    {
        std::cout << desc << std::endl;
    }
    else
    {
        std::cerr << desc << std::endl;
        rc = 1;
    }  
}
catch(const prog_opts::error& e)
{
    std::cerr << "Failed start with given command line arguments: " << e.what() << std::endl;
    rc = 1;
}

return rc;
}

我建议您详细查看 boost 文档:http: //www.boost.org/doc/libs/1_32_0/doc/html/program_options/overview.html#id548509

此外,对于更复杂的参数依赖关系,将它们“组合”在一起可能是有意义的,创建几个选项描述。 http://www.boost.org/doc/libs/1_32_0/doc/html/program_options/howto.html#id549397

于 2013-02-18T16:18:56.647 回答
0

我将通过以下方式“手动”实现该功能。我不知道 Boost 提供的任何其他功能。

unsigned int requiredArgsGroupCount = args.count("algo") + args.count("exchanges") + args.count("admin_port");
if ( (requiredArgsGroupCount > 0) && (requiredArgsGroupCount < 3) )
{
    // Indicate that all three arguments must be specified
}

只需在参数描述文本中指出,如果指定了一个,则所有三个都是必需的。

于 2012-11-13T15:19:36.993 回答