fix mismatch between var name and program behavior, add "cellar translate" command

This commit is contained in:
Nicholas O'Connor 2019-05-15 12:08:33 -07:00
parent 8055a01608
commit 540ab70986
2 changed files with 27 additions and 8 deletions

View File

@ -1,2 +1,3 @@
translate translate_command Translates a path from Windows-y to POSIX or vice versa.
version print_version Prints version information. version print_version Prints version information.
help help_command Hello, there! help help_command Hello, there!

View File

@ -1,31 +1,35 @@
#include <exception> #include <exception>
#include <iostream>
#include <string> #include <string>
#include <vector>
#include <regex> #include <regex>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include "output.hpp" #include "output.hpp"
#include "paths.hpp" #include "paths.hpp"
#include "internal/core.hpp"
using namespace std;
using namespace cellar; using namespace cellar;
std::string cellar::paths::translate(std::string in_path, bool lazy) { string cellar::paths::translate(std::string in_path, bool lazy) {
bool posix_input; bool windows_input;
static std::regex drive_letter(R"([a-zA-Z]:\\)"); static regex drive_letter(R"([a-zA-Z]:\\)");
posix_input = ! std::regex_match(in_path.substr(0, 3), drive_letter); windows_input = regex_match(in_path.substr(0, 3), drive_letter);
if (!lazy) { if (!lazy) {
output::warning("non-lazy path translation is not implemented yet"); output::warning("non-lazy path translation is not implemented yet");
} }
if (posix_input) { if (windows_input) {
// lazy // lazy
if (boost::algorithm::to_lower_copy(in_path.substr(0,1)) != "z") { if (boost::algorithm::to_lower_copy(in_path.substr(0,1)) != "z") {
throw std::invalid_argument("lazy path translation isn't possible for drive letters other than Z:"); throw invalid_argument("lazy path translation isn't possible for drive letters other than Z:");
} }
std::string out_path = in_path.substr(2, in_path.size() - 2); string out_path = in_path.substr(2, in_path.size() - 2);
size_t slashpos = out_path.find("\\"); size_t slashpos = out_path.find("\\");
while (slashpos != std::string::npos) { while (slashpos != std::string::npos) {
@ -36,7 +40,7 @@ std::string cellar::paths::translate(std::string in_path, bool lazy) {
return out_path; return out_path;
} else { } else {
// lazy // lazy
std::string out_path = "Z:"; string out_path = "Z:";
out_path.append(in_path); out_path.append(in_path);
size_t slashpos = out_path.find("/"); size_t slashpos = out_path.find("/");
@ -48,3 +52,17 @@ std::string cellar::paths::translate(std::string in_path, bool lazy) {
return out_path; return out_path;
} }
} }
void cellar::core::translate_command(int argc, vector<string> argv) {
if (argv.size() < 2) {
output::error("pass an argument");
return;
}
// TODO: tclap me later
vector<string> pathargs(argv.cbegin() + 1, argv.cend());
for (auto arg : pathargs) {
cout << paths::translate(arg) << endl;
}
}