WIP: parse command line

This commit is contained in:
Glen Whitney 2022-12-28 12:12:45 -05:00
parent 13329f9f6b
commit 0c7eb0fe0c
1 changed files with 37 additions and 11 deletions

View File

@ -1,14 +1,40 @@
#include <algorithm>
#include <format>
#include <iostream>
int main (int argc, char* argv[])
{
using namespace std;
if (argc < 2)
{
cerr << "error: missing name" << endl;
return 1;
}
cout << "Hello, " << argv[1] << '!' << endl;
// 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);
}