为什么我总是在传递位置参数时得到默认值
Why do I always get default values when passing positional arguments?
我正在尝试熟悉boost::program_options
,并且我遇到了位置参数的问题。
这是我的main
函数,我通过命令行设置选项。po
是boost::program_options
的命名空间别名。
int main(int argc, char** argv)
{
int retval = SUCCESS;
try
{
// Define and parse the program options
po::options_description desc("Options");
desc.add_options()
("help", "Print help 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, so do after help in case
// there are any 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 the top of main: "
<< e.what() << ", application will now exit" << std::endl;
return ERROR_UNHANDLED_EXCEPTION;
}
return retval;
} // main
当我尝试用cout << wm["infile"].as<std::string>();
打印vm
(variable_map)的位置参数时,我总是得到"infile"参数的默认值。
我将可执行文件调用为./a.out in.file out.file
进行测试。
我做错了什么?
我明白了!
po::store(po::parse_command_line(argc, argv, desc), vm);
po::store(po::command_line_parser(argc, argv).options(desc).positional(pargd).run(), vm);
虽然我不确定我理解为什么还…
相关文章:
- 当给定默认值时,为什么此模板参数推导失败
- 如何在C++中提供模板化函数作为另一个函数的参数,默认值?
- 在 c++ 中,如果我创建一个接受一个具有默认值的参数的构造函数 - 它会用作默认(空)构造函数吗?
- 为什么无法在可变参数模板中将尾随模板参数指定为默认值?
- 可变参数模板和具有继承的默认值
- 具有默认值的单个参数构造函数是否与默认构造函数相同?
- 在子类函数覆盖中省略具有默认值的参数
- 函数作为具有默认值的参数
- 具有另一个具有默认值的模板参数的模板推导
- const变量是否可以在具有默认值的参数中赋值(作为可选参数)
- 可以通过常量引用返回默认参数的值吗?
- 对 const 引用参数使用默认值会导致崩溃
- C ,构造器中允许的参考参数默认值
- 是否可以设计一个包含模板参数默认值的类
- 构造函数中参数C++默认值
- 如何设置依赖于其他参数的参数默认值
- QInput对话框参数默认值
- 在函数定义中指定参数默认值会导致错误 C2143:语法错误:'='之前缺少')'
- 内置类型的模板参数默认值
- 是否可以使用构造函数或对象作为其他类方法的参数默认值?