diff --git a/drogon_ctl/create.cc b/drogon_ctl/create.cc index f50b27e7..b41ff013 100644 --- a/drogon_ctl/create.cc +++ b/drogon_ctl/create.cc @@ -23,8 +23,8 @@ std::string create::detail() return "Use create command to create some source files of drogon webapp\n\n" "Usage:drogon_ctl create " "[-options] \n\n" - "drogon_ctl create view [-o ]//create " - "HttpView source files from csp file\n\n" + "drogon_ctl create view [-o ] [-n " + "]//create HttpView source files from csp files\n\n" "drogon_ctl create controller [-s] <[namespace::]class_name> //" "create HttpSimpleController source files\n\n" "drogon_ctl create controller -h <[namespace::]class_name> //" diff --git a/drogon_ctl/create_view.cc b/drogon_ctl/create_view.cc index b338ef30..7a0e755b 100644 --- a/drogon_ctl/create_view.cc +++ b/drogon_ctl/create_view.cc @@ -14,6 +14,7 @@ #include "create_view.h" #include "cmd.h" +#include #include #include #include @@ -247,9 +248,9 @@ static void parseLine(std::ofstream &oSrcFile, void create_view::handleCommand(std::vector ¶meters) { - for (auto iter = parameters.begin(); iter != parameters.end(); iter++) + for (auto iter = parameters.begin(); iter != parameters.end();) { - auto file = *iter; + auto &file = *iter; if (file == "-o" || file == "--output") { iter = parameters.erase(iter); @@ -258,13 +259,24 @@ void create_view::handleCommand(std::vector ¶meters) outputPath_ = *iter; iter = parameters.erase(iter); } - break; + continue; + } + else if (file == "-n" || file == "--namespace") + { + iter = parameters.erase(iter); + if (iter != parameters.end()) + { + namespaces_ = utils::splitString(*iter, "::"); + iter = parameters.erase(iter); + } + continue; } else if (file[0] == '-') { std::cout << ARGS_ERROR_STR << std::endl; return; } + ++iter; } createViewFiles(parameters); } @@ -324,11 +336,21 @@ void create_view::newViewHeaderFile(std::ofstream &file, "it!\n"; file << "#include \n"; file << "using namespace drogon;\n"; + for (auto &np : namespaces_) + { + file << "namespace " << np << "\n"; + file << "{\n"; + } file << "class " << className << ":public DrTemplate<" << className << ">\n"; file << "{\npublic:\n\t" << className << "(){};\n\tvirtual ~" << className << "(){};\n\t" - "virtual std::string genText(const DrTemplateData &) override;\n};"; + "virtual std::string genText(const DrTemplateData &) " + "override;\n};\n"; + for (auto i = 0; i < namespaces_.size(); ++i) + { + file << "}\n"; + } } void create_view::newViewSourceFile(std::ofstream &file, @@ -350,6 +372,22 @@ void create_view::newViewSourceFile(std::ofstream &file, file << "#include \n"; file << "#include \n"; file << "#include \n"; + if (!namespaces_.empty()) + { + file << "using namespace "; + for (int i = 0; i < namespaces_.size(); ++i) + { + if (i != namespaces_.size() - 1) + { + file << namespaces_[i] << "::"; + } + else + { + file << namespaces_[i] << ";"; + } + } + file << "\n"; + } // Find layout tag std::string layoutName; std::regex layoutReg("<%layout[ \\t]+(((?!%\\}).)*[^ \\t])[ \\t]*%>"); diff --git a/drogon_ctl/create_view.h b/drogon_ctl/create_view.h index 4fc770e9..2e5fb5b1 100644 --- a/drogon_ctl/create_view.h +++ b/drogon_ctl/create_view.h @@ -30,6 +30,7 @@ class create_view : public DrObject, public CommandHandler protected: std::string outputPath_{"."}; + std::vector namespaces_; void createViewFiles(std::vector &cspFileNames); int createViewFile(const std::string &script_filename); void newViewHeaderFile(std::ofstream &file, const std::string &className);