Skip to content

Commit

Permalink
Fix Issue nanocurrency#3748
Browse files Browse the repository at this point in the history
Sorted command line options output
  • Loading branch information
Justin-Randall committed Mar 10, 2022
1 parent 2a0ee9f commit 85e15b3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
21 changes: 21 additions & 0 deletions nano/lib/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <boost/dll/runtime_symbol_info.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

#include <cstddef>
#include <iostream>
Expand Down Expand Up @@ -196,3 +197,23 @@ void assert_internal (char const * check_expr, char const * func, char const * f

abort ();
}

// Issue #3748
void nano::sort_options_description (const boost::program_options::options_description & source, boost::program_options::options_description & target)
{
// Grab all of the options, get the option display name, stick it in a map using the display name as
// the key (the map will sort) and the value as the option itself.
const auto & options = source.options ();
std::map<std::string, boost::shared_ptr<boost::program_options::option_description>> sorted_options;
for (const auto & option : options)
{
auto pair = std::make_pair (option->canonical_display_name (2), option);
sorted_options.insert (pair);
}

// Rebuild for display purposes only.
for (const auto & option_pair : sorted_options)
{
target.add (option_pair.second);
}
}
8 changes: 8 additions & 0 deletions nano/lib/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ namespace system
{
class error_code;
}

namespace program_options
{
class options_description;
}
}

void assert_internal (char const * check_expr, char const * func, char const * file, unsigned int line, bool is_release_assert, std::string_view error = "");
Expand Down Expand Up @@ -203,4 +208,7 @@ constexpr TARGET_TYPE narrow_cast (SOURCE_TYPE const & val)
debug_assert (val == static_cast<SOURCE_TYPE> (res));
return res;
}

// Issue #3748
void sort_options_description (const boost::program_options::options_description & source, boost::program_options::options_description & target);
}
6 changes: 5 additions & 1 deletion nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2033,7 +2033,11 @@ int main (int argc, char * const * argv)
}
else
{
std::cout << description << std::endl;
// Issue #3748
// Regardless how the options were added, output the options in alphabetical order so they are easy to find.
boost::program_options::options_description sorted_description ("Command line options");
nano::sort_options_description (description, sorted_description);
std::cout << sorted_description << std::endl;
result = -1;
}
}
Expand Down
6 changes: 5 additions & 1 deletion nano/nano_rpc/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ int main (int argc, char * const * argv)
}
else
{
std::cout << description << std::endl;
// Issue #3748
// Regardless how the options were added, output the options in alphabetical order so they are easy to find.
boost::program_options::options_description sorted_description ("Command line options");
nano::sort_options_description (description, sorted_description);
std::cout << sorted_description << std::endl;
}

return 1;
Expand Down

0 comments on commit 85e15b3

Please sign in to comment.