2021-05-10 05:32:47 +00:00
|
|
|
#include <iostream>
|
2021-05-10 06:15:33 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-08-18 11:57:54 +00:00
|
|
|
#include <stdexcept>
|
2021-05-10 05:16:10 +00:00
|
|
|
|
2021-05-10 06:15:33 +00:00
|
|
|
#include "helpers.hpp"
|
2021-05-10 05:16:10 +00:00
|
|
|
|
2021-08-18 11:57:54 +00:00
|
|
|
void printHelp() {
|
|
|
|
std::cout << "Usage:\n";
|
|
|
|
std::cout << "-w <width> - specify the width in integers\n";
|
|
|
|
std::cout << "--string <string> - input string to print within the "
|
|
|
|
"specified width";
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
|
2021-05-10 05:16:10 +00:00
|
|
|
int main(int argc, char **argv) {
|
2021-08-18 11:57:54 +00:00
|
|
|
Helpers::ParseArgsResultType parsed;
|
|
|
|
try {
|
|
|
|
parsed = Helpers::parseArgs(argc, argv, {}, {"w", "string"});
|
|
|
|
} catch(const std::invalid_argument &e) {
|
|
|
|
std::cout << "ERROR: invalid argument\n";
|
|
|
|
printHelp();
|
|
|
|
return 1;
|
|
|
|
}
|
2021-05-10 05:32:47 +00:00
|
|
|
|
2021-05-10 06:15:33 +00:00
|
|
|
// for(auto pair : parsed) {
|
|
|
|
// std::cout << pair.first << ", " << pair.second << std::endl;
|
|
|
|
// }
|
|
|
|
|
|
|
|
unsigned long long width;
|
|
|
|
if(auto iter = parsed.find("w"); iter != parsed.end()) {
|
|
|
|
width = std::stoull(iter->second);
|
|
|
|
std::cout << "Got width == " << width << std::endl;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string str;
|
|
|
|
if(auto iter = parsed.find("string"); iter != parsed.end()) {
|
|
|
|
str = iter->second;
|
|
|
|
std::cout << "Got string == \"" << str << "\"" << std::endl;
|
|
|
|
} else {
|
|
|
|
return 2;
|
2021-05-10 05:32:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 06:15:33 +00:00
|
|
|
Helpers::printDividers(width);
|
|
|
|
|
|
|
|
std::vector<std::string> tokens = Helpers::strSplit(str, {' ', '\n', '\r'});
|
|
|
|
|
|
|
|
// for(auto token : tokens) {
|
|
|
|
// std::cout << token << '\n';
|
|
|
|
// }
|
|
|
|
// std::cout << std::endl;
|
|
|
|
|
|
|
|
Helpers::printGreedyTextJustification(width, tokens);
|
|
|
|
|
|
|
|
Helpers::printDividers(width);
|
|
|
|
|
2021-05-10 05:16:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|