boost解析argv

#include <boost/program_options.hpp>

namespace po = boost::program_options;

bool parse_command_line(int argc, char *argv[], po::variables_map &vm) {
    po::options_description desc("Allowed options");
    desc.add_options()
            ("help", "produce help message")
            ("a", po::value<std::string>()->required(), "provide the data base dir")
            ("b", po::value<std::string>()->required(), "dest dir for save final kv")
            ("c", po::value<int>()->required(), "provide which frame to start with");try {
        po::store(po::parse_command_line(argc, argv, desc), vm);
        if (vm.count("help")) {
            std::cerr << desc << std::endl;
            return false;
        }

        po::notify(vm);
    } catch (std::exception& e) {
        std::cerr << "Error: " << e.what() << "\n";
        return false;
    } catch (...) {
        std::cerr << "Unknown error!" << "\n";
        return false;
    }
    return true;
}

int main(int argc, char *argv[]) {
    // parse command lines
    po::variables_map boost_args;
    if (!parse_command_line(argc, argv, boost_args)) {
        std::cerr << "Parse input command line failed." << std::endl;
        return 1;
    }

     cout << boost_args["a"] << endl;
}
 

 

posted @ 2016-04-10 21:52  带刀的剑客  阅读(419)  评论(0编辑  收藏  举报