Skip to content

Commit

Permalink
Better error propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
ken-matsui committed Dec 16, 2023
1 parent c372d56 commit e217d2e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/Cmd/Build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ static void parseMMOutput(
Logger::debug("");
}

void build(Vec<String> args) {
int build(Vec<String> args) {
if (!std::filesystem::exists("src")) {
throw std::runtime_error("src directory not found");
}
Expand All @@ -136,7 +136,7 @@ void build(Vec<String> args) {
"invalid option: `", args[0], "`", "\n\n",
" run `poac help build` for a list of options"
);
return;
return EXIT_FAILURE;
}
}

Expand Down Expand Up @@ -188,6 +188,7 @@ void build(Vec<String> args) {
ofs.close();

std::system("cd poac-out && make");
return EXIT_SUCCESS;
}

void build_help() {
Expand Down
2 changes: 1 addition & 1 deletion src/Cmd/Build.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

#include "../Rustify.hpp"

void build(Vec<String> args);
int build(Vec<String> args);
void build_help();
21 changes: 10 additions & 11 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#define POAC_VERSION "0.6.0"

void help(Vec<String> args) {
int help(Vec<String> args) {
if (args.empty()) {
std::cout << "poac " << POAC_VERSION << '\n';
std::cout << "A package manager and build system for C++" << '\n';
Expand All @@ -27,7 +27,7 @@ void help(Vec<String> args) {
std::cout
<< " help\tPrints this message or the help of the given subcommand(s)"
<< '\n';
return;
return EXIT_SUCCESS;
}

HashMap<StringRef, Fn<void()>> helps;
Expand All @@ -39,10 +39,11 @@ void help(Vec<String> args) {
"no such subcommand: `", subcommand, "`", "\n\n",
" run `poac help` for a list of subcommands"
);
return;
return EXIT_FAILURE;
}

helps[subcommand]();
return EXIT_SUCCESS;
}

int main(int argc, char* argv[]) {
Expand All @@ -52,7 +53,7 @@ int main(int argc, char* argv[]) {
String arg = argv[i];
if (arg == "-v" || arg == "--version") {
std::cout << "poac " << POAC_VERSION << '\n';
return 0;
return EXIT_SUCCESS;
}
if (arg == "--verbose") {
Logger::setLevel(LogLevel::debug);
Expand All @@ -68,10 +69,10 @@ int main(int argc, char* argv[]) {
"no subcommand provided", "\n\n",
" run `poac help` for a list of commands"
);
return 1;
return EXIT_FAILURE;
}

HashMap<StringRef, Fn<void(Vec<String>)>> cmds;
HashMap<StringRef, Fn<int(Vec<String>)>> cmds;
cmds["help"] = help;
cmds["build"] = build;

Expand All @@ -81,15 +82,13 @@ int main(int argc, char* argv[]) {
"no such command: `", subcommand, "`", "\n\n",
" run `poac help` for a list of commands"
);
return 1;
return EXIT_FAILURE;
}

try {
cmds[subcommand](Vec<String>(args.begin() + 1, args.end()));
return cmds[subcommand](Vec<String>(args.begin() + 1, args.end()));
} catch (const std::exception& e) {
Logger::error(e.what());
return 1;
return EXIT_FAILURE;
}

return 0;
}

0 comments on commit e217d2e

Please sign in to comment.