From 1d4fd8e7306af64fc3f3473206eb7b2c0ba3c4c1 Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Wed, 2 Aug 2017 13:44:01 +0200
Subject: [PATCH] add stub for help command in cli

---
 alib2cli/src/command/HelpCommand.h | 45 ++++++++++++++++++++++++++++++
 alib2cli/src/parser/Parser.cpp     | 14 ++++++++++
 alib2cli/src/parser/Parser.h       |  2 ++
 3 files changed, 61 insertions(+)
 create mode 100644 alib2cli/src/command/HelpCommand.h

diff --git a/alib2cli/src/command/HelpCommand.h b/alib2cli/src/command/HelpCommand.h
new file mode 100644
index 0000000000..df9fcbf3bc
--- /dev/null
+++ b/alib2cli/src/command/HelpCommand.h
@@ -0,0 +1,45 @@
+#ifndef _CLI_HELP_COMMAND_H_
+#define _CLI_HELP_COMMAND_H_
+
+#include <command/Command.h>
+#include <environment/Environment.h>
+
+namespace cli {
+
+class HelpCommand : public Command {
+	std::string m_command;
+
+public:
+	HelpCommand ( std::string command ) : m_command ( std::move ( command ) ) {
+	}
+
+	virtual Command::Result run ( Environment & ) const override {
+		if ( m_command == "execute" ) {
+			std::cout << "Execute command executes statements and either prints the result or writes the result to a file" << std::endl;
+		} else if ( m_command == "export" ) {
+			std::cout << "Export command stores the result of statements in a variable" << std::endl;
+			std::cout << "" << std::endl;
+			std::cout << "Syntax:" << std::endl;
+			std::cout << "execute variable = statement_list" << std::endl;
+			std::cout << "" << std::endl;
+			std::cout << "variable is an identifier under which the result should be stored." << std::endl;
+			std::cout << "for details of syntax of statement_list see help of execute statement." << std::endl;
+		} else if ( m_command == "" ) {
+			std::cout << "Simple help for the query language" << std::endl;
+			std::cout << "" << std::endl;
+			std::cout << "command quit: quits the processor." << std::endl;
+			std::cout << "command help: shows this help." << std::endl;
+			std::cout << "command export: stores the result of statements into a variable" << std::endl;
+			std::cout << "command execute: executes statements" << std::endl;
+			std::cout << "" << std::endl;
+			std::cout << "for details use help of individual command" << std::endl;
+		} else {
+			std::cout << "The command " << m_command << " either does not exist or does not have a help entry." << std::endl;
+		}
+		return Command::Result::OK;
+	}
+};
+
+} /* namespace cli */
+
+#endif /* _CLI_HELP_COMMAND_H_ */
diff --git a/alib2cli/src/parser/Parser.cpp b/alib2cli/src/parser/Parser.cpp
index 7a0a666ef9..839fc620c9 100644
--- a/alib2cli/src/parser/Parser.cpp
+++ b/alib2cli/src/parser/Parser.cpp
@@ -19,6 +19,7 @@
 #include <command/ExecuteCommand.h>
 #include <command/ExportCommand.h>
 #include <command/QuitCommand.h>
+#include <command/HelpCommand.h>
 
 #include <primitive/Integer.h>
 #include <primitive/String.h>
@@ -144,6 +145,14 @@ void Parser::result ( std::shared_ptr < StatementList > & list ) {
 	}
 }
 
+std::string Parser::help_parameter ( ) {
+	if ( check ( cli::Lexer::TokenType::IDENTIFIER ) ) {
+		return matchIdentifier ( );
+	} else {
+		return "";
+	}
+}
+
 std::unique_ptr < Command > Parser::parse ( ) {
 	if ( check_nonreserved_kw ( "execute" ) ) {
 		match_nonreserved_kw ( "execute" );
@@ -163,6 +172,11 @@ std::unique_ptr < Command > Parser::parse ( ) {
 		match_nonreserved_kw ( "quit" );
 		match ( cli::Lexer::TokenType::END );
 		return std::make_unique < QuitCommand > ( );
+	} else if ( check_nonreserved_kw ( "help" ) ) {
+		match_nonreserved_kw ( "help" );
+		std::string command = help_parameter ( );
+		match ( cli::Lexer::TokenType::END );
+		return std::make_unique < HelpCommand > ( std::move ( command ) );
 	} else {
 		throw exception::CommonException ( "Mismatched set while expanding param rule." );
 	}
diff --git a/alib2cli/src/parser/Parser.h b/alib2cli/src/parser/Parser.h
index 96501c3ae9..61ba5be85b 100644
--- a/alib2cli/src/parser/Parser.h
+++ b/alib2cli/src/parser/Parser.h
@@ -84,6 +84,8 @@ public:
 
 	void result ( std::shared_ptr < StatementList > & list );
 
+	std::string help_parameter ( );
+
 	std::unique_ptr < Command > parse ( );
 
 };
-- 
GitLab