Files
cargoxx/src/cli/run.cpp

126 lines
3.7 KiB
C++

module;
#include <CLI11.hpp>
module cargoxx.cli;
import std;
import cargoxx.util;
namespace cargoxx::cli {
auto run(int argc, char** argv) -> int {
CLI::App app{"cargoxx — a Cargo-style project tool for modern C++"};
app.require_subcommand(1);
auto* new_cmd = app.add_subcommand("new", "Create a new cargoxx project");
std::string new_name;
bool new_lib = false;
new_cmd->add_option("name", new_name, "Project name")->required();
new_cmd->add_flag("--lib", new_lib, "Create a library project");
auto* build_cmd = app.add_subcommand(
"build", "Generate flake.nix and build/CMakeLists.txt; build with nix+cmake");
bool build_no_build = false;
bool build_release = false;
std::string build_target;
build_cmd->add_flag("--no-build", build_no_build,
"Generate files only; do not invoke nix/cmake");
build_cmd->add_flag("--release", build_release, "Build the release profile");
build_cmd->add_option("--target", build_target,
"Build a specific target (passed to cmake --build)");
auto* run_cmd = app.add_subcommand("run", "Build and run a binary target");
bool run_release = false;
std::string run_bin;
std::vector<std::string> run_args;
run_cmd->add_flag("--release", run_release, "Run the release profile");
run_cmd->add_option("--bin", run_bin, "Binary target to run");
run_cmd->add_option("args", run_args, "Arguments passed to the binary")
->expected(0, -1);
auto* test_cmd = app.add_subcommand("test", "Build and run all test targets via ctest");
bool test_release = false;
test_cmd->add_flag("--release", test_release, "Test the release profile");
auto* clean_cmd = app.add_subcommand("clean", "Remove the build/ directory");
try {
app.parse(argc, argv);
} catch (const CLI::ParseError& e) {
return app.exit(e);
}
std::error_code ec;
auto cwd = std::filesystem::current_path(ec);
if (ec) {
std::cerr << std::format("error: cannot determine current working directory: {}\n",
ec.message());
return 1;
}
if (*new_cmd) {
auto r = cmd_new(new_name, new_lib, cwd);
if (!r) {
std::cerr << util::format(r.error());
return 1;
}
std::cout << std::format(" Created `{}` project\n", new_name);
return 0;
}
if (*build_cmd) {
std::optional<std::string> target;
if (!build_target.empty()) {
target = build_target;
}
auto r = cmd_build(cwd, build_no_build, build_release, target);
if (!r) {
std::cerr << util::format(r.error());
return 1;
}
if (build_no_build) {
std::cout << " Generated flake.nix, build/CMakeLists.txt, Cargoxx.lock\n";
} else {
std::cout << " Built\n";
}
return 0;
}
if (*run_cmd) {
std::optional<std::string> bin;
if (!run_bin.empty()) {
bin = run_bin;
}
auto r = cmd_run(cwd, run_release, bin, run_args);
if (!r) {
std::cerr << util::format(r.error());
return 1;
}
return *r;
}
if (*test_cmd) {
auto r = cmd_test(cwd, test_release);
if (!r) {
std::cerr << util::format(r.error());
return 1;
}
return *r;
}
if (*clean_cmd) {
auto r = cmd_clean(cwd);
if (!r) {
std::cerr << util::format(r.error());
return 1;
}
std::cout << " Cleaned build/\n";
return 0;
}
return 0;
}
} // namespace cargoxx::cli