Skip to content

Commit 5629cde

Browse files
added help command
1 parent 3cb8d6d commit 5629cde

10 files changed

Lines changed: 137 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ project(argparser)
55
set(ARGPARSER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/argparser")
66
set(TEST_DIR "${CMAKE_CURRENT_SOURCE_DIR}/test")
77

8-
set(TEST_ENABLE ON)
8+
set(TEST_ENABLE OFF)
99

1010
if(TEST_ENABLE)
1111
# Add test-related configurations or dependencies here

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ To use ArgParser in your C++ program, include the `argparser.h` header file and
3737
#include <argparser.h>
3838
3939
int main(int argc, char* argv[]) {
40-
ArgParser parser;
40+
ArgParser parser(
41+
"Argument Parser v1.1", // Project name
42+
"This project can be used to parse command line arguments" // Description
43+
);
4144
4245
// Add arguments
4346
parser.addArgument("boolArg", "-b", "--bool", "Bool argument", false);
@@ -60,6 +63,53 @@ int main(int argc, char* argv[]) {
6063
}
6164
```
6265

66+
## Help Command
67+
To display help information, use the following commands:
68+
69+
```cmd
70+
./<exe-name>.exe -h
71+
```
72+
or
73+
```cmd
74+
./<exe-name>.exe --help
75+
```
76+
77+
Upon execution, it will output:
78+
```
79+
Argument Parser v1.1
80+
81+
DESCRIPTION:
82+
This project can be used to parse command line arguments
83+
USAGE:
84+
-b, --bool : This is a flag argument
85+
-d, --double : This is a double argument
86+
-i, --int : This is a int argument
87+
-s, --string : This is a string argument
88+
```
89+
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.
90+
91+
Otherthan that user can add own help command with built-in help command, then the user specified function will execute before `help` command.
92+
93+
```cpp
94+
// Example user-specified callback
95+
void userSpecifiedCallback() {
96+
printf("Executing user-specified callback before the built-in help command.");
97+
}
98+
99+
int main(int argc, char* argv[]) {
100+
ArgParser parser;
101+
102+
// Set user-specified callback for help
103+
parser.setHelpCallback(userSpecifiedCallback);
104+
105+
// Parse command-line arguments
106+
parser.parse(argc, argv);
107+
108+
// ... rest of the program
109+
return 0;
110+
}
111+
```
112+
63113
## Examples
64114

65115
Here are some examples demonstrating the usage of ArgParser:

argparser/include/argparser.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ using namespace std;
1212

1313
class ArgParser {
1414
public:
15+
using Callback = std::function<void()>;
16+
1517
ArgParser();
18+
ArgParser(const string& _name, const string& _description);
1619
~ArgParser();
1720

1821
template <typename T>
@@ -33,13 +36,19 @@ class ArgParser {
3336
}
3437

3538
bool argExists(const string &name) const;
39+
void setHelpCallback(const Callback& callback);
3640

3741
private:
3842

3943
bool precheck(const string& name, const string& shortcmd, const string& longcmd);
4044
void postcheck();
45+
void help();
4146

47+
Callback help_callback;
4248
map<string, Argument*> args;
49+
50+
string name;
51+
string description;
4352
};
4453

4554
#endif // !_ARGPARSER_H_

argparser/include/argument.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Argument{
3737
string getName() const;
3838
string getShortCmd() const;
3939
string getLongCmd() const;
40+
string getHelp() const;
4041
void setArgPosition(int position);
4142

4243
private:

argparser/src/argdouble.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ ArgStatus DoubleArgument::loadValue(const string& arg, int pos){
1717

1818
try{
1919
value = stod(arg);
20-
printf("Float Value: %f\n", value);
2120
return PARSER_OK;
2221
} catch (const std::invalid_argument& e) {
2322
fprintf(stderr, "Error: %s is not a valid float.\n", arg.c_str());

argparser/src/argint.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ ArgStatus IntArgument::loadValue(const string& arg, int pos){
1717

1818
try{
1919
value = stoi(arg);
20-
printf("Int Value: %d\n", value);
2120
return PARSER_OK;
2221
} catch (const std::invalid_argument& e) {
2322
fprintf(stderr, "Error: %s is not a valid integer.\n", arg.c_str());

argparser/src/argparser.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <argint.h>
44
#include <argdouble.h>
55
#include <argstring.h>
6+
#include <string.h>
67
// #define VALIDATE_STRING(x) \
78
// do { \
89
// if (x.empty()) { \
@@ -14,8 +15,20 @@
1415
#define VALIDATE_STRING(x)
1516
#define LONGCMD_STRING "--"
1617
#define SHORTCMD_STRING '-'
18+
#define LONG_HELPCMD "--help"
19+
#define SHORT_HELPCMD "-h"
1720

18-
ArgParser::ArgParser() {
21+
#define DEFAULT_PARSER_NAME "Default Project Name"
22+
#define DEFAULT_PARSER_DESCRIPTION "Default Project Description"
23+
24+
25+
ArgParser::ArgParser()
26+
: name(DEFAULT_PARSER_NAME), description(DEFAULT_PARSER_DESCRIPTION){
27+
28+
}
29+
30+
ArgParser::ArgParser(const string& _name, const string& _description)
31+
: name(_name), description(_description){
1932

2033
}
2134

@@ -36,6 +49,10 @@ bool ArgParser::precheck(
3649
return false;
3750
}
3851

52+
if(longcmd == LONG_HELPCMD || shortcmd == SHORT_HELPCMD){
53+
return false;
54+
}
55+
3956
for (const auto& argument : args) {
4057
if( argument.second->getName() == name ||
4158
argument.second->getLongCmd() == longcmd ||
@@ -51,6 +68,30 @@ void ArgParser::postcheck(){
5168

5269
}
5370

71+
void ArgParser::help(){
72+
73+
if (help_callback) {
74+
help_callback();
75+
}
76+
77+
printf("%s\n\n", name.c_str());
78+
printf("DESCRIPTION:\n\t%s\n", description.c_str());
79+
80+
printf("USAGE:\n");
81+
for (const auto& argument : args) {
82+
printf("\t%s, %s \t: %s\n",
83+
argument.second->getShortCmd().c_str(),
84+
argument.second->getLongCmd().c_str(),
85+
argument.second->getHelp().c_str());
86+
}
87+
88+
printf("\n");
89+
}
90+
91+
void ArgParser::setHelpCallback(const Callback& callback){
92+
help_callback = callback;
93+
}
94+
5495
template <>
5596
ArgStatus ArgParser::addArgument<bool>(
5697
const string& name,
@@ -164,6 +205,11 @@ ArgStatus ArgParser::parse(int argc, char* argv[]){
164205
Argument* temp = nullptr;
165206
ArgStatus ret = PARSER_ERROR;
166207

208+
if(strcmp(argv[1], LONG_HELPCMD) == PARSER_OK || strcmp(argv[1], SHORT_HELPCMD) == PARSER_OK){
209+
help();
210+
return PARSER_OK;
211+
}
212+
167213
for (int i = 0; i < argc; ++i) {
168214
temp = find(argv[i]);
169215
if(temp != nullptr && !temp->status()) {

argparser/src/argstring.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ ArgStatus StringArgument::loadValue(const string& arg, int pos){
1616

1717
if(!arg.empty()){
1818
value = arg;
19-
printf("String Value: %s\n", value.c_str());
2019
}
2120
else{
2221
return PARSER_INVALID_ARGUMENT;

argparser/src/argument.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ string Argument::getLongCmd() const {
4040
return longcmd;
4141
}
4242

43+
string Argument::getHelp() const {
44+
return help;
45+
}
46+
4347
void Argument::setArgPosition(int position){
4448
argpos = position;
4549
}

main.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,40 @@ using namespace std;
44

55

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

9-
parser.addArgument("debug", "-d", "--debug", "This is a flag argument", false);
12+
// char* sargv[] = { (char*)"program_name",
13+
// (char*)"--int", (char*)"123", // Int Argument
14+
// (char*)"--string", (char*)"Hello World", // String Argument
15+
// (char*)"--bool", // Bool Argument
16+
// (char*)"--double", (char*)"65.343"}; // Double Argument
17+
char* sargv[] = { (char*)"program_name", (char*)"-h"};
18+
int sargc = sizeof(sargv) / sizeof(sargv[0]);
19+
20+
21+
parser.addArgument("bool", "-b", "--bool", "This is a flag argument", false);
1022
parser.addArgument("int", "-i", "--int", "This is a int argument", 10);
1123
parser.addArgument("double", "-d", "--double", "This is a double argument", 0.0);
1224
parser.addArgument("string", "-s", "--string", "This is a string argument", "null");
1325
parser.parse(argc, argv);
14-
parser.print();
26+
//parser.print();
1527

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

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

24-
if(parser.argExists("debug")){
25-
printf("debug is there\n");
26-
}
27-
else{
28-
printf("debug is not there\n");
29-
}
33+
// printf("Int Value : %d\n", intValue);
34+
// printf("Double Value : %f\n", doubleValue);
35+
// printf("String Value : %s\n", stringValue.c_str());
3036

37+
// if(parser.argExists("debug")){
38+
// printf("debug is there\n");
39+
// }
40+
// else{
41+
// printf("debug is not there\n");
42+
// }
3143
}

0 commit comments

Comments
 (0)