diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index a5c67f1..1795169 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -2339,7 +2339,7 @@ OptionAdder::operator() // (length-1) and longer names std::string short_name {""}; auto first_short_name_iter = - std::partition(option_names.begin(), option_names.end(), + std::stable_partition(option_names.begin(), option_names.end(), [&](const std::string& name) { return name.length() > 1; } ); auto num_length_1_names = (option_names.end() - first_short_name_iter); diff --git a/test/options.cpp b/test/options.cpp index 6df4972..6faa438 100644 --- a/test/options.cpp +++ b/test/options.cpp @@ -1191,3 +1191,34 @@ TEST_CASE("No Options help", "[options]") CHECK_NOTHROW(options.parse(argc, argv)); CHECK(options.help().find("test ...") != std::string::npos); } + +TEST_CASE("Ordering of multiple long options", "[help]") +{ + cxxopts::Options options("test", "Ordering of multiple long options"); + options.add_options() + ("x,alphax,betax,gammax,deltax", "Multiple names x", cxxopts::value()) + ("deltay,gammay,y,betay,alphay", "Multiple names y", cxxopts::value()) + ("alphaz,betaz,z", "Multiple names z", cxxopts::value()) + ("alphaw,w", "Multiple names w", cxxopts::value()) + ("beta,gamma,alpha,delta", "Without short name", cxxopts::value()); + + auto opts = options.group_help("").options; + + // Veryfiy ordering + REQUIRE(opts.size()==5); + CHECK(opts[0].l == std::vector{"alphax", "betax", "gammax", "deltax"}); + CHECK(opts[1].l == std::vector{"deltay", "gammay", "betay", "alphay"}); + CHECK(opts[2].l == std::vector{"alphaz", "betaz"}); + CHECK(opts[3].l == std::vector{"alphaw"}); + CHECK(opts[4].l == std::vector{"beta", "gamma", "alpha", "delta"}); + + + std::string help = options.help(); + + // Verify first long options are always present in help output + CHECK(help.find("--alphax") != std::string::npos); + CHECK(help.find("--deltay") != std::string::npos); + CHECK(help.find("--alphaz") != std::string::npos); + CHECK(help.find("--alphaw") != std::string::npos); + CHECK(help.find("--beta") != std::string::npos); +}