[boost 학습노트] 명령행 분석 라이브러리(program options)

코드:
// Copyright Vladimir Prus 2002-2004.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

/* The simplest usage of the library.
 */

#include <boost/program_options.hpp>
namespace po = boost::program_options;

#include <iostream>
#include <iterator>
using namespace std;

int main(int ac, char* av[])
{
    try {

        po::options_description desc("Allowed options");
        desc.add_options()
            ("help,h", "produce help message")
            ("compression", po::value<double>(), "set compression level")
			("include-path,I", po::value<std::string>(), "set path")
			("debugflag,D", po::value<bool>(), "set file")
        ;

        po::variables_map vm;        
        po::store(po::parse_command_line(ac, av, desc), vm);
        po::notify(vm);

        if (vm.count("help")) {
            cout << desc << "
"; //return 0; } if (vm.count("compression")) { cout << "Compression level was set to " << vm["compression"].as<double>() << ".
"; } else { cout << "Compression level was not set.
"; } if (vm.count("include-path")) { cout << "include-path are : " << vm["include-path"].as<std::string>() << ".
"; } else { cout << "include-path was not set.
"; } if (vm.count("debugflag")) { cout << "debugflag are : " << vm["debugflag"].as<bool>() << ".
"; } else { cout << "debugflag was not set.
"; } } catch(exception& e) { cerr << "error: " << e.what() << "
"; return 1; } catch(...) { cerr << "Exception of unknown type!
"; } return 0; }

위 절차에서는 다음 매개변수를 사용하여 실행합니다.
testDemo --help --compression 12 -I ssssss -D true
결과:
Allowed options:   -h [ --help ]             produce help message   --compression arg         set compression level   -I [ --include-path ] arg set path   -D [ --debugflag ] arg    set file Compression level was set to 12. include-path are :  ssssss. debugflag are : 1.

좋은 웹페이지 즐겨찾기