You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cmkr/src/args.cpp

84 lines
2.5 KiB

4 years ago
#include "args.h"
4 years ago
#include "build.h"
4 years ago
#include "gen.h"
#include "help.h"
4 years ago
#include <exception>
4 years ago
#include <filesystem>
#include <iostream>
#include <stdexcept>
4 years ago
#include <string>
#include <vector>
namespace fs = std::filesystem;
namespace cmkr::args {
4 years ago
const char *handle_args(int argc, char **argv) {
std::vector<std::string> args;
for (int i = 0; i < argc; ++i)
args.push_back(argv[i]);
if (args.size() < 2)
4 years ago
return "Please provide command line arguments!";
std::string main_arg = args[1];
if (main_arg == "gen") {
bool cont = false;
if (args.size() > 2 && args[2] == "-y")
cont = true;
auto current_path = fs::current_path();
if (fs::exists(current_path / "CMakeLists.txt") && cont == false) {
std::cout << "A CMakeLists.txt file already exists in the current directory.\nWould you "
"like to overwrite it?[y/n]"
<< std::endl;
std::string resp;
std::cin >> resp;
if (resp != "y")
return "CMake generation aborted!";
}
auto ret = cmkr::gen::generate_cmake(current_path.string().c_str());
4 years ago
if (ret)
return "CMake generation error!";
return "CMake generation successful!";
} else if (main_arg == "help") {
4 years ago
return cmkr::help::message();
} else if (main_arg == "version") {
4 years ago
return cmkr::help::version();
} else if (main_arg == "init") {
4 years ago
std::string typ = "exe";
if (args.size() > 2)
typ = args[2];
auto ret = cmkr::gen::generate_project(typ.c_str());
4 years ago
if (ret)
return "Initialization failure!";
return "Directory initialized!";
} else if (main_arg == "build") {
4 years ago
auto ret = build::run(argc, argv);
4 years ago
if (ret)
4 years ago
return "CMake build error!";
return "CMake run completed!";
4 years ago
} else if (main_arg == "install") {
auto ret = build::install();
if (ret)
return "CMake install error!";
return "CMake install completed!";
} else if (main_arg == "clean") {
auto ret = build::clean();
if (ret)
return "CMake clean error!";
4 years ago
return "Cleaned build directory!";
} else {
return "Unknown argument!";
}
}
4 years ago
} // namespace cmkr::args
const char *cmkr_args_handle_args(int argc, char **argv) {
4 years ago
try {
return cmkr::args::handle_args(argc, argv);
} catch (const std::exception &e) {
return e.what();
} catch (...) {
4 years ago
return "Unknown error!";
4 years ago
}
4 years ago
}