Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better argument parsing for shell #112

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions src/shell/Shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ struct spectestseps : std::numpunct<char> {
std::string do_grouping() const { return "\3"; }
};

struct ArgParser {
std::string exportToRun;
std::vector<std::string> fileNames;
};

using namespace Walrus;

static void printI32(int32_t v)
Expand Down Expand Up @@ -870,6 +875,33 @@ static void runExports(Store* store, const std::string& filename, const std::vec
&data);
}

static void parseArguments(int argc, char* argv[], ArgParser& argParser)
{
const std::vector<std::string_view> args(argv + 1, argv + argc);

for (auto it = args.begin(); it != args.end(); ++it) {
if (*it == "--run-export") {
if (*it == args.back()) {
fprintf(stderr, "error: --run-export requires an argument\n");
exit(1);
}

std::advance(it, 1);
argParser.exportToRun = *it;
} else if (auto arg = std::string(*it); endsWith(arg, "wat") || endsWith(arg, "wast") || endsWith(arg, "wasm")) {
argParser.fileNames.emplace_back(*it);
} else {
fprintf(stderr, "error: unknown argument: %s\n", it->data());
exit(1);
}
}

if (argParser.fileNames.empty()) {
fprintf(stderr, "error: no input files\n");
exit(1);
}
}

int main(int argc, char* argv[])
{
#ifndef NDEBUG
Expand All @@ -888,22 +920,11 @@ int main(int argc, char* argv[])
Store* store = new Store(engine);

SpecTestFunctionTypes functionTypes;
std::string exportToRun;
ArgParser argParser;

for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-') {
if (strcmp(argv[i], "--run-export") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "error: --run-export requires an argument\n");
return 1;
}

exportToRun = argv[++i];
continue;
}
}
parseArguments(argc, argv, argParser);

std::string filePath = argv[i];
for (const auto& filePath : argParser.fileNames) {
FILE* fp = fopen(filePath.data(), "r");
if (fp) {
fseek(fp, 0, SEEK_END);
Expand All @@ -915,8 +936,8 @@ int main(int argc, char* argv[])
fclose(fp);

if (endsWith(filePath, "wasm")) {
if (!exportToRun.empty()) {
runExports(store, filePath, buf, exportToRun);
if (!argParser.exportToRun.empty()) {
runExports(store, filePath, buf, argParser.exportToRun);
} else {
auto trapResult = executeWASM(store, filePath, buf, functionTypes);
if (trapResult.exception) {
Expand All @@ -928,7 +949,7 @@ int main(int argc, char* argv[])
executeWAST(store, filePath, buf, functionTypes);
}
} else {
printf("Cannot open file %s\n", argv[i]);
printf("Cannot open file %s\n", filePath.data());
return -1;
}
}
Expand Down