-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e512835
commit 677b1d6
Showing
12 changed files
with
185 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#include <iostream> | ||
#include "ArgumentParser.h" | ||
|
||
inline bool expectArg(int current, int argc, std::string error) | ||
{ | ||
if (!(current < argc)) | ||
{ | ||
std::cerr << "[ERROR] expected argumnet for: " << error << std::endl; | ||
exit(-1); | ||
} | ||
} | ||
|
||
inline void unrecognisedArg(std::string arg) | ||
{ | ||
std::cerr << "[ERROR] unrecognised argument: " << arg << std::endl; | ||
exit(-1); | ||
} | ||
|
||
inline void invalidArg(std::string arg, std::string value) | ||
{ | ||
std::cerr << "[ERROR] Invalid argument to '" << arg << "': " << value << std::endl; | ||
exit(-1); | ||
} | ||
|
||
void ArgumentParser::Parse(int argc, char** argv) | ||
{ | ||
for (int i = 1; i < argc; i++) | ||
{ | ||
std::string current_arg = std::string(argv[i]); | ||
if (this->parser_state == ARGUMENT_ATTR) | ||
{ | ||
this->argmap[this->processing_arg] = current_arg; | ||
this->parser_state = ARGUMENT_NORMAL; | ||
} | ||
else | ||
{ | ||
if (current_arg[0] != '-') | ||
{ | ||
/* If this is the first argument and doesnt begin with a dash, just store it */ | ||
if (this->parser_state == ARGUMENT_LIST_BEGIN) | ||
{ | ||
this->argmap["first_argument"] = current_arg; | ||
this->argmap[this->first_arg] = current_arg; | ||
} | ||
else | ||
{ | ||
unrecognisedArg(current_arg); | ||
} | ||
} | ||
else | ||
{ | ||
/* Remove the '-' */ | ||
current_arg.erase(0, 1); | ||
if (std::find(this->expected_args.begin(), this->expected_args.end(), current_arg) != this->expected_args.end()) | ||
{ | ||
this->parser_state = ARGUMENT_ATTR; | ||
this->processing_arg = current_arg; | ||
} | ||
else | ||
{ | ||
unrecognisedArg(current_arg); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void ArgumentParser::AddArgument(std::string arg, bool first_arg) | ||
{ | ||
this->expected_args.push_back(arg); | ||
if (first_arg) | ||
{ | ||
this->first_arg = arg; | ||
} | ||
} | ||
|
||
void ArgumentParser::GetArgument(std::string arg, std::string& result) | ||
{ | ||
if (this->argmap.find(arg) != this->argmap.end()) | ||
{ | ||
result = this->argmap[arg]; | ||
} | ||
} | ||
|
||
void ArgumentParser::GetArgument(std::string arg, int& result) | ||
{ | ||
if (this->argmap.find(arg) != this->argmap.end()) | ||
{ | ||
try | ||
{ | ||
result = std::stoi(this->argmap[arg]); | ||
} | ||
catch (...) | ||
{ | ||
invalidArg(arg, this->argmap[arg]); | ||
} | ||
} | ||
} | ||
|
||
void ArgumentParser::GetArgument(std::string arg, float& result) | ||
{ | ||
if (this->argmap.find(arg) != this->argmap.end()) | ||
{ | ||
try | ||
{ | ||
result = std::stof(this->argmap[arg]); | ||
} | ||
catch (...) | ||
{ | ||
invalidArg(arg, this->argmap[arg]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Nicely written class for parsing arguments. Heh, just kidding, I wrote this for some C++ practice. | ||
*/ | ||
|
||
#ifndef ARGUMENT_PARSER_H | ||
#define ARGUMENT_PARSER_H | ||
|
||
#include <string> | ||
|
||
#include "CRTermConfig.h" | ||
|
||
typedef enum | ||
{ | ||
ARGUMENT_LIST_BEGIN, | ||
ARGUMENT_NORMAL, | ||
ARGUMENT_ATTR | ||
} ARGUMENT_PARSER_STATE; | ||
|
||
class ArgumentParser | ||
{ | ||
public: | ||
ARGUMENT_PARSER_STATE parser_state = ARGUMENT_LIST_BEGIN; | ||
std::unordered_map<std::string, std::string> argmap; | ||
std::vector<std::string> expected_args; | ||
std::string first_arg; | ||
std::string processing_arg; | ||
ArgumentParser(void) { }; | ||
void Parse(int argc, char** argv); | ||
void AddArgument(std::string argument, bool first_arg = false); | ||
void GetArgument(std::string, std::string&); | ||
void GetArgument(std::string, float&); | ||
void GetArgument(std::string, int&); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#ifndef CRTERM_H | ||
#define CRTERM_H | ||
|
||
#define CRTERM_VERSION_STRING "CRTerm 0.1.0" | ||
#define CRTERM_VERSION_STRING "CRTerm 0.2.0" | ||
#define CRTERM_CREDIT_STRING "(C) Siddharth Gautam, 2023\nThis software comes with NO WARRANTY.\n" | ||
|
||
#endif |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters