Skip to content

Commit 0883de3

Browse files
fixed string argument issue
1 parent 4d078a2 commit 0883de3

4 files changed

Lines changed: 21 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ mono_crash.*
580580
[Dd]ebugPublic/
581581
[Rr]elease/
582582
[Rr]eleases/
583+
[Bb]uild/
583584
x64/
584585
x86/
585586
[Ww][Ii][Nn]32/

argparser/src/argparser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ ArgStatus ArgParser::parse(int argc, char* argv[]){
235235
Argument* prev = nullptr;
236236
ArgStatus ret = PARSER_OK;
237237

238-
if(defaultHelp){
238+
if(argc > 1 && defaultHelp){
239239
if(strcmp(argv[1], LONG_HELPCMD) == PARSER_OK || strcmp(argv[1], SHORT_HELPCMD) == PARSER_OK){
240240
help();
241241
return PARSE_DEFAULT_HELP_OK;

argparser/src/argstring.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <argstring.h>
22
#include <typedef.h>
3+
#include <stdexcept>
34

45
StringArgument::StringArgument(const std::string& _name, const std::string& _shortcmd, const std::string& _longcmd, const std::string& _help, const std::string& defaultval)
56
: Argument(_name, _shortcmd, _longcmd, _help, true),
@@ -15,7 +16,17 @@ ArgStatus StringArgument::loadValue(const std::string& arg, int pos){
1516
setArgPosition(pos);
1617

1718
if(!arg.empty()){
18-
value = arg;
19+
try{
20+
value = arg;
21+
return PARSER_OK;
22+
}catch (const std::invalid_argument& e) {
23+
fprintf(stderr, "Error: Invalid string argument: %s (%s)\n", arg.c_str(), e.what());
24+
return PARSER_INVALID_ARGUMENT;
25+
}
26+
catch (const std::exception& e) {
27+
fprintf(stderr, "Unexpected error while parsing string: %s\n", e.what());
28+
return PARSER_ERROR;
29+
}
1930
}
2031
else{
2132
return PARSER_INVALID_ARGUMENT;

main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <iostream>
22
#include <cmath>
33
#include <argparser.h>
4+
#include <string.h>
45

56
// Function to calculate the volume of a rectangular box given its dimensions
67
double calculateBoxVolume(double length, double width, double depth) {
@@ -19,6 +20,7 @@ int main(int argc, char* argv[]) {
1920
parser.addArgument("width", "-w", "--width", "Width of the box", 0.0);
2021
parser.addArgument("depth", "-d", "--depth", "Depth of the box", 0.0);
2122
parser.addArgument("verbose", "-v", "--verbose", "Enable verbose output", false);
23+
parser.addArgument("tstr", "-s", "--string", "Test string", "Test");
2224

2325
// Parse command-line arguments
2426
ret = parser.parse(argc, argv);
@@ -34,11 +36,16 @@ int main(int argc, char* argv[]) {
3436
}
3537

3638
// Retrieve values
39+
std::string test_str = parser.get<std::string>("tstr");
3740
double length = parser.get<double>("length");
3841
double width = parser.get<double>("width");
3942
double depth = parser.get<double>("depth");
4043
bool verbose = parser.get<bool>("verbose");
4144

45+
if (parser.argExists("tstr")) {
46+
printf("String Test Value %s\n", test_str.c_str());
47+
}
48+
4249
// Check if the provided dimensions are valid
4350
if (parser.argExists("length") && parser.argExists("width") && parser.argExists("depth")) {
4451
// Calculate the volume of the box

0 commit comments

Comments
 (0)