From 98b44ec9234385f88a563f39cac9a52c25b6996e Mon Sep 17 00:00:00 2001 From: Julian Daube Date: Mon, 9 Oct 2017 13:56:48 +0200 Subject: [PATCH] add options to command --- main.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index d6dd061..77c4bf1 100644 --- a/main.cpp +++ b/main.cpp @@ -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 + +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; }