added option to take data from parameter instead of stdin

development
Michael Ochmann 2 years ago
parent a1a3ada1f9
commit eeaf910c27
  1. 40
      main.cpp

@ -2,6 +2,7 @@
#include <getopt.h> #include <getopt.h>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "QR.hpp" #include "QR.hpp"
#define UNUSED(var) (void) var; #define UNUSED(var) (void) var;
@ -17,6 +18,7 @@ Released under MIT license.
Usage: Usage:
-f --format output file format. can be one of "cli, png, svg" -f --format output file format. can be one of "cli, png, svg"
-h --help show this help -h --help show this help
-i --input take data from this argument instead of stdin
-o --output output file name -o --output output file name
-s --size desired output file size in pixels -s --size desired output file size in pixels
-t --type output QR code type. can be one of "small, medium, large" -t --type output QR code type. can be one of "small, medium, large"
@ -32,6 +34,7 @@ int main(int argc, char* argv[]) {
} }
const option options[] = { const option options[] = {
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
{"input", required_argument, nullptr, 'i'},
{"output", required_argument, nullptr, 'o'}, {"output", required_argument, nullptr, 'o'},
{"size", required_argument, nullptr, 's'}, {"size", required_argument, nullptr, 's'},
{"type", required_argument, nullptr, 't'}, {"type", required_argument, nullptr, 't'},
@ -43,10 +46,12 @@ int main(int argc, char* argv[]) {
size_t segmentSize = 0; size_t segmentSize = 0;
massivedynamic::Type type = massivedynamic::Type::MEDIUM; massivedynamic::Type type = massivedynamic::Type::MEDIUM;
massivedynamic::Format format = massivedynamic::Format::CONSOLE; massivedynamic::Format format = massivedynamic::Format::CONSOLE;
bool fromStdin = true;
std::string paramData = "";
for(;;) { for(;;) {
int index = -1; int index = -1;
int result = getopt_long(argc, argv, "ho:s:t:f:", options, &index); int result = getopt_long(argc, argv, "ho:s:t:f:i:", options, &index);
if (result == -1) if (result == -1)
break; break;
@ -90,6 +95,11 @@ int main(int argc, char* argv[]) {
} }
break; break;
} }
case 'i': {
fromStdin = false;
paramData = optarg;
break;
}
case 'h': case 'h':
default: default:
printHelp(); printHelp();
@ -100,23 +110,31 @@ int main(int argc, char* argv[]) {
std::string line; std::string line;
std::stringstream data; std::stringstream data;
if (!isatty(fileno(stdin))) { if (fromStdin) {
while (std::getline(std::cin, line)) { if (!isatty(fileno(stdin))) {
if (std::cin.eof() || line.empty()) { while (std::getline(std::cin, line)) {
std::cin.clear(); if (std::cin.eof() || line.empty()) {
break; std::cin.clear();
break;
}
data << line;
} }
data << line;
} }
}
if (isatty(fileno(stdin)) || data.str().empty()) { if (isatty(fileno(stdin)) || data.str().empty()) {
std::cerr << "ERROR: no data from stdinput." << std::endl; std::cerr << "ERROR: no data from stdinput." << std::endl;
exit(1);
}
} else
data << paramData;
if (data.str().empty()) {
std::cerr << "ERROR: input data is empty" << std::endl;
exit(1); exit(1);
} }
if ((format == massivedynamic::Format::PNG || format == massivedynamic::Format::SVG) && outputFile.empty()) { if ((format == massivedynamic::Format::PNG || format == massivedynamic::Format::SVG) && outputFile.empty()) {
std::cerr << "output file name (-o, --output) can not be empty" << std::endl; std::cerr << "ERROR: output file name (-o, --output) can not be empty" << std::endl;
return 1; return 1;
} }

Loading…
Cancel
Save