Skip to content
Snippets Groups Projects
Commit a4d5c7af authored by Jan Trávníček's avatar Jan Trávníček
Browse files

use tclap to parse arguments and cleanup

parent 1b5449fd
No related branches found
No related tags found
No related merge requests found
//============================================================================
// 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);
......
......@@ -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;
}
}
......@@ -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;
}
}
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment