diff --git a/src/autocomplete.cpp b/src/autocomplete.cpp index 1591b81..7cab974 100644 --- a/src/autocomplete.cpp +++ b/src/autocomplete.cpp @@ -9,6 +9,19 @@ #include #include +namespace autocomplete { + + const std::set system_commands_noargs = {"templates","autocomplete_list_servers","autocomplete_list_services","autocomplete_list_commands"}; + const std::set system_commands_always_available = {"help","edit"}; + const std::set system_commands_require_config = {"server","templates","create-service","create-template","create-server","ssh","list"}; + + bool is_no_arg_cmd(std::string cmd) + { + return system_commands_noargs.find(cmd) != system_commands_noargs.end(); + } + +} + bool dropshell::autocomplete(const std::vector &args) { if (args.size() < 3) // dropshell autocomplete ??? @@ -30,10 +43,8 @@ bool dropshell::autocomplete(const std::vector &args) return true; } - - std::string noargcmds[] = {"templates","autocomplete_list_servers","autocomplete_list_services","autocomplete_list_commands"}; - if (std::find(std::begin(noargcmds), std::end(noargcmds), cmd) != std::end(noargcmds)) - return true; + if (autocomplete::is_no_arg_cmd(cmd)) + return true; // no arguments needed. if (!dropshell::gConfig().is_config_set()) return false; // can't help without working config. @@ -89,14 +100,11 @@ bool dropshell::autocomplete_list_commands() dropshell::get_all_used_commands(commands); // add in commmands hard-coded and handled in main - commands.merge(std::set{ - "help","edit" // these are always available. - }); + commands.insert(autocomplete::system_commands_always_available.begin(), autocomplete::system_commands_always_available.end()); + if (dropshell::gConfig().is_config_set()) - commands.merge(std::set{ - "server","templates","create-service","create-template","create-server","ssh", - "list" // only if we have a config. - }); + commands.insert(autocomplete::system_commands_require_config.begin(), autocomplete::system_commands_require_config.end()); + for (const auto& command : commands) { std::cout << command << std::endl; }