forked from glen/cplusminus
WIP: parse command line
This commit is contained in:
parent
13329f9f6b
commit
0c7eb0fe0c
@ -1,14 +1,40 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <format>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main (int argc, char* argv[])
|
// From https://stackoverflow.com/questions/865668
|
||||||
{
|
bool cmdOptionExists(char** begin, char** end, const std::string& option) {
|
||||||
using namespace std;
|
return std::find(begin, end, option) != end;
|
||||||
|
}
|
||||||
|
|
||||||
if (argc < 2)
|
int main (int argc, char* argv[]) {
|
||||||
{
|
const std::string stdflag = "-";
|
||||||
cerr << "error: missing name" << endl;
|
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
if (outfile.length() == 0) {
|
||||||
cout << "Hello, " << argv[1] << '!' << endl;
|
if (infile == stdflag) outfile = stdflag;
|
||||||
|
else outfile = infile.substr(0, infile.rfind('.')) + ".cxx";
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << std::format("Transforming from {} to {}.\n", infile, outfile);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user