boost::program_options 最小示例

此文章中的示例:

未来可能会添加更多示例

两个参数:–input/-i 和 –output/-o,都是字符串,都是必需的

boost_program_options_examples.cpp
/**
 * 像这样编译:g++ -o test test.cpp -lboost_program_options
 * 用法示例:
 *      ./test -i infile.txt -o outfile.txt
 *      ./test --input infile.txt --output outfile.txt
 */
#include <boost/program_options.hpp>
#include <string>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    // 参数将存储在这里
    std::string input;
    std::string output;
    // 在这里配置选项
    po::options_description desc ("Allowed options");
    desc.add_options ()
        ("help,h", "print usage message")
        ("input,i", po::value(&input), "Input file")
        ("output,o", po::value(&output), "Output file");
    // 解析命令行参数
    po::variables_map vm;
    po::store (po::command_line_parser (argc, argv).options (desc).run (), vm);
    po::notify (vm);
    // 检查是否有足够的参数或是否给出了 --help
    if (vm.count ("help") || !vm.count ("input") || !vm.count ("output")) {
        std::cerr << desc << "\n";
        return 1;
    }
    // 你的代码放在这里
}

单个位置字符串参数

boost_program_options_examples_part2.cpp
/**
 * 像这样编译:g++ -o test test.cpp -lboost_program_options
 * 用法示例:
 *      ./test infile.txt
 *      ./test --input infile.txt
 */
#include <boost/program_options.hpp>
#include <string>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    // 参数将存储在这里
    std::string input;
    // 注意:在 desc 和 positionals 中声明相同的选项!
    // 没有声明普通选项,只有位置参数!
    po::options_description desc ("Allowed options");
    desc.add_options()
        ("help", "Print usage")
        ("input", po::value(&input), "Input file");
    // 在这里配置选项
    po::positional_options_description positionals;
    positionals.add("input", 1 /* 1 = max num of inputs ; -1: arbitrary number */);
    // 解析命令行参数
    po::variables_map vm;
    po::store (po::command_line_parser (argc, argv)
        .positional(positionals)
        .options(desc).run (), vm);
    po::notify (vm);
    // 检查是否有足够的参数或是否给出了 --help
    if (vm.count ("help") || !vm.count ("input")) {
        std::cerr << desc << "\n";
        return 1;
    }
    // 你的代码放在这里!
    std::cout << "Input is " << input << std::endl;
}

双类型参数(带自动类型检查)

boost_program_options_examples_part3.cpp
/**
 * 像这样编译:g++ -o test test.cpp -lboost_program_options
 * 用法示例:
 *      ./test -v 1.23
 * 这不起作用
 *      ./test -v ABC
 */
#include <boost/program_options.hpp>
#include <string>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    // 参数将存储在这里
    double value;
    // 在这里配置选项
    po::options_description desc ("Allowed options");
    desc.add_options ()
        ("help,h", "print usage message")
        ("value,v", po::value(&value), "The value");
    // 解析命令行参数
    po::variables_map vm;
    po::store (po::command_line_parser (argc, argv).options (desc).run (), vm);
    po::notify (vm);
    // 检查是否有足够的参数或是否给出了 --help
    if (vm.count ("help") || !vm.count ("value")) {
        std::cerr << desc << "\n";
        return 1;
    }
    // 你的代码放在这里!
    std::cout << value << std::endl;
}

Check out similar posts by category: Boost, C/C++