From dc2e52da1dfe72570863851c4da014bf5a6d4703 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Thu, 3 Aug 2017 08:02:35 +0200
Subject: [PATCH] add set command to cli

---
 alib2cli/src/command/SetCommand.h  | 34 ++++++++++++++++++++++++++++++
 alib2cli/src/parser/Parser.cpp     |  8 +++++++
 alib2std/src/extensions/string.cpp |  8 +++++++
 alib2std/src/extensions/string.hpp |  3 +++
 4 files changed, 53 insertions(+)
 create mode 100644 alib2cli/src/command/SetCommand.h

diff --git a/alib2cli/src/command/SetCommand.h b/alib2cli/src/command/SetCommand.h
new file mode 100644
index 0000000000..12e01e25ce
--- /dev/null
+++ b/alib2cli/src/command/SetCommand.h
@@ -0,0 +1,34 @@
+#ifndef _CLI_SET_COMMAND_H_
+#define _CLI_SET_COMMAND_H_
+
+#include <command/Command.h>
+#include <environment/Environment.h>
+#include <global/GlobalData.h>
+
+namespace cli {
+
+class SetCommand : public Command {
+	std::string m_param;
+	std::string m_value;
+
+public:
+	SetCommand ( std::string param, std::string value ) : m_param ( std::move ( param ) ), m_value ( std::move ( value ) ) {
+	}
+
+	virtual Command::Result run ( Environment & ) const override {
+		if ( m_param == "verbose" ) {
+			common::GlobalData::verbose = std::from_string < bool > ( m_value );
+		} else if ( m_param == "measure" ) {
+			common::GlobalData::measure = std::from_string < bool > ( m_value );
+		} else if ( m_param == "optimizeXml" ) {
+			common::GlobalData::optimizeXml = std::from_string < bool > ( m_value );
+		} else {
+			std::cout << "The set parameter " << m_param << " does not exist." << std::endl;
+		}
+		return Command::Result::OK;
+	}
+};
+
+} /* namespace cli */
+
+#endif /* _CLI_SET_COMMAND_H_ */
diff --git a/alib2cli/src/parser/Parser.cpp b/alib2cli/src/parser/Parser.cpp
index 8980f01dbe..556c62979a 100644
--- a/alib2cli/src/parser/Parser.cpp
+++ b/alib2cli/src/parser/Parser.cpp
@@ -24,6 +24,7 @@
 #include <command/QuitCommand.h>
 #include <command/HelpCommand.h>
 #include <command/IntrospectionCommand.h>
+#include <command/SetCommand.h>
 
 #include <primitive/Integer.h>
 #include <primitive/String.h>
@@ -196,6 +197,13 @@ std::unique_ptr < Command > Parser::parse ( ) {
 		std::string command = optional_identifier ( );
 		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < IntrospectionCommand > ( std::move ( command ) );
+	} else if ( check_nonreserved_kw ( "set" ) ) {
+		match_nonreserved_kw ( "set" );
+		std::string param = matchIdentifier ( );
+		std::string value = getTokenValue ( );
+		match ( cli::Lexer::TokenType::INTEGER, cli::Lexer::TokenType::IDENTIFIER );
+		match ( cli::Lexer::TokenType::END );
+		return std::make_unique < SetCommand > ( std::move ( param ), std::move ( value ) );
 	} else {
 		throw exception::CommonException ( "Mismatched set while expanding param rule." );
 	}
diff --git a/alib2std/src/extensions/string.cpp b/alib2std/src/extensions/string.cpp
index 1d51dc08a6..dc3fd4ca33 100644
--- a/alib2std/src/extensions/string.cpp
+++ b/alib2std/src/extensions/string.cpp
@@ -25,6 +25,14 @@ int from_string ( const string & value ) {
 	return stoi__private ( value.c_str() );
 }
 
+template < >
+bool from_string ( const string & value ) {
+	if ( value == "true" || value == "1" )
+		return true;
+	else
+		return false;
+}
+
 template < >
 long from_string ( const string & value ) {
 	return stol__private ( value.c_str() );
diff --git a/alib2std/src/extensions/string.hpp b/alib2std/src/extensions/string.hpp
index 1d9749723b..1f411754eb 100644
--- a/alib2std/src/extensions/string.hpp
+++ b/alib2std/src/extensions/string.hpp
@@ -39,6 +39,9 @@ string from_string ( const string & value );
 template < >
 int from_string ( const string & value );
 
+template < >
+bool from_string ( const string & value );
+
 template < >
 long from_string ( const string & value );
 
-- 
GitLab