StdInCommand(false) should work same as ArgumentsCommand() v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 09 Aug 2018 22:58:38 +0200
branchv_0
changeset 18 9e543fd0254c
parent 17 f543468a7e49
child 19 22f493401ac0
StdInCommand(false) should work same as ArgumentsCommand()
StdInCommand.h
relpipe-in-cli.cpp
--- a/StdInCommand.h	Sun Jul 29 14:42:50 2018 +0200
+++ b/StdInCommand.h	Thu Aug 09 22:58:38 2018 +0200
@@ -23,7 +23,8 @@
  */
 class StdInCommand : public Command {
 private:
-	wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
+	relpipe::writer::boolean_t readStdIn;
+	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
 
 	/**
 	 * Reads next value from arguments and (if no arguments left) from input
@@ -39,7 +40,7 @@
 
 		if (i < arguments.size()) {
 			return arguments[i++];
-		} else {
+		} else if (readStdIn) {
 			std::stringstream value;
 
 			while (true) {
@@ -48,16 +49,23 @@
 				if (ch == 0 || input.eof() || input.fail()) break;
 				else value << ch;
 			}
-			
+
 			if (required && value.str().empty()) throw RelpipeCLIException(L"Missing value on STDIN.", CLI::EXIT_CODE_BAD_SYNTAX);
 
 			return convertor.from_bytes(value.str());
+		} else {
+			if (required) throw RelpipeCLIException(L"Missing value on CLI.", CLI::EXIT_CODE_BAD_SYNTAX);
+			return L"";
 		}
 	}
 
 
 public:
 
+	StdInCommand(relpipe::writer::boolean_t readStdIn) :
+	readStdIn(readStdIn) {
+	}
+
 	void process(std::istream& input, std::ostream& output, const relpipe::writer::string_t& command, const std::vector<relpipe::writer::string_t>& arguments) override {
 		using namespace relpipe::writer;
 
@@ -80,7 +88,7 @@
 
 		while (true) {
 			string_t value = readNext(input, arguments, i, false);
-			if (value.empty() && (input.eof() || input.fail())) break;
+			if (value.empty() && (input.eof() || input.fail() || (!readStdIn && i >= arguments.size()))) break;
 			else writer->writeAttribute(value);
 			// TODO: check attribute count (avoid unfinished records)
 		}
--- a/relpipe-in-cli.cpp	Sun Jul 29 14:42:50 2018 +0200
+++ b/relpipe-in-cli.cpp	Thu Aug 09 22:58:38 2018 +0200
@@ -22,7 +22,8 @@
 	// TODO: help command
 	if (commandName == L"demo") return new DemoCommand();
 	else if (commandName == L"generate") return new ArgumentsCommand();
-	else if (commandName == L"generate-from-stdin") return new StdInCommand();
+	else if (commandName == L"generate-without-stdin") return new StdInCommand(false); // TODO: just for testing, StdInCommand(false) should work same as ArgumentsCommand()
+	else if (commandName == L"generate-from-stdin") return new StdInCommand(true);
 	else throw RelpipeCLIException(L"Unknown command: " + commandName, CLI::EXIT_CODE_UNKNOWN_COMMAND);
 }