cplusminus/cpm/cxx/cpm.cxx

41 lines
1.3 KiB
C++

#include <algorithm>
#include <format>
#include <iostream>
// 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;
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";
}
std::cout << std::format("Transforming from {} to {}.\n", infile, outfile);
}