commit 4cda95db89f7f2106d3f7a9c0374c0d1490402db
parent f329d651df3844b0df7a5c997c11004114d70c4f
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Sat, 8 Jun 2024 23:21:27 +0200
Fix option sorting
Diffstat:
2 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/args.hpp b/args.hpp
@@ -299,22 +299,25 @@ class Parser {
void push(const char *lg) { opt_long.push_back(lg); }
bool operator<(const help_entry_t &rhs) const {
- if (group && rhs.group) {
- if (group < 0 && rhs.group < 0) return group < rhs.group;
- if (group < 0 || rhs.group < 0) return rhs.group < 0;
- return group < rhs.group;
- }
+ if (group != rhs.group) {
+ if (group && rhs.group) {
+ if (group < 0 && rhs.group < 0) return group < rhs.group;
+ if (group < 0 || rhs.group < 0) return rhs.group < 0;
+ return group < rhs.group;
+ }
- if (group || rhs.group) return !group;
+ return !group;
+ }
- if (opt_long.empty() && rhs.opt_long.empty())
- return opt_short.front() < rhs.opt_short.front();
+ const char l1 = !opt_long.empty() ? opt_long.front()[0]
+ : !opt_short.empty() ? opt_short.front()
+ : '0';
- if (opt_long.empty())
- return opt_short.front() <= rhs.opt_long.front()[0];
+ const char l2 = !rhs.opt_long.empty() ? rhs.opt_long.front()[0]
+ : !rhs.opt_short.empty() ? rhs.opt_short.front()
+ : '0';
- if (rhs.opt_long.empty())
- return opt_long.front()[0] <= rhs.opt_short.front();
+ if (l1 != l2) return l1 < l2;
return std::strcmp(opt_long.front(), rhs.opt_long.front()) < 0;
}
diff --git a/demo.cpp b/demo.cpp
@@ -43,9 +43,9 @@ using enum Parser::Option;
static const Parser::option_t options[] = {
{ 0, 'R', 0, 0, "random 0-group option"},
{ 0, 0, 0, 0, "Program mode", 1},
+ {"relocatable", 'r', 0, 0, "Output in relocatable format"},
{ "hex", 'h', 0, 0, "Output in hex format"},
{"hexadecimal", 0, 0, ALIAS | HIDDEN},
- {"relocatable", 'r', 0, 0, "Output in relocatable format"},
{ 0, 0, 0, 0, "For developers", 4},
{ "debug", 777, 0, 0, "Enable debugging mode"},
{ 0, 0, 0, 0, "Input/output", 3},