add options to command

This commit is contained in:
Julian Daube 2017-10-09 13:56:48 +02:00
parent 4382ad6d59
commit 98b44ec923

103
main.cpp
View File

@ -280,37 +280,118 @@ InputExtractor::List InputExtractor::operator()(const Path::Path &file, const Me
return result;
}
struct Config {
std::string targetName;
Path::Path outfilePath;
bool outfileOverride;
} config ;
std::ostream &writeTarget(std::ostream &stream, const std::string &target_name) {
stream << target_name << ":";
return stream;
}
std::ostream &writeTargetDepends(std::ostream &stream, const Path::Path &path) {
stream << path << "\\\n";
return stream;
}
int openOutfile(std::ofstream &stream) {
cout << "writing dependency rules to " << config.outfilePath << endl;
stream.close();
stream.open(config.outfilePath);
if (!stream) {
cerr << "could not write to " << config.outfilePath << endl;
return 0;
}
if (!config.targetName.empty()) {
writeTarget(stream, config.targetName);
}
return 1;
}
#include <getopt.h>
struct option long_opts[] = {
{"output", required_argument, NULL, 'o'},
{"target", required_argument, NULL, 't'},
{}, // terminator
};
void help(int argc, char ** args) {
cout << args[0] << " [-o output] files..." << endl;
cout << "--output" << endl;
cout << "-o\tby default the program will create a depfile for every input" << endl;
cout << "\tnaming it like the tex file with a .d extension. By giving the -o Option" << endl;
cout << "\tthe output fill instead be put in this file exclusivly" << endl;
}
int main(int argc, char ** args) {
// find all the files the given tex files depend on
int fd = 0;
struct stat filestat;
int long_index = 0;
int opt = 0;
while((opt = getopt_long(argc, args, "o:t:", long_opts, &long_index)) != -1) {
switch(opt) {
case 'o':
config.outfilePath = optarg;
config.outfileOverride = true;
break;
case 't':
config.targetName = optarg;
break;
case '?':
help(argc, args);
return 0;
}
}
for(;argc > 1; --argc) {
std::ofstream outfile;
Path::Path filename = args[argc-1];
if (!config.outfilePath.empty() && !openOutfile(outfile)) {
return -1; // failed to open outfile
}
// scan remaining arguments as input filenames
for(; optind < argc; optind++) {
Path::Path filename = args[optind];
InputExtractor parser;
// parse file
InputExtractor::List list = parser.Include(filename);
// output results in makefile rule style
Path::Path outfile_name = Path::Basename(filename) + ".d";
cout << "writing dependecy rules to " << outfile_name << "...";
// check for open outfile
if (!config.outfileOverride) {
config.outfilePath = Path::Basename(filename) + ".d";
std::ofstream outfile(outfile_name);
if (!outfile) {
cout << "could not create file!" << endl;
continue;
if (!openOutfile(outfile)) {
continue; // just skip this file
}
}
outfile << filename << ":";
// check for target override
if (config.targetName.empty()) {
config.targetName = filename;
}
for (auto it = list.begin(); it != list.end(); it++) {
outfile << *it << "\t\\\n";
writeTargetDepends(outfile, *it);
}
// add newline for cleanness
outfile << endl;
cout << "done" << endl;
}