Skip to content

Commit

Permalink
some fixing up.
Browse files Browse the repository at this point in the history
  • Loading branch information
mabruzzo committed Sep 6, 2024
1 parent befe0e8 commit 79690b9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/grcli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ FetchContent_MakeAvailable(benchmark)
add_executable(Grackle_Grcli
ChemistryData.h
args/CliParser.h
args/EscapedArgsConfig.h
FieldData.h FieldData.C
cmd_bench.h cmd_bench.C
cmd_show.h cmd_show.C
Expand Down
91 changes: 91 additions & 0 deletions src/grcli/args/EscapedArgsConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#ifndef ESCAPED_ARGS_CONFIG_H
#define ESCAPED_ARGS_CONFIG_H

#include <cstdlib>
#include <cstdio>
#include <string_view>
#include <vector>

#include "../args/CliParser.h"
#include "../utils.h"

namespace args {


/// This struct is used to help with parsing a batch of escaped arguments
struct EscapedArgsSpec {
char short_opt_character;
std::string_view group_arg_start;
std::string_view group_arg_sentinel;

template<typename Fn>
bool try_parse(const char* leading_arg, CliParser& parser, Fn fn) {

char short_opt[3] = {'-', short_opt_character, '\0'};

if (starts_with(leading_arg, short_opt)) {
const char * escaped = leading_arg + 2;
if (leading_arg[2] == '\0') {
escaped = (parser.has_next()) ? parser.next() : nullptr;
}

if (escaped == nullptr) {
std::fprintf(stderr, "the %s arg is missing an option\n");
std::exit(1);
}
fn(escaped);
return true;

} else if (leading_arg == this->group_arg_start) {
int num_encountered = 0;
const char * elem = parser.has_next() ? parser.next() : nullptr;
while ((elem != nullptr) && (elem != this->group_arg_sentinel)) {
fn(elem);
num_encountered++;
elem = parser.has_next() ? parser.next() : nullptr;
}

if (num_encountered == 0) {
std::fprintf(stderr,
"the \"%s\" flag doesn't start a group of parameters\n",
leading_arg);
std::exit(1);
}
return true;

}
return false;
}
};


/// class used to collect the arguments that are forwarded to a backend
class BackendConfig {

/// this will be forwarded to the backend
std::vector<char *> arg_vec_;

public:

/// this performs a copy of arg_vec_
///
/// @note
/// This is NOT a deep copy. Modifying the contents of the elements will
/// affect the value in various locations
std::vector<char *> args_copy() const { return arg_vec_; }

/// Try to parse the argument
bool try_parse(const char* leading_arg, CliParser& parser) {
EscapedArgsSpec arg_spec{'B', "--backend-start", "--backend-stop"};

auto fn = [this](const char* arg) {
this->arg_vec_.push_back((char*)arg); // this cast is bad...
};
return arg_spec.try_parse(leading_arg, parser, fn);
}

};

} // namespace args

#endif /* ESCAPED_ARGS_CONFIG_H */
16 changes: 10 additions & 6 deletions src/grcli/cmd_bench.C
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@
std::optional<CliParamSpec> gr_param_spec;
scenario::CliGridSpec grid_spec;
std::optional<OperationSpec> op_spec;
args::EscapedArgsConfig backend_arg_spec(true,
"--backend-start",
"--backend-stop");
args::BackendConfig backend_arg_spec;

const char* ptr;
while ((ptr = parser.next()) != nullptr) {
Expand Down Expand Up @@ -88,10 +86,16 @@
auto my_test = [&driver](benchmark::State& st) { driver(st); };
benchmark::RegisterBenchmark("scenario", my_test);

const char * const * argv = backend_arg_spec.begin();
int argc = backend_arg_spec.end() - argv;
// construct argv and argc
// ToDo: there is probably a smarter way to do this without copying the
// vector (since we aren't copying the pointers this isn't terrible)
std::vector<char*> argv_vec = backend_arg_spec.args_copy();
int argc = int(argv_vec.size()) + 1;
argv_vec.reserve(argc + 2);
argv_vec.insert(argv_vec.begin(), parser.bin_name());
argv_vec.push_back(nullptr);

benchmark::Initialize(&argc, (char**)(argv));
benchmark::Initialize(&argc, (char**)(argv_vec.data()));
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();

Expand Down

0 comments on commit 79690b9

Please sign in to comment.