以下のコードでは、プログラム オプションを使用して、コマンドラインまたはファイルからパラメーターを読み取りました。さらに、ConfigProxy::setConfigを使用して実行時にプログラムでオプションを設定できます。
po::options_description desc("Allowed options");
desc.add_options()
...
("compression", po::value<int>(), "set compression level");
po::variables_map vm;
class ConfigProxy
{
template< typename T>
void setConfig( const std::string key, const T value ){
... // check if the key exists in variable map "vm"
// key exists, set the value
runtimeConfig[key] = po::variable_value( boost::any(value), false);
}
po::variable_value& operator[] (const std::string key) const{
...
// if exists in runtimeConfig return the value in runtimeConfig
// of type program_options::variable_value
...
// else return value in variable map "vm"
}
std::map<std::string, boost::program_options::variable_value> runtimeConfig;
}
ConfigProxy を介して、オプション値が取得されます
if( vm.count("compression") ){
int value = proxyConfig["compression"].as<int>();
...
}
ただし、ユーザーが指定した「圧縮」オプションの値が間違ったタイプの場合、たとえば
configProxy.setConfig("compression", "12" );
...
int value = configProxy["compression"].as<int>(); // was set as string
その後、例外がスローされます
what(): boost::bad_any_cast: failed conversion using boost::any_cast
例外は、型キャストの問題を明確に示しています。しかし、このメッセージは、ユーザーがどのオプションがエラーの原因であるかを見つけるのにあまり役に立たないようです。
bad_any_cast例外をスローする代わりに、このタイプのエラーについてユーザーに通知するより良い方法はありますか?
- - - 編集 - - - - - - - - - - - - -
Luc Danton と Tony のおかげで、プログラム オプションがどのようにエラーを表示するかがわかりました。
void validate(boost::any& v,
const std::vector< std::basic_string<charT> >& xs,
T*, long)
{
validators::check_first_occurrence(v);
std::basic_string<charT> s(validators::get_single_string(xs));
try {
v = any(lexical_cast<T>(s));
}
catch(const bad_lexical_cast&) {
boost::throw_exception(invalid_option_value(s));
}
}
ロジックを実装することで、bad_any_cast 例外を取り除くことができると思います。