diff --git a/alib2cli/src/command/SetCommand.h b/alib2cli/src/command/SetCommand.h new file mode 100644 index 0000000000000000000000000000000000000000..12e01e25ce6196a9412e12d12c43475b27806847 --- /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 8980f01dbe17a0c75312fcb272f07c0625514e16..556c62979a0117b145b0e32151762358e386b943 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 1d51dc08a6f461ea3f3a0bd9f90c58819795846d..dc3fd4ca33a7348912a163b7c839020f57f0c75f 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 1d9749723bd1244300a2ff91e088c63c90f12d5b..1f411754eb43da4dac85cfe3643b239c50488cb2 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 );