Skip to content

Commit 53ba9ab

Browse files
committed
Robustify disambiguation of full/approximate matches.
Fixes #3942. [SVN r59744]
1 parent af37add commit 53ba9ab

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/options_description.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ namespace boost { namespace program_options {
306306
bool short_ignore_case) const
307307
{
308308
shared_ptr<option_description> found;
309+
bool had_full_match = false;
309310
vector<string> approximate_matches;
310311
vector<string> full_matches;
311312

@@ -323,15 +324,17 @@ namespace boost { namespace program_options {
323324
if (r == option_description::full_match)
324325
{
325326
full_matches.push_back(m_options[i]->key(name));
327+
found = m_options[i];
328+
had_full_match = true;
326329
}
327330
else
328331
{
329332
// FIXME: the use of 'key' here might not
330333
// be the best approach.
331334
approximate_matches.push_back(m_options[i]->key(name));
335+
if (!had_full_match)
336+
found = m_options[i];
332337
}
333-
334-
found = m_options[i];
335338
}
336339
if (full_matches.size() > 1)
337340
boost::throw_exception(

test/options_description_test.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ void test_approximation()
5353
BOOST_CHECK_EQUAL(desc.find("all", true).long_name(), "all");
5454
BOOST_CHECK_EQUAL(desc.find("all-ch", true).long_name(), "all-chroots");
5555

56+
options_description desc2;
57+
desc2.add_options()
58+
("help", "display this message")
59+
("config", value<string>(), "config file name")
60+
("config-value", value<string>(), "single config value")
61+
;
62+
63+
BOOST_CHECK_EQUAL(desc2.find("config", true).long_name(), "config");
64+
BOOST_CHECK_EQUAL(desc2.find("config-value", true).long_name(),
65+
"config-value");
66+
5667

5768
// BOOST_CHECK(desc.count_approx("foo") == 1);
5869
// set<string> a = desc.approximations("f");

0 commit comments

Comments
 (0)