mirror of https://github.com/djcb/mu.git
server: improve command documentation output
required args come first, then alphabetic.
This commit is contained in:
parent
24e94afe2f
commit
7d83a1c5d6
|
@ -25,6 +25,7 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#include "utils/mu-error.hh"
|
#include "utils/mu-error.hh"
|
||||||
#include "utils/mu-sexp-parser.hh"
|
#include "utils/mu-sexp-parser.hh"
|
||||||
|
@ -80,6 +81,27 @@ struct CommandInfo {
|
||||||
const ArgMap args;
|
const ArgMap args;
|
||||||
const std::string docstring;
|
const std::string docstring;
|
||||||
const Handler handler;
|
const Handler handler;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a sorted list of argument names, for display. Required args come
|
||||||
|
* first, then alphabetical.
|
||||||
|
*
|
||||||
|
* @return vec with the sorted names.
|
||||||
|
*/
|
||||||
|
std::vector<std::string> sorted_argnames() const { // sort args -- by required, then alphabetical.
|
||||||
|
std::vector<std::string> names;
|
||||||
|
for (auto&& arg: args)
|
||||||
|
names.emplace_back(arg.first);
|
||||||
|
std::sort(names.begin(), names.end(), [&](const auto& name1, const auto& name2) {
|
||||||
|
const auto& arg1{args.find(name1)->second};
|
||||||
|
const auto& arg2{args.find(name2)->second};
|
||||||
|
if (arg1.required != arg2.required)
|
||||||
|
return arg1.required;
|
||||||
|
else
|
||||||
|
return name1 < name2;
|
||||||
|
});
|
||||||
|
return names;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
/// All commands, mapping their name to information about them.
|
/// All commands, mapping their name to information about them.
|
||||||
using CommandMap = std::unordered_map<std::string, CommandInfo>;
|
using CommandMap = std::unordered_map<std::string, CommandInfo>;
|
||||||
|
|
|
@ -742,11 +742,12 @@ help_handler (Context& context, const Parameters& params)
|
||||||
if (!full)
|
if (!full)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
for (auto&& arg: info.args) {
|
for (auto&& argname: info.sorted_argnames()) {
|
||||||
|
const auto& arg{info.args.find(argname)};
|
||||||
std::cout << ";; "
|
std::cout << ";; "
|
||||||
<< format("%-17s : %-22s ", arg.first.c_str(),
|
<< format("%-17s : %-24s ", arg->first.c_str(),
|
||||||
to_string(arg.second).c_str());
|
to_string(arg->second).c_str());
|
||||||
std::cout << " " << arg.second.docstring << "\n";
|
std::cout << " " << arg->second.docstring << "\n";
|
||||||
}
|
}
|
||||||
std::cout << ";;\n";
|
std::cout << ";;\n";
|
||||||
}
|
}
|
||||||
|
@ -1090,8 +1091,8 @@ make_command_map (Context& context)
|
||||||
|
|
||||||
cmap.emplace("add",
|
cmap.emplace("add",
|
||||||
CommandInfo{
|
CommandInfo{
|
||||||
ArgMap{ {"path", ArgInfo{Type::String, true, "message-path" }},
|
ArgMap{ {"path", ArgInfo{Type::String, true, "file system path to the message" }},
|
||||||
{"maildir", ArgInfo{Type::String, true, "maildir" }}},
|
{"maildir", ArgInfo{Type::String, true, "the maildir the where the message lives" }}},
|
||||||
"add a message to the store",
|
"add a message to the store",
|
||||||
[&](const auto& params){add_handler(context, params);}});
|
[&](const auto& params){add_handler(context, params);}});
|
||||||
|
|
||||||
|
@ -1164,7 +1165,7 @@ make_command_map (Context& context)
|
||||||
{"cleanup", ArgInfo{Type::Symbol, false,
|
{"cleanup", ArgInfo{Type::Symbol, false,
|
||||||
"whether to remove stale messages from the store"}},
|
"whether to remove stale messages from the store"}},
|
||||||
{"lazy-check", ArgInfo{Type::Symbol, false,
|
{"lazy-check", ArgInfo{Type::Symbol, false,
|
||||||
"Whether to avoid indexing up-to-date directories"}}},
|
"whether to avoid indexing up-to-date directories"}}},
|
||||||
"scan maildir for new/updated/removed messages",
|
"scan maildir for new/updated/removed messages",
|
||||||
[&](const auto& params){index_handler(context, params);}});
|
[&](const auto& params){index_handler(context, params);}});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue