Skip to content

Commit

Permalink
basic refactoring for cli
Browse files Browse the repository at this point in the history
  • Loading branch information
riasc committed Nov 13, 2024
1 parent 812f969 commit dd22b64
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 2 deletions.
17 changes: 17 additions & 0 deletions cli/include/Index.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef GENOGROVE_INDEX_HPP
#define GENOGROVE_INDEX_HPP

// Class
#include "genogrove/all.hpp"
#include "Subcall.hpp"

// CXXopts
#include "cxxopts.hpp"

class Index : public Subcall {
public:
cxxopts::ParseResult parseArgs(int argc, char** argv) override;
void execute(const cxxopts::ParseResult& args) override;
};

#endif //GENOGROVE_INDEX_HPP
19 changes: 19 additions & 0 deletions cli/include/Overlap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef GENOGROVE_OVERLAP_HPP
#define GENOGROVE_OVERLAP_HPP

// Standard
#include <iostream>

// Class
#include "Subcall.hpp"

// CXXopts
#include "cxxopts.hpp"

class Overlap : public Subcall {
public:
cxxopts::ParseResult parseArgs(int argc, char** argv) override;
void execute(const cxxopts::ParseResult& args) override;
};

#endif //GENOGROVE_OVERLAP_HPP
8 changes: 8 additions & 0 deletions cli/include/Subcall.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// Created by Richard Albin Schaefer on 11/12/24.
//

#ifndef GENOGROVE_SUBCALL_HPP
#define GENOGROVE_SUBCALL_HPP

#endif //GENOGROVE_SUBCALL_HPP
27 changes: 27 additions & 0 deletions cli/src/Index.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "Index.hpp"

cxxopts::ParseResult Index::parseArgs(int argc, char** argv) {
cxxopts::Options options("index", "Index an Interval File");
options.add_options()
("inputfile", "The input file to be indexed",
cxxopts::value<std::string>())
("-o, outputfile", "Write the index to the specified file",
cxxopts::value<std::string>())
("k, order", "The order of the tree (default: 3)",
cxxopts::value<int>()->default_value("3"))
;
options.parse_positional({"inputfile"});
return options.parse(argc, argv);
}

void Index::execute(const cxxopts::ParseResult& args) {
if(args.count("inputfile")) {
std::string inputfile = args["inputfile"].as<std::string>();
std::cout << "Indexing file: " << inputfile << std::endl;
}

if(args.count("outputfile")) {
std::string outputfile = args["outputfile"].as<std::string>();
std::cout << "Writing index to file: " << outputfile << std::endl;
}
}
25 changes: 25 additions & 0 deletions cli/src/Overlap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "Overlap.hpp"

cxxopts::ParseResult Overlap::parseArgs(int argc, char** argv) {
cxxopts::Options options("overlap", "Search for interval overlaps in the index");
options.add_options()
("input", "The search string or file to search for",
cxxopts::value<std::string>())
("o, output", "The output file to store the overlaps",
cxxopts::value<std::string>())
;
options.parse_positional({"input"});
return options.parse(argc, argv);
}

void Overlap::execute(const cxxopts::ParseResult& args) {
if(args.count("input")) {
std::string input = args["input"].as<std::string>();
std::cout << "Searching for overlaps in file: " << input << std::endl;
}

if(args.count("output")) {
std::string output = args["output"].as<std::string>();
std::cout << "Writing overlaps to file: " << output << std::endl;
}
}
47 changes: 45 additions & 2 deletions cli/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
// Standard
#include <iostream>
#include <cxxopts.hpp>
#include "cxxopts.hpp"

// Class
#include "genogrove/all.hpp"
#include "Subcall.hpp"
#include "Index.hpp"
#include "Overlap.hpp"

std::unique_ptr<Subcall> createSubcall(const std::string& subcall) {
if(subcall == "index") {
return std::make_unique<Index>();
} else if(subcall == "overlap") {
return std::make_unique<Overlap>();
} else {
return nullptr;
}
}

void printGeneralHelp(cxxopts::Options& options) {
std::cout << options.help() << "\n";
std::cout << "Available subcommands: \n";
std::cout << "\tindex:\tIndex an Interval File\n";
std::cout << "\toverlap:\tSearch for interval overlaps in the index\n";
std::cout << "For more details on a subcommand, use the --help option with the subcommand.\n";
}

int main(int argc, char** argv) {
cxxopts::Options options("genogrove", "A C++ library for the Interval B+ Tree (IBPTree) data structure");
Expand All @@ -16,8 +37,30 @@ int main(int argc, char** argv) {

// parse the command line arguments
auto args = options.parse(argc, argv);
if(args.count("help")) {

// check if the help option was selected (on the main level)
if(args.count("help") || !args.count("subcall")) {
std::cout << options.help() << std::endl;
return 0;
}

if(!args.count("subcall")) {
std::cerr << "Error: No subcommand has been specified.\n";
std::cerr << options.help() << std::endl;
return 1;
}

std::string subcall = args["subcall"].as<std::string>();
std::unique_ptr<Subcall> command = createSubcall(subcall);

if(!command) {
std::cerr << "Error: Unknown subcommand '" << subcall << "'.\n";
std::cerr << options.help() << std::endl;
return 1;
}

// parse additional options for the subcommand
auto subcallArgs = command->parseArgs(argc, argv);

}

0 comments on commit dd22b64

Please sign in to comment.