23 #include <boost/algorithm/string.hpp>
49 void parse(
const std::string& str)
64 if (
n <
args.size()) {
74 if (
n <
args.size()) {
93 std::size_t first_space =
str_.find_first_of(
' ',
args.back());
94 std::size_t next_arg_begin =
str_.find_first_not_of(
' ', first_space);
95 if (next_arg_begin != std::string::npos) {
96 args.push_back(next_arg_begin);
104 mutable std::vector<std::size_t>
args;
121 template <
class Worker>
133 const std::string&
usage =
"",
const std::string&
flags =
"")
169 for (
int i = 0;
i < 100; ++
i) {
176 cmd = actual_cmd + (
data.empty() ?
"" :
" ") +
data;
185 (
static_cast<Worker*
>(
this)->*(
c->handler))();
188 print(
get_cmd(),
_(
"This command is currently unavailable."));
198 symbols[
"help_command"] =
"help";
199 symbols[
"command"] =
get_cmd();
201 std::string string_user =
get_cmd();
205 bool has_command_proposal =
false;
211 len_min = std::min(string_user.length(), key.length());
215 if(distance * 100 / len_min < 34) {
216 symbols[
"command_proposal"] = key;
217 has_command_proposal =
true;
224 if(has_command_proposal) {
225 print(
"help",
VGETTEXT(
"Unknown command ‘$command’, did you mean ‘$command_proposal’? try $help_command "
226 "for a list of available commands.", symbols));
229 print(
"help",
VGETTEXT(
"Unknown command ‘$command’, try $help_command "
230 "for a list of available commands.", symbols));
237 std::vector<std::string> res;
239 res.push_back(
i.first);
252 _(
"Available commands list and command-specific help. "
253 "Use \"help all\" to include currently unavailable commands."),
257 _(
"[all|<command>]\n“all” = overview of all commands, <command> = name of a specific command (provides more detail)"));
262 virtual void print(
const std::string& title,
const std::string& message) = 0;
285 virtual std::string
get_arg(
unsigned argn)
const
290 virtual std::string
get_data(
unsigned argn = 1)
const
301 symbols[
"arg_id"] = std::to_string(argn);
316 typename command_map::const_iterator
i =
command_map_.find(cmd);
330 std::stringstream ss;
340 if (!
i.second.flags.empty()) {
341 ss <<
" (" <<
i.second.flags <<
") ";
348 symbols[
"list_of_commands"] = ss.str();
353 symbols[
"help_command"] =
"help";
355 print(
_(
"help"),
VGETTEXT(
"Available commands $flags_description:\n$list_of_commands", symbols));
356 print(
_(
"help"),
VGETTEXT(
"Type $help_command <command> for more info.", symbols));
364 std::stringstream ss;
371 if (
c->help.empty() &&
c->usage.empty()) {
372 ss <<
_(
" No help available.");
375 ss <<
" - " <<
c->help <<
"\n";
377 if (!
c->usage.empty()) {
379 ss <<
_(
"Usage:") <<
" " <<
cmd_prefix_ << cmd <<
" " <<
c->usage <<
"\n";
382 ss <<
_(
"Usage:") <<
" " << cmd <<
" " <<
c->usage <<
"\n";
386 if (!flags_description.empty()) {
390 const std::vector<std::string> l =
get_aliases(cmd);
394 ss <<
_n(
"command^Alias:",
"Aliases:", l.size()) <<
" " <<
utils::join(l,
" ") <<
"\n";
396 print(
_(
"help"), ss.str());
421 const std::string& usage =
"",
const std::string& flags =
"")
424 std::pair<typename command_map::iterator, bool> r;
425 r =
command_map_.insert(
typename command_map::value_type(cmd,
c));
432 const std::string& cmd)
438 static const std::vector<std::string>
get_aliases(
const std::string& cmd)
440 std::vector<std::string> aliases;
441 typedef command_alias_map::value_type
p;
443 if (
i.second == cmd) {
444 aliases.push_back(
i.first);
void parse(const std::string &str)
std::string get_arg(unsigned n) const
cmd_arg_parser(const cmd_arg_parser &)
void advance_to_arg(unsigned n) const
const std::string & get_str() const
cmd_arg_parser & operator=(const cmd_arg_parser &)
std::vector< std::size_t > args
std::string get_data(unsigned n) const
std::string get_cmd() const
cmd_arg_parser(const std::string &str)
std::map< std::string, std::string > command_alias_map
static const std::vector< std::string > get_aliases(const std::string &cmd)
const command * get_command(const std::string &cmd) const
virtual std::string get_flags_description() const
virtual void register_command(const std::string &cmd, command_handler h, const std::string &help="", const std::string &usage="", const std::string &flags="")
std::map< std::string, command > command_map
static bool help_on_unknown_
virtual std::string get_command_flags_description(const command &) const
virtual ~map_command_handler()
virtual std::string get_arg(unsigned argn) const
command * get_command(const std::string &cmd)
static bool show_unavailable_
virtual std::string get_cmd() const
std::vector< std::string > get_commands_list() const
static void set_cmd_prefix(const std::string &value)
virtual bool is_enabled(const command &) const
static std::string cmd_prefix_
void dispatch(std::string cmd)
virtual std::string get_data(unsigned argn=1) const
static void set_cmd_flag(bool value)
static void set_help_on_unknown(bool value)
void(Worker::* command_handler)()
void command_failed_need_arg(int argn)
static command_map command_map_
virtual void init_map()=0
virtual void parse_cmd(const std::string &cmd_string)
void command_failed(const std::string &message, bool=false)
bool help_command(const std::string &acmd)
static command_alias_map command_alias_map_
std::string get_actual_cmd(const std::string &cmd) const
virtual void register_alias(const std::string &to_cmd, const std::string &cmd)
virtual void print(const std::string &title, const std::string &message)=0
Definitions for the interface to Wesnoth Markup Language (WML).
static std::string _n(const char *str1, const char *str2, int n)
static std::string _(const char *str)
Handling of system events.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
void trim(std::string_view &s)
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
std::map< std::string, t_string > string_map
std::string::const_iterator iterator
bool has_flag(const char f) const
command(command_handler h, const std::string &help="", const std::string &usage="", const std::string &flags="")
command & add_flag(const char f)
static map_location::direction n