c++ - Why do I always get default values when passing positional arguments? -
i'm trying familiarize myself boost::program_options
, , i'm running problem positional arguments.
here's main
function, set options passed through command line. please note po
namespace alias boost::program_options
.
int main(int argc, char** argv) { int retval = success; try { // define , parse program options po::options_description desc("options"); desc.add_options() ("help", "print messages") ("mode,m", po::value<std::string>()->default_value("ecb"), "cipher mode of operation") ("keyfile,f", po::value<bool>(), "use keyfile") ("key,k", po::value<std::string>(), "ascii key") ("infile", po::value<std::string>()->default_value("plaintext.txt"), "input file") ("outfile", po::value<std::string>()->default_value("ciphertext.txt"), "output file"); po::positional_options_description pargd; pargd.add("infile", 1); pargd.add("outfile", 2); po::variables_map vm; try { po::store(po::parse_command_line(argc, argv, desc), vm); // can throw // --help option if ( vm.count("help") ) { std::cout << "basic command line parameter app" << std::endl << desc << std::endl; return success; } po::notify(vm); // throws on error, after in case // there problems } catch(po::error& e) { std::cerr << "error: " << e.what() << std::endl << std::endl; std::cerr << desc << std::endl; return error_in_command_line; } ///application code here retval = application(vm); } catch(std::exception& e) { std::cerr << "unhandled exception reached top of main: " << e.what() << ", application exit" << std::endl; return error_unhandled_exception; } return retval; } // main
when try print positional argument of vm
(the variable_map) cout << wm["infile"].as<std::string>();
, default value "infile" parameter.
i'm calling executable ./a.out in.file out.file
test.
what doing wrong?
i figured out!
po::store(po::parse_command_line(argc, argv, desc), vm);
should be...
po::store(po::command_line_parser(argc, argv).options(desc).positional(pargd).run(), vm);
although i'm not sure understand why yet...
Comments
Post a Comment