diff --git a/include/boost/program_options/parsers.hpp b/include/boost/program_options/parsers.hpp index 0576469278..f9d7addda7 100644 --- a/include/boost/program_options/parsers.hpp +++ b/include/boost/program_options/parsers.hpp @@ -175,7 +175,9 @@ namespace boost { namespace program_options { /** Parse a config file. - Read from given stream. + Read from given stream. Errors while reading the stream are not + handled in any special manner. The method `bad()` on the given + stream can be used to check if an error has been occurred. */ template<class charT> #if ! BOOST_WORKAROUND(__ICL, BOOST_TESTED_AT(700)) @@ -188,7 +190,8 @@ namespace boost { namespace program_options { /** Parse a config file. Read from file with the given name. The character type is - passed to the file stream. + passed to the file stream. An exception of type `reading_file` + is thrown when the file cannot be read. */ #ifdef BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS template<class charT> diff --git a/src/parsers.cpp b/src/parsers.cpp index dd62c66d37..3e435eea3f 100644 --- a/src/parsers.cpp +++ b/src/parsers.cpp @@ -152,7 +152,16 @@ namespace boost { namespace program_options { { boost::throw_exception(reading_file(filename)); } - return parse_config_file(strm, desc, allow_unregistered); + + basic_parsed_options<charT> result + = parse_config_file(strm, desc, allow_unregistered); + + if (strm.bad()) + { + boost::throw_exception(reading_file(filename)); + } + + return result; } template diff --git a/test/parsers_test.cpp b/test/parsers_test.cpp index 4cf832ee75..259ffa5a78 100644 --- a/test/parsers_test.cpp +++ b/test/parsers_test.cpp @@ -375,6 +375,15 @@ void test_unregistered() check_value(a3[1], "m1.v1", "1"); } +void test_fail_directory(const char* some_dir) +{ + options_description desc; + TEST_CHECK_THROW( + parse_config_file<char>(some_dir, desc), + reading_file, + "providing directory as config file must cause exception") +} + int main(int, char* av[]) @@ -383,6 +392,7 @@ int main(int, char* av[]) test_config_file(av[1]); test_environment(); test_unregistered(); + test_fail_directory("."); return 0; }