Skip to content

Commit

Permalink
added help command
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaja-Suganda committed Dec 17, 2023
1 parent 3cb8d6d commit 5629cde
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(argparser)
set(ARGPARSER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/argparser")
set(TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/test")

set(TEST_ENABLE ON)
set(TEST_ENABLE OFF)

if(TEST_ENABLE)
# Add test-related configurations or dependencies here
Expand Down
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ To use ArgParser in your C++ program, include the `argparser.h` header file and
#include <argparser.h>
int main(int argc, char* argv[]) {
ArgParser parser;
ArgParser parser(
"Argument Parser v1.1", // Project name
"This project can be used to parse command line arguments" // Description
);
// Add arguments
parser.addArgument("boolArg", "-b", "--bool", "Bool argument", false);
Expand All @@ -60,6 +63,53 @@ int main(int argc, char* argv[]) {
}
```

## Help Command
To display help information, use the following commands:

```cmd
./<exe-name>.exe -h
```
or
```cmd
./<exe-name>.exe --help
```

Upon execution, it will output:
```
Argument Parser v1.1

DESCRIPTION:
This project can be used to parse command line arguments
USAGE:
-b, --bool : This is a flag argument
-d, --double : This is a double argument
-i, --int : This is a int argument
-s, --string : This is a string argument
```
Replace `<exe-name>` with the actual name of your executable. This section provides a quick guide on how users can access `help` information along with an example output showcasing the available command-line arguments.
Otherthan that user can add own help command with built-in help command, then the user specified function will execute before `help` command.
```cpp
// Example user-specified callback
void userSpecifiedCallback() {
printf("Executing user-specified callback before the built-in help command.");
}
int main(int argc, char* argv[]) {
ArgParser parser;
// Set user-specified callback for help
parser.setHelpCallback(userSpecifiedCallback);
// Parse command-line arguments
parser.parse(argc, argv);
// ... rest of the program
return 0;
}
```

## Examples

Here are some examples demonstrating the usage of ArgParser:
Expand Down
9 changes: 9 additions & 0 deletions argparser/include/argparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ using namespace std;

class ArgParser {
public:
using Callback = std::function<void()>;

ArgParser();
ArgParser(const string& _name, const string& _description);
~ArgParser();

template <typename T>
Expand All @@ -33,13 +36,19 @@ class ArgParser {
}

bool argExists(const string &name) const;
void setHelpCallback(const Callback& callback);

private:

bool precheck(const string& name, const string& shortcmd, const string& longcmd);
void postcheck();
void help();

Callback help_callback;
map<string, Argument*> args;

string name;
string description;
};

#endif // !_ARGPARSER_H_
1 change: 1 addition & 0 deletions argparser/include/argument.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Argument{
string getName() const;
string getShortCmd() const;
string getLongCmd() const;
string getHelp() const;
void setArgPosition(int position);

private:
Expand Down
1 change: 0 additions & 1 deletion argparser/src/argdouble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ ArgStatus DoubleArgument::loadValue(const string& arg, int pos){

try{
value = stod(arg);
printf("Float Value: %f\n", value);
return PARSER_OK;
} catch (const std::invalid_argument& e) {
fprintf(stderr, "Error: %s is not a valid float.\n", arg.c_str());
Expand Down
1 change: 0 additions & 1 deletion argparser/src/argint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ ArgStatus IntArgument::loadValue(const string& arg, int pos){

try{
value = stoi(arg);
printf("Int Value: %d\n", value);
return PARSER_OK;
} catch (const std::invalid_argument& e) {
fprintf(stderr, "Error: %s is not a valid integer.\n", arg.c_str());
Expand Down
48 changes: 47 additions & 1 deletion argparser/src/argparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <argint.h>
#include <argdouble.h>
#include <argstring.h>
#include <string.h>
// #define VALIDATE_STRING(x) \
// do { \
// if (x.empty()) { \
Expand All @@ -14,8 +15,20 @@
#define VALIDATE_STRING(x)
#define LONGCMD_STRING "--"
#define SHORTCMD_STRING '-'
#define LONG_HELPCMD "--help"
#define SHORT_HELPCMD "-h"

ArgParser::ArgParser() {
#define DEFAULT_PARSER_NAME "Default Project Name"
#define DEFAULT_PARSER_DESCRIPTION "Default Project Description"


ArgParser::ArgParser()
: name(DEFAULT_PARSER_NAME), description(DEFAULT_PARSER_DESCRIPTION){

}

ArgParser::ArgParser(const string& _name, const string& _description)
: name(_name), description(_description){

}

Expand All @@ -36,6 +49,10 @@ bool ArgParser::precheck(
return false;
}

if(longcmd == LONG_HELPCMD || shortcmd == SHORT_HELPCMD){
return false;
}

for (const auto& argument : args) {
if( argument.second->getName() == name ||
argument.second->getLongCmd() == longcmd ||
Expand All @@ -51,6 +68,30 @@ void ArgParser::postcheck(){

}

void ArgParser::help(){

if (help_callback) {
help_callback();
}

printf("%s\n\n", name.c_str());
printf("DESCRIPTION:\n\t%s\n", description.c_str());

printf("USAGE:\n");
for (const auto& argument : args) {
printf("\t%s, %s \t: %s\n",
argument.second->getShortCmd().c_str(),
argument.second->getLongCmd().c_str(),
argument.second->getHelp().c_str());
}

printf("\n");
}

void ArgParser::setHelpCallback(const Callback& callback){
help_callback = callback;
}

template <>
ArgStatus ArgParser::addArgument<bool>(
const string& name,
Expand Down Expand Up @@ -164,6 +205,11 @@ ArgStatus ArgParser::parse(int argc, char* argv[]){
Argument* temp = nullptr;
ArgStatus ret = PARSER_ERROR;

if(strcmp(argv[1], LONG_HELPCMD) == PARSER_OK || strcmp(argv[1], SHORT_HELPCMD) == PARSER_OK){
help();
return PARSER_OK;
}

for (int i = 0; i < argc; ++i) {
temp = find(argv[i]);
if(temp != nullptr && !temp->status()) {
Expand Down
1 change: 0 additions & 1 deletion argparser/src/argstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ ArgStatus StringArgument::loadValue(const string& arg, int pos){

if(!arg.empty()){
value = arg;
printf("String Value: %s\n", value.c_str());
}
else{
return PARSER_INVALID_ARGUMENT;
Expand Down
4 changes: 4 additions & 0 deletions argparser/src/argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ string Argument::getLongCmd() const {
return longcmd;
}

string Argument::getHelp() const {
return help;
}

void Argument::setArgPosition(int position){
argpos = position;
}
Expand Down
36 changes: 24 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,40 @@ using namespace std;


int main(int argc, char* argv[]){
ArgParser parser;
ArgParser parser(
"Argument Parser v1.1", // Project name
"This project can be used to parse command line arguments" // Description
);

parser.addArgument("debug", "-d", "--debug", "This is a flag argument", false);
// char* sargv[] = { (char*)"program_name",
// (char*)"--int", (char*)"123", // Int Argument
// (char*)"--string", (char*)"Hello World", // String Argument
// (char*)"--bool", // Bool Argument
// (char*)"--double", (char*)"65.343"}; // Double Argument
char* sargv[] = { (char*)"program_name", (char*)"-h"};
int sargc = sizeof(sargv) / sizeof(sargv[0]);


parser.addArgument("bool", "-b", "--bool", "This is a flag argument", false);
parser.addArgument("int", "-i", "--int", "This is a int argument", 10);
parser.addArgument("double", "-d", "--double", "This is a double argument", 0.0);
parser.addArgument("string", "-s", "--string", "This is a string argument", "null");
parser.parse(argc, argv);
parser.print();
//parser.print();

auto intValue = parser.get<int>("int");
auto doubleValue = parser.get<double>("double");
auto stringValue = parser.get<string>("string");

printf("Int Value : %d\n", intValue);
printf("Double Value : %f\n", doubleValue);
printf("String Value : %s\n", stringValue.c_str());

if(parser.argExists("debug")){
printf("debug is there\n");
}
else{
printf("debug is not there\n");
}
// printf("Int Value : %d\n", intValue);
// printf("Double Value : %f\n", doubleValue);
// printf("String Value : %s\n", stringValue.c_str());

// if(parser.argExists("debug")){
// printf("debug is there\n");
// }
// else{
// printf("debug is not there\n");
// }
}

0 comments on commit 5629cde

Please sign in to comment.