aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Karatarakis <alkarata@microsoft.com>2017-10-13 14:54:31 -0700
committerAlexander Karatarakis <alkarata@microsoft.com>2017-10-13 14:54:31 -0700
commitf6a0d78c82789af482eb3c885cbbb73aba7dfb38 (patch)
tree4db7ff5dda752482d36b0afc8ce4cc47a070be97
parent9e19c24d2911ff1ef648aeed0c596e3987eafebf (diff)
downloadvcpkg-f6a0d78c82789af482eb3c885cbbb73aba7dfb38.tar.gz
vcpkg-f6a0d78c82789af482eb3c885cbbb73aba7dfb38.zip
`autocomplete` Rework command, fixing a lot of corner cases in the process
-rw-r--r--toolsrc/include/vcpkg_Commands.h8
-rw-r--r--toolsrc/src/commands_autocomplete.cpp66
2 files changed, 34 insertions, 40 deletions
diff --git a/toolsrc/include/vcpkg_Commands.h b/toolsrc/include/vcpkg_Commands.h
index 7cfa2760e..c9a33a8f4 100644
--- a/toolsrc/include/vcpkg_Commands.h
+++ b/toolsrc/include/vcpkg_Commands.h
@@ -194,10 +194,10 @@ namespace vcpkg::Commands
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
}
- namespace Autocomplete
- {
- void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
- }
+ namespace Autocomplete
+ {
+ void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths);
+ }
namespace Help
{
diff --git a/toolsrc/src/commands_autocomplete.cpp b/toolsrc/src/commands_autocomplete.cpp
index abad19df7..478a3c478 100644
--- a/toolsrc/src/commands_autocomplete.cpp
+++ b/toolsrc/src/commands_autocomplete.cpp
@@ -5,6 +5,7 @@
#include "vcpkg_Commands.h"
#include "vcpkg_System.h"
#include "vcpkglib.h"
+#include <regex>
namespace vcpkg::Commands::Autocomplete
{
@@ -44,20 +45,26 @@ namespace vcpkg::Commands::Autocomplete
return results;
}
+ [[noreturn]] static void output_sorted_results_and_exit(const LineInfo& line_info,
+ std::vector<std::string>&& results)
+ {
+ const SortedVector<std::string> sorted_results(results);
+ System::println(Strings::join("\n", sorted_results));
+ Checks::exit_success(line_info);
+ }
+
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
Metrics::g_metrics.lock()->set_send_metrics(false);
+ const std::string to_autocomplete = Strings::join(" ", args.command_arguments);
+ const std::vector<std::string> tokens = Strings::split(to_autocomplete, " ");
- if (args.command_arguments.size() != 1)
- {
- Checks::exit_success(VCPKG_LINE_INFO);
- }
+ std::smatch match;
- const std::string to_autocomplete = args.command_arguments.at(0);
- const std::vector<std::string> tokens = Strings::split(to_autocomplete, " ");
- if (tokens.size() == 1)
+ // Handles vcpkg <command>
+ if (std::regex_match(to_autocomplete, match, std::regex{R"###(^(\S*)$)###"}))
{
- const std::string requested_command = tokens[0];
+ const std::string requested_command = match[1].str();
std::vector<std::string> valid_commands = {"install",
"search",
@@ -79,38 +86,25 @@ namespace vcpkg::Commands::Autocomplete
return Strings::case_insensitive_ascii_starts_with(s, requested_command);
});
- if (valid_commands.size() == 1)
- {
- System::println(valid_commands[0] + " ");
- }
- else
- {
- System::println(Strings::join("\n", valid_commands));
- }
-
- Checks::exit_success(VCPKG_LINE_INFO);
+ output_sorted_results_and_exit(VCPKG_LINE_INFO, std::move(valid_commands));
}
- if (tokens.size() == 2)
+ // Handles vcpkg install <package>
+ if (std::regex_match(to_autocomplete, match, std::regex{R"###(^install.* (\S*)$)###"}))
{
- const std::string requested_command = tokens[0];
- const std::string start_with = tokens[1];
- std::vector<std::string> results;
- if (requested_command == "install")
- {
- auto sources_and_errors = Paragraphs::try_load_all_ports(paths.get_filesystem(), paths.ports);
- auto& source_paragraphs = sources_and_errors.paragraphs;
-
- results = autocomplete_install(source_paragraphs, start_with);
- }
- else if (requested_command == "remove")
- {
- const StatusParagraphs status_db = database_load_check(paths);
- const std::vector<StatusParagraph*> installed_packages = get_installed_ports(status_db);
- results = autocomplete_remove(installed_packages, start_with);
- }
+ const std::string start_with = match[1].str();
+ auto sources_and_errors = Paragraphs::try_load_all_ports(paths.get_filesystem(), paths.ports);
+ auto& source_paragraphs = sources_and_errors.paragraphs;
+ output_sorted_results_and_exit(VCPKG_LINE_INFO, autocomplete_install(source_paragraphs, start_with));
+ }
- System::println(Strings::join("\n", results));
+ // Handles vcpkg remove <package>
+ if (std::regex_match(to_autocomplete, match, std::regex{R"###(^remove.* (\S*)$)###"}))
+ {
+ const std::string start_with = match[1].str();
+ const StatusParagraphs status_db = database_load_check(paths);
+ const std::vector<StatusParagraph*> installed_packages = get_installed_ports(status_db);
+ output_sorted_results_and_exit(VCPKG_LINE_INFO, autocomplete_remove(installed_packages, start_with));
}
Checks::exit_success(VCPKG_LINE_INFO);