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

Fix a bug in parsing occa cli options #770

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
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
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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to expose this function so I don't have to redundantly implement it again in tests. Hope that is okay.

}
}

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");
}
Loading