diff --git a/src/options_description.cpp b/src/options_description.cpp index 7d5a941..546b78e 100644 --- a/src/options_description.cpp +++ b/src/options_description.cpp @@ -258,7 +258,8 @@ namespace boost { namespace program_options { options_description::find_nothrow(const std::string& name, bool approx) const { - int found = -1; + shared_ptr found; + vector approximate_matches; // We use linear search because matching specified option // name with the declared option name need to take care about // case sensitivity and trailing '*' and so we can't use simple map. @@ -284,25 +285,16 @@ namespace boost { namespace program_options { return m_options[i].get(); } - if (found != -1) - { - vector alts; - // FIXME: the use of 'key' here might not - // be the best approach. - alts.push_back(m_options[found]->key(name)); - alts.push_back(m_options[i]->key(name)); - boost::throw_exception(ambiguous_option(name, alts)); - } - else - { - found = i; - } - } - if (found != -1) { - return m_options[found].get(); - } else { - return 0; + found = m_options[i]; + // FIXME: the use of 'key' here might not + // be the best approach. + approximate_matches.push_back(m_options[i]->key(name)); } + if (approximate_matches.size() > 1) + boost::throw_exception( + ambiguous_option(name, approximate_matches)); + else + return found.get(); } BOOST_PROGRAM_OPTIONS_DECL diff --git a/test/options_description_test.cpp b/test/options_description_test.cpp index d5ec259..885e539 100644 --- a/test/options_description_test.cpp +++ b/test/options_description_test.cpp @@ -44,9 +44,9 @@ void test_approximation() ("foo", new untyped_value()) ("fee", new untyped_value()) ("baz", new untyped_value()) - ("all", new untyped_value()) ("all-chroots", new untyped_value()) ("all-sessions", new untyped_value()) + ("all", new untyped_value()) ; BOOST_CHECK_EQUAL(desc.find("fo", true).long_name(), "foo");