`boost::program_options` 不能在 `variables_map` 上使用 `store` 两次
`boost::program_options` cannot use `store` twice on `variables_map`
我正在尝试编辑 boost::program_options::variables_map
以根据第一个位置选项添加隐含选项。但是,在 boost::variables_map
.
上使用 po::store
时,它不会再次使用
这是一个失败的最小示例:
#include <boost/program_options.hpp>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
namespace po = boost::program_options;
std::ostream &operator<<( std::ostream &s, po::variables_map const &vm )
{
for (const auto& it : vm)
{
s << it.first.c_str() << " ";
auto& value = it.second.value();
if (auto v = boost::any_cast<char>(&value))
s << *v;
else if (auto v = boost::any_cast<short>(&value))
s << *v;
else if (auto v = boost::any_cast<int>(&value))
s << *v;
else if (auto v = boost::any_cast<long>(&value))
s << *v;
else if (auto v = boost::any_cast<std::string>(&value))
s << *v;
else if (auto v = boost::any_cast<std::vector<std::string>>(&value))
{
s << "[";
for( const auto &w : *v )
s << " " << w;
s << " ]";
}
else
s << "<?>";
s << '\n';
}
return s;
}
int main( int argc, char **argv )
{
po::options_description desc("Options");
desc.add_options()
("help","show this help message")
("verbose","print extra messages")
("dummy","pointless")
("dummy-int", po::value< int >(), "pointless")
("dummy-str", po::value< std::string >(), "pointless")
;
po::variables_map vm;
po::store( po::parse_command_line( argc, argv, desc ), vm );
po::notify( vm );
bool verbose = vm.count("verbose");
if( verbose )
std::cout << vm << std::endl;
if( vm.count( "help" ) )
{
std::cout << desc << std::endl;
return 0;
}
const char* newOption = "--dummy";
po::store( po::parse_command_line( 1, &newOption, desc), vm );
po::notify( vm );
if( verbose )
std::cout << vm << std::endl;
return 0;
}
构建和运行程序,我可以在终端上看到:
dummy-int 3
verbose
dummy-int 3
verbose
换句话说,选项 dummy
是 而不是 存储到 vm
!你能帮我找到为什么不吗?感谢您的帮助!
您忘记了第一个参数有特殊含义 -- 它是程序被调用时的名称。
根据 documentation:
argv[0]
is the pointer to the initial character of a null-terminated multibyte strings that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment).
在代码段中
const char* newOption = "--dummy";
po::store( po::parse_command_line( 1, &newOption, desc), vm );
您将单元素数组作为 argv
传递,这意味着您没有提供任何参数,并且没有任何内容可供库解析。因此,没有向 variable_map
实例添加任何内容。
我正在尝试编辑 boost::program_options::variables_map
以根据第一个位置选项添加隐含选项。但是,在 boost::variables_map
.
po::store
时,它不会再次使用
这是一个失败的最小示例:
#include <boost/program_options.hpp>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>
namespace po = boost::program_options;
std::ostream &operator<<( std::ostream &s, po::variables_map const &vm )
{
for (const auto& it : vm)
{
s << it.first.c_str() << " ";
auto& value = it.second.value();
if (auto v = boost::any_cast<char>(&value))
s << *v;
else if (auto v = boost::any_cast<short>(&value))
s << *v;
else if (auto v = boost::any_cast<int>(&value))
s << *v;
else if (auto v = boost::any_cast<long>(&value))
s << *v;
else if (auto v = boost::any_cast<std::string>(&value))
s << *v;
else if (auto v = boost::any_cast<std::vector<std::string>>(&value))
{
s << "[";
for( const auto &w : *v )
s << " " << w;
s << " ]";
}
else
s << "<?>";
s << '\n';
}
return s;
}
int main( int argc, char **argv )
{
po::options_description desc("Options");
desc.add_options()
("help","show this help message")
("verbose","print extra messages")
("dummy","pointless")
("dummy-int", po::value< int >(), "pointless")
("dummy-str", po::value< std::string >(), "pointless")
;
po::variables_map vm;
po::store( po::parse_command_line( argc, argv, desc ), vm );
po::notify( vm );
bool verbose = vm.count("verbose");
if( verbose )
std::cout << vm << std::endl;
if( vm.count( "help" ) )
{
std::cout << desc << std::endl;
return 0;
}
const char* newOption = "--dummy";
po::store( po::parse_command_line( 1, &newOption, desc), vm );
po::notify( vm );
if( verbose )
std::cout << vm << std::endl;
return 0;
}
构建和运行程序,我可以在终端上看到:
dummy-int 3
verbose
dummy-int 3
verbose
换句话说,选项 dummy
是 而不是 存储到 vm
!你能帮我找到为什么不吗?感谢您的帮助!
您忘记了第一个参数有特殊含义 -- 它是程序被调用时的名称。
根据 documentation:
argv[0]
is the pointer to the initial character of a null-terminated multibyte strings that represents the name used to invoke the program itself (or an empty string "" if this is not supported by the execution environment).
在代码段中
const char* newOption = "--dummy";
po::store( po::parse_command_line( 1, &newOption, desc), vm );
您将单元素数组作为 argv
传递,这意味着您没有提供任何参数,并且没有任何内容可供库解析。因此,没有向 variable_map
实例添加任何内容。