Skip to content

Commit

Permalink
Fix a bug in parsing occa cli options (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
thilinarmtb authored Nov 20, 2024
1 parent 1bb7c22 commit 4ac4209
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/occa/internal/bin/occa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace occa {
namespace bin {
cli::command buildOccaCommand();
bool runTranslate(const json &args);
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/occa/internal/utils/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ namespace occa {
}

occa::json parser::parseArgs(const strVector &args_,
const std::vector<command> &commands,
const bool supressErrors) {
strVector args = splitShortOptionArgs(args_);
const int argc = (int) args.size();
Expand Down Expand Up @@ -445,7 +446,12 @@ namespace occa {

// No option
if (!opt) {
checkOptions = (arg == "==");
for (auto cmd : commands) {
if (arg == cmd.name) {
checkOptions = 0;
break;
}
}
jArguments += arg;
continue;
}
Expand Down Expand Up @@ -779,7 +785,7 @@ namespace occa {

const bool hasCommands = commands.size();

json parsedArgs = parseArgs(shellArgs, supressErrors);
json parsedArgs = parseArgs(shellArgs, commands, supressErrors);
lastCommandArgs = parsedArgs;

json &jArguments = parsedArgs["arguments"];
Expand Down
3 changes: 3 additions & 0 deletions src/occa/internal/utils/cli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ namespace occa {
};
//==================================

class command;

//---[ Parser ]---------------------
class parser : public printable {
public:
Expand Down Expand Up @@ -157,6 +159,7 @@ namespace occa {

occa::json parseArgs(const int argc, const char **argv);
occa::json parseArgs(const strVector &args_,
const std::vector<command> &commands = {},
const bool supressErrors = false);

bool hasCustomHelpOption();
Expand Down
31 changes: 31 additions & 0 deletions tests/src/internal/utils/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <occa/defines.hpp>
#include <occa/internal/utils/cli.hpp>
#include <occa/internal/utils/testing.hpp>
#include <occa/internal/bin/occa.hpp>
#include <occa/internal/modes.hpp>

void testPretty();
void testOption();
Expand Down Expand Up @@ -231,4 +233,33 @@ void testParser() {
}

void testCommand() {
occa::cli::command translateCommand;
translateCommand
.withName("translate")
.withCallback(occa::bin::runTranslate)
.withDescription("Translate kernels")
.addOption(occa::cli::option('m', "mode",
"Output mode (Default: Serial)")
.withArg()
.expandsFunction([&](const occa::json &args) {
occa::strVector suggestions;
for (auto &it : occa::getModeMap()) {
suggestions.push_back(it.second->name());
}
return suggestions;
}))
.addOption(occa::cli::option('v', "verbose",
"Verbose output"))
.addArgument(occa::cli::argument("FILE",
"An .okl file")
.isRequired()
.expandsFiles());

const auto &options = translateCommand.options;
ASSERT_EQ(options.size(), (unsigned long)2);

const int argc = 4;
const char *argv[] = {"translate", "addVectors.okl", "-m", "cuda"};
const auto json = translateCommand.parseArgs(argc, (const char **)&argv);
ASSERT_EQ(json["options"]["mode"], "cuda");
}

0 comments on commit 4ac4209

Please sign in to comment.