From a4d5c7af3209d684640c731e2a98352869bbfdae Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Tue, 28 Oct 2014 20:57:08 +0100
Subject: [PATCH] use tclap to parse arguments and cleanup

---
 acat2/src/acat.cpp                 | 53 +++++++-----------
 aderivation2/src/aderivation.cpp   | 85 +++++++++++++++--------------
 aintegral2/src/aintegral.cpp       | 86 ++++++++++++++++--------------
 arun2/src/arun.cpp                 |  5 +-
 astat2/src/astat.cpp               |  9 +++-
 astringology2/src/astringology.cpp |  5 +-
 6 files changed, 126 insertions(+), 117 deletions(-)

diff --git a/acat2/src/acat.cpp b/acat2/src/acat.cpp
index c58cd7d705..7e89cf357c 100644
--- a/acat2/src/acat.cpp
+++ b/acat2/src/acat.cpp
@@ -1,50 +1,37 @@
-//============================================================================
-// Name        : acat.cpp
-// Author      : Martin Zak
-//============================================================================
+/*
+ * acat.cpp
+ *
+ *  Created on: 24. 2. 2014
+ *      Author: Martin Zak
+ */
 
 #include <iostream>
+#include <tclap/CmdLine.h>
 #include <string>
-#include <set>
-#include <cstdlib>
-#include <unistd.h>
-#include <exception>
 #include <exception/AlibException.h>
 #include <factory/DataFactory.hpp>
 #include <sax/SaxParseInterface.h>
 #include <sax/ParserException.h>
 #include <object/Object.h>
-#include <std/list.hpp>
-
-
-#define VERSION "0.0.1"
-#define USAGE "Usage: catPDA [-c] [input]";
 
 int main(int argc, char** argv) {
-	if(argc == 2 && std::string(argv[1])=="-v" ) {
-			std::cout << argv[0] << " version " << VERSION << "\n";
-			std::cout << USAGE;
-			std::cout << std::endl;
-			return EXIT_SUCCESS;
-		}
+	try {
+		TCLAP::CmdLine cmd("Regexp derivative compute binary", ' ', "0.01");
 
-	int i=getopt(argc, argv, "t:");
-	while(i!=-1) {
-		switch(i){
-		default:
-			optind--;
-			break;
-		}
-		i=getopt(argc, argv, "t:");
-	}
+		TCLAP::UnlabeledValueArg<std::string> input(	"input",	"Input to cat",		false,	"-",		"file");
+		cmd.add( input );
+
+		cmd.parse(argc, argv);
 
-	try {
 		std::list<sax::Token> tokens;
-		if(optind == argc) {
-			std::string input(std::istreambuf_iterator<char>(std::cin), std::istreambuf_iterator<char>());
-			sax::SaxParseInterface::parseMemory(input, tokens);
+		if(input.isSet()) {
+			if(input.getValue() == "-") {
+				sax::SaxParseInterface::parseStdin(tokens);
+			} else {
+				sax::SaxParseInterface::parseFile(input.getValue(), tokens);
+			}
 		} else {
-			sax::SaxParseInterface::parseFile(argv[optind],tokens);
+			sax::SaxParseInterface::parseStdin(tokens);
 		}
 
 		alib::Object object = alib::DataFactory::fromTokens<alib::Object>(tokens);
diff --git a/aderivation2/src/aderivation.cpp b/aderivation2/src/aderivation.cpp
index b3021c88dc..b09584aa34 100644
--- a/aderivation2/src/aderivation.cpp
+++ b/aderivation2/src/aderivation.cpp
@@ -6,50 +6,55 @@
  */
 
 #include <iostream>
+#include <tclap/CmdLine.h>
 
 #include <factory/DataFactory.hpp>
 #include <exception/AlibException.h>
 
 #include "regexp/transform/RegExpDerivation.h"
 
-int main(int argc, char** argv)
-{
-    try
-    {
-        if(argc == 2 && std::string("-h").compare(argv[1]) == 0)
-        {
-            std::cout << "aderivation. Derivates regular expression by given string." << std::endl;
-            std::cout << "Usage: aderivation regexp.xml string.xml" << std::endl;
-            return -1;
-        }
-
-        if(argc == 2)
-        {
-            regexp::RegExp regexp = alib::DataFactory::fromStdin<regexp::RegExp>();
-            string::LinearString* string = static_cast<string::LinearString*>(std::move(alib::DataFactory::fromFile<string::LinearString>(argv[1])).plunder());
-            alib::DataFactory::toStdout(regexp::RegExpDerivation::derivation(regexp, *string));
-            delete string;
-            return 0;
-        }
-
-        if(argc == 3)
-        {
-            regexp::RegExp regexp = alib::DataFactory::fromFile<regexp::RegExp>(argv[1]);
-            string::LinearString* string = static_cast<string::LinearString*>(std::move(alib::DataFactory::fromFile<string::LinearString>(argv[2])).plunder());
-            alib::DataFactory::toStdout(regexp::RegExpDerivation::derivation(regexp, *string));
-            delete string;
-            return 0;
-        }
-
-        return -1;
-    }
-    catch(const exception::AlibException& exception)
-    {
-        alib::DataFactory::toStdout(exception);
-        return 1;
-    }
-    catch(...)
-    {
-        return 127;
-    }
+int main(int argc, char** argv) {
+	try {
+		TCLAP::CmdLine cmd("Regexp derivative compute binary", ' ', "0.01");
+		TCLAP::ValueArg<std::string> string(	"s",	"string",	"String to derivate by",	false,	"-",		"file");
+		cmd.add( string );
+
+		TCLAP::ValueArg<std::string> regexp(	"r",	"regexp",	"Regexp to derivate",		false,	"-",		"file");
+		cmd.add( regexp );
+
+		cmd.parse(argc, argv);
+
+		std::list<sax::Token> stringTokens;
+		if(string.isSet()) {
+			if(string.getValue() == "-") {
+				sax::SaxParseInterface::parseStdin(stringTokens);
+			} else {
+				sax::SaxParseInterface::parseFile(string.getValue(), stringTokens);
+			}
+		}
+
+		std::list<sax::Token> regexpTokens;
+		if(regexp.isSet()) {
+			if(regexp.getValue() == "-") {
+				sax::SaxParseInterface::parseStdin(regexpTokens);
+			} else {
+				sax::SaxParseInterface::parseFile(regexp.getValue(), regexpTokens);
+			}
+		}
+
+		if(false){
+			regexp::RegExp regexp = alib::DataFactory::fromTokens<regexp::RegExp>(regexpTokens);
+			string::LinearString string = alib::DataFactory::fromTokens<string::LinearString>(stringTokens);
+			alib::DataFactory::toStdout(regexp::RegExpDerivation::derivation(regexp, string));
+			return 0;
+		}
+	} catch(const exception::AlibException& exception) {
+		alib::DataFactory::toStdout(exception);
+		return 1;
+	} catch(const TCLAP::ArgException& exception) {
+		std::cout << exception.error() << std::endl;
+		return 2;
+	} catch(...) {
+		return 127;
+	}
 }
diff --git a/aintegral2/src/aintegral.cpp b/aintegral2/src/aintegral.cpp
index 0a4d0e9a56..0fc59797f7 100644
--- a/aintegral2/src/aintegral.cpp
+++ b/aintegral2/src/aintegral.cpp
@@ -6,51 +6,55 @@
  */
 
 #include <iostream>
+#include <tclap/CmdLine.h>
 
 #include <factory/DataFactory.hpp>
 #include <exception/AlibException.h>
 
 #include "regexp/transform/RegExpIntegral.h"
 
-int main(int argc, char** argv)
-{
-    try
-    {
-        if(argc == 2 && std::string("-h").compare(argv[1]) == 0)
-        {
-            std::cout << "aintegral. Integrates regular expression by given string." << std::endl;
-            std::cout << "Usage: aintegral regexp.xml string.xml" << std::endl;
-            return -1;
-        }
-
-        if(argc == 2)
-        {
-            regexp::RegExp regexp = alib::DataFactory::fromStdin<regexp::RegExp>();
-            string::LinearString* string = static_cast<string::LinearString*>(std::move(alib::DataFactory::fromFile<string::LinearString>(argv[1])).plunder());
-            alib::DataFactory::toStdout(regexp::RegExpIntegral::integral(regexp, *string));
-            delete string;
-            return 0;
-        }
-
-        if(argc == 3)
-        {
-            regexp::RegExp regexp = alib::DataFactory::fromFile<regexp::RegExp>(argv[1]);
-            string::LinearString* string = static_cast<string::LinearString*>(std::move(alib::DataFactory::fromFile<string::LinearString>(argv[2])).plunder());
-            alib::DataFactory::toStdout(regexp::RegExpIntegral::integral(regexp, *string));
-            delete string;
-            return 0;
-        }
-
-        return -1;
-
-    }
-    catch(const exception::AlibException& exception)
-    {
-        alib::DataFactory::toStdout(exception);
-        return 1;
-    }
-    catch(...)
-    {
-        return 127;
-    }
+int main(int argc, char** argv) {
+	try {
+		TCLAP::CmdLine cmd("Regexp derivative compute binary", ' ', "0.01");
+		TCLAP::ValueArg<std::string> string(	"s",	"string",	"String to derivate by",	false,	"-",		"file");
+		cmd.add( string );
+
+		TCLAP::ValueArg<std::string> regexp(	"r",	"regexp",	"Regexp to derivate",		false,	"-",		"file");
+		cmd.add( regexp );
+
+		cmd.parse(argc, argv);
+
+		std::list<sax::Token> stringTokens;
+		if(string.isSet()) {
+			if(string.getValue() == "-") {
+				sax::SaxParseInterface::parseStdin(stringTokens);
+			} else {
+				sax::SaxParseInterface::parseFile(string.getValue(), stringTokens);
+			}
+		}
+
+		std::list<sax::Token> regexpTokens;
+		if(regexp.isSet()) {
+			if(regexp.getValue() == "-") {
+				sax::SaxParseInterface::parseStdin(regexpTokens);
+			} else {
+				sax::SaxParseInterface::parseFile(regexp.getValue(), regexpTokens);
+			}
+		}
+
+		if(false){
+			regexp::RegExp regexp = alib::DataFactory::fromTokens<regexp::RegExp>(regexpTokens);
+			string::LinearString string = alib::DataFactory::fromTokens<string::LinearString>(stringTokens);
+			alib::DataFactory::toStdout(regexp::RegExpIntegral::integral(regexp, string));
+			return 0;
+		}
+	} catch(const exception::AlibException& exception) {
+		alib::DataFactory::toStdout(exception);
+		return 1;
+	} catch(const TCLAP::ArgException& exception) {
+		std::cout << exception.error() << std::endl;
+		return 2;
+	} catch(...) {
+		return 127;
+	}
 }
diff --git a/arun2/src/arun.cpp b/arun2/src/arun.cpp
index 9903f06ad4..6982d2a0ee 100644
--- a/arun2/src/arun.cpp
+++ b/arun2/src/arun.cpp
@@ -10,13 +10,13 @@
 
 int main(int argc, char* argv[]) {
 	try {
+		TCLAP::CmdLine cmd("Automaton run binary", ' ', "0.01");
 
 		std::vector<std::string> allowed;
 		allowed.push_back("occurrences");
 		allowed.push_back("accept");
 		TCLAP::ValuesConstraint<std::string> allowedVals( allowed );
 
-		TCLAP::CmdLine cmd("Automaton run binary", ' ', "0.01");
 		TCLAP::ValueArg<std::string> type(	"t",	"type",		"run type",		false,	"accept",	&allowedVals);
 		cmd.add(type);
 
@@ -72,6 +72,9 @@ int main(int argc, char* argv[]) {
 	} catch( const exception::AlibException & e ) {
 		alib::DataFactory::toStdout( e );
 		return 1;
+	} catch(const TCLAP::ArgException& exception) {
+		std::cout << exception.error() << std::endl;
+		return 2;
 	} catch (...) {
 		return 127;
 	}
diff --git a/astat2/src/astat.cpp b/astat2/src/astat.cpp
index bab80d56dc..8fb37c0d12 100644
--- a/astat2/src/astat.cpp
+++ b/astat2/src/astat.cpp
@@ -9,6 +9,7 @@
 
 int main(int argc, char* argv[]) {
 	try {
+		TCLAP::CmdLine cmd("Prints usefull information about automata, grammars, regexps, ...", ' ', "0.01");
 
 		std::vector<std::string> allowed;
 		allowed.push_back("noop");
@@ -17,15 +18,18 @@ int main(int argc, char* argv[]) {
 		allowed.push_back("both");
 		TCLAP::ValuesConstraint<std::string> allowedVals( allowed );
 
-		TCLAP::CmdLine cmd("Prints usefull information about automata, grammars, regexps, ...", ' ', "0.01");
 		TCLAP::ValueArg<std::string> states(		"s",	"states",		"Print or display number of states",			false,	"noop",		&allowedVals);
 		cmd.add(states);
+
 		TCLAP::ValueArg<std::string> finalStates(	"f",	"finalStates",		"Print or display number of final states",		false,	"noop",		&allowedVals);
 		cmd.add(finalStates);
+
 		TCLAP::ValueArg<std::string> initialStates(	"i",	"initialStates",	"Print or display number of initial states",		false,	"noop",		&allowedVals);
 		cmd.add(initialStates);
+
 		TCLAP::ValueArg<std::string> alphabet(		"a",	"alphabet",		"Print or display number of symbols in alphabet",	false,	"noop",		&allowedVals);
 		cmd.add(alphabet);
+
 		TCLAP::ValueArg<std::string> transitions(	"t",	"transitions",		"Print or display number of transitions",		false,	"noop",		&allowedVals);
 		cmd.add(transitions);
 
@@ -124,6 +128,9 @@ int main(int argc, char* argv[]) {
 	} catch( const exception::AlibException & e ) {
 		alib::DataFactory::toStdout( e );
 		return 1;
+	} catch(const TCLAP::ArgException& exception) {
+		std::cout << exception.error() << std::endl;
+		return 2;
 	} catch (...) {
 		return 127;
 	}
diff --git a/astringology2/src/astringology.cpp b/astringology2/src/astringology.cpp
index f839b21b4f..b430d27fc5 100644
--- a/astringology2/src/astringology.cpp
+++ b/astringology2/src/astringology.cpp
@@ -10,13 +10,13 @@
 
 int main(int argc, char* argv[]) {
 	try {
+		TCLAP::CmdLine cmd("Stringology algorithm access binary", ' ', "0.01");
 
 		std::vector<std::string> allowed;
 		allowed.push_back("exactMatchingAutomaton");
 		allowed.push_back("exactMatch");
 		TCLAP::ValuesConstraint<std::string> allowedVals( allowed );
 
-		TCLAP::CmdLine cmd("Stringology algorithm access binary", ' ', "0.01");
 		TCLAP::ValueArg<std::string> algorithm(	"a",	"algorithm",	"Execute algorithm",		false,	"exactMatch",	&allowedVals);
 		cmd.add(algorithm);
 
@@ -66,6 +66,9 @@ int main(int argc, char* argv[]) {
 	} catch( const exception::AlibException & e ) {
 		alib::DataFactory::toStdout( e );
 		return 1;
+	} catch(const TCLAP::ArgException& exception) {
+		std::cout << exception.error() << std::endl;
+		return 2;
 	} catch (...) {
 		return 127;
 	}
-- 
GitLab