これは、現在の回答を補完するオプションとしてフラグを提供する完全な例です。
#include <iostream>
#include <boost/program_options.hpp>
using namespace std;
namespace po = boost::program_options;
int main(int ac, char* av[])
{
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("overwrite,o", po::bool_switch()->default_value(false),
"enable file overwrite");
po::variables_map vm;
po::store(po::parse_command_line(ac, av, desc), vm);
boolalpha(cout); // display true and false for bool
cout << "overwrite is: " << vm["overwrite"].as<bool>() << endl;
return 0;
}
私のqmakeプロファイル(私はQt 5.4を使用しています):
TEMPLATE = app
CONFIG += console
CONFIG += c++14
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
include(deployment.pri)
qtcAddDeployment()
INCLUDEPATH += /opt/boost_1_57_0
unix:!macx: LIBS += -L/opt/boost_1_57_0/stage/lib -lboost_program_options
オプションなしでプログラムを実行すると、次のようになります。
./untitled4
overwrite is: false
ただし、「-o」オプション/フラグを指定して実行すると、次のようになります。
./untitled4 -o
overwrite is: true