Skip to content

Commit

Permalink
added basic cli setup
Browse files Browse the repository at this point in the history
  • Loading branch information
iamAbhishekkumar committed Oct 6, 2024
1 parent a990b40 commit eb75faf
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
48 changes: 48 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"files.associations": {
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"concepts": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"random": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"typeinfo": "cpp"
}
}
24 changes: 24 additions & 0 deletions include/argParser.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef ARGPARSER_H
#define ARGPARSER_H
#include <algorithm>
#include <iostream>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

class ArgParser {
private:
std::vector<std::string> tokens;

public:
ArgParser(int &argc, char **argv);

// Returns the command line option value if it exists
std::optional<std::string_view> getCmdOption(std::string_view option) const;

// Checks if a command line option exists
bool cmdOptionExists(std::string_view option) const;
};

#endif
22 changes: 22 additions & 0 deletions src/argParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <../include/argParser.hpp>

ArgParser::ArgParser(int &argc, char **argv) {
for (int i = 0; i < argc; i++) {
tokens.push_back(argv[i]);
}
}

// Returns the command line option value if it exists
std::optional<std::string_view> ArgParser::getCmdOption(
std::string_view option) const {
auto itr = std::find(tokens.begin(), tokens.end(), option);
if (itr != tokens.end() && ++itr != tokens.end()) {
return *itr;
}
return std::nullopt; // Indicate no option found
}

// Checks if a command line option exists
bool ArgParser::cmdOptionExists(std::string_view option) const {
return std::find(tokens.begin(), tokens.end(), option) != tokens.end();
}
36 changes: 34 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
#include <../include/argParser.hpp>
#include <iostream>
using namespace std;
int main() {}

int main(int argc, char* argv[]) {
if (argc == 1) {
std::cout << "No arguments provided.!!!" << std::endl;
return -1;
}

ArgParser argParser(argc, argv);

if (argc == 2 && std::string_view(argv[1]) == "init") {
std::cout << "Initialize empty gitlite repository in <current_dir_path>"
<< std::endl;
// Add logic for initialization
return 0; // Exit after handling "init"
}

// Check for "-h" flag
if (argParser.cmdOptionExists("-h")) {
std::cout << "Help requested" << std::endl;
// Show help message
return 0; // Exit after showing help
}

// Get filename after "-f" option
auto filename = argParser.getCmdOption("-f");
if (filename) {
std::cout << "Filename: " << *filename << std::endl;
// Do something with the filename
return 0;
}

std::cout << "Unknown command entered!!!" << std::endl;
}

0 comments on commit eb75faf

Please sign in to comment.