Parse the "argv" via the Boost.program_options (get the number, string and array from command line) #devsample #boost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parse the "argv" via the Boost.program_options | |
* (get the number, string and array from command line) | |
*/ | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <boost/program_options.hpp> | |
/* namespace alias */ | |
namespace po = boost::program_options; | |
int main(int argc, char **argv) | |
{ | |
/* examples: | |
* app -h : print help message | |
* app --help : print help message | |
* | |
* app -f "test" : set the test filename | |
* app -file "test" : set the test filename | |
* | |
* app -n 100 : set the test number | |
* app --number 100 : set the test number | |
* | |
* app -F "1.cpp" "2.cpp" : set test files | |
* app --files "1.cpp" "2.cpp" : set test files | |
*/ | |
po::options_description desc("MainOptions"); | |
desc.add_options() | |
("help,h", "Print help messages") | |
("file,f", | |
po::value<std::string>()->default_value("defaultName"), | |
"A test filename") | |
("number,n", | |
po::value<int>()->default_value(0), | |
"A test Number") | |
("files,F", | |
po::value< std::vector<std::string> >()->multitoken(), | |
"Test files"); | |
po::variables_map vm; | |
try { | |
po::store(po::parse_command_line(argc, argv, desc), vm); | |
} | |
catch (po::error &e) { | |
/* Invalid options */ | |
std::cerr << "ERROR: " << e.what() << std::endl << std::endl; | |
std::cout << "Boost program_options tester:" << std::endl | |
<< desc << std::endl; | |
return 0; | |
} | |
if (vm.count("help")) | |
{ | |
/* print usage */ | |
std::cout << "Boost program_options tester:" << std::endl | |
<< desc << std::endl; | |
return 0; | |
} | |
std::cout << "Test filename: " << vm["file"].as<std::string>() | |
<< std::endl; | |
std::cout << "Test number: " << vm["number"].as<int>() | |
<< std::endl; | |
if (vm.count("files")) | |
{ | |
std::vector<std::string> files = vm["files"].as< std::vector<std::string> >(); | |
for (std::vector<std::string>::iterator itr = files.begin(); itr != files.end(); ++itr) | |
{ | |
std::cout << "Filename: " << *itr << std::endl; | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CMake build script (read .ini) | |
cmake_minimum_required(VERSION 2.8) | |
# project name & version | |
project(ProgOpts) | |
# common settings (Boost libraries) | |
if (MSVC) | |
# Enable the static libraries on Windows | |
foreach (flag_var | |
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE | |
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO | |
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE | |
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO) | |
# update to the static version of the run time library | |
string(REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") | |
endforeach() | |
unset(Boost_LIBRARIES) | |
set(Boost_USE_STATIC ON) | |
set(Boost_USE_STATIC_LIBS ON) | |
set(Boost_USE_MULTITHREADED ON) | |
set(Boost_USE_STATIC_RUNTIME ON) | |
endif() | |
find_package(Boost REQUIRED COMPONENTS | |
program_options) | |
include_directories("${PROJECT_SOURCE_DIR}" | |
"${Boost_INCLUDE_DIRS}") | |
set(progopts_boost_dbg_libs | |
"${Boost_PROGRAM_OPTIONS_LIBRARY_DEBUG}") | |
set(progopts_boost_opt_libs | |
"${Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE}") | |
# application | |
add_executable(prog-opts | |
"${PROJECT_SOURCE_DIR}/0-program-options.cpp") | |
target_link_libraries(prog-opts | |
debug "${progopts_boost_dbg_libs}" | |
optimized "${progopts_boost_opt_libs}") | |
# install | |
install(TARGETS prog-opts DESTINATION bin) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment