2022-12-28 17:12:45 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <format>
|
2022-12-18 21:50:01 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2022-12-28 17:12:45 +00:00
|
|
|
// From https://stackoverflow.com/questions/865668
|
|
|
|
bool cmdOptionExists(char** begin, char** end, const std::string& option) {
|
|
|
|
return std::find(begin, end, option) != end;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char* argv[]) {
|
|
|
|
const std::string stdflag = "-";
|
|
|
|
std::string infile = stdflag;
|
|
|
|
std::string outfile = "";
|
|
|
|
bool showHelp = false;
|
2022-12-18 21:50:01 +00:00
|
|
|
|
2022-12-28 17:12:45 +00:00
|
|
|
if (cmdOptionExists(argv, argv+argc, "-h")) showHelp = true;
|
|
|
|
else if (cmdOptionExists(argv, argv+argc, "--help")) showHelp = true;
|
|
|
|
else switch (argc) {
|
|
|
|
case 3: outfile = argv[2]; /* DROP */
|
|
|
|
case 2: infile = argv[1]; /* DROP */
|
|
|
|
case 1: break;
|
|
|
|
default: showHelp = true;
|
|
|
|
}
|
|
|
|
if (showHelp) {
|
|
|
|
std::cerr << R"(
|
|
|
|
Usage: cpm [infile [outfile]]
|
|
|
|
If infile is not specified or is '-', stdin is used.
|
|
|
|
If outfile is '-' or unspecified and infile is stdin, stdout is used.
|
|
|
|
Otherwise if outfile is unspecified, the name of the infile with its
|
|
|
|
final '.' extension (if any) removed and '.cxx' added is used.
|
|
|
|
)";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (outfile.length() == 0) {
|
|
|
|
if (infile == stdflag) outfile = stdflag;
|
|
|
|
else outfile = infile.substr(0, infile.rfind('.')) + ".cxx";
|
|
|
|
}
|
2022-12-18 21:50:01 +00:00
|
|
|
|
2022-12-28 17:12:45 +00:00
|
|
|
std::cout << std::format("Transforming from {} to {}.\n", infile, outfile);
|
2022-12-18 21:50:01 +00:00
|
|
|
}
|