Skip to content

Commit 4202251

Browse files
committed
Added application name regex (wip).
1 parent ae2b0e4 commit 4202251

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

include/cppcommandline.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,16 @@ template<> Option::Type Option::getType<double>() const { return Type::Double; }
424424
class Parser
425425
{
426426
public:
427+
std::string command() const
428+
{
429+
return mCommand;
430+
}
431+
432+
std::string applicationName() const
433+
{
434+
return mAppName;
435+
}
436+
427437
Option &option()
428438
{
429439
mOptions.emplace_back(Option());
@@ -438,9 +448,24 @@ class Parser
438448

439449
void parse(int argc, char **argv)
440450
{
451+
if(argc == 0)
452+
throw(std::logic_error("Missing mandatory first command line argument"));
453+
else
454+
{
455+
mCommand = argv[0];
456+
std::cmatch m;
457+
if(std::regex_match(mCommand.c_str(), m, std::regex("([^\\\\\\/]+$)")))
458+
{
459+
mAppName = m[1];
460+
461+
if(std::regex_match(mAppName.c_str(), std::regex("\\.exe$")))
462+
mAppName.erase(mAppName.size() - 4, 4);
463+
}
464+
}
465+
441466
mArgs = std::vector<std::string>();
442467

443-
for(int i = 0; i < argc; i++)
468+
for(int i = 1; i < argc; i++)
444469
mArgs.emplace_back(std::string(argv[i]));
445470

446471
std::vector<Option*> options;
@@ -478,6 +503,8 @@ class Parser
478503
}
479504

480505
private:
506+
std::string mCommand;
507+
std::string mAppName;
481508
std::vector<std::string> mArgs;
482509
std::vector<Option> mOptions;
483510
};

test/cppcommandlinetest.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,19 @@ void CppCommandLineTest::parse()
262262
{
263263
{
264264
SCENARIO("Single positional option")
265-
std::vector<const char*> args{"value"};
265+
std::vector<const char*> args{"./app.exe", "value"};
266266
cppcommandline::Parser parser;
267267
std::string value;
268268
parser.option().bindTo(value);
269269
parser.parse(static_cast<int>(args.size()), const_cast<char**>(args.data()));
270270
QCOMPARE(value, std::string("value"));
271+
QCOMPARE(parser.command(), std::string("./app.exe"));
272+
QCOMPARE(parser.applicationName(), std::string("app"));
271273
}
272274

273275
{
274276
SCENARIO("Multiple positional options")
275-
std::vector<const char*> args{"value", "-10", "5.5"};
277+
std::vector<const char*> args{"./app", "value", "-10", "5.5"};
276278
cppcommandline::Parser parser;
277279
std::string value;
278280
int iValue = 0;
@@ -288,7 +290,7 @@ void CppCommandLineTest::parse()
288290

289291
{
290292
SCENARIO("Single long name option")
291-
std::vector<const char*> args{"--value"};
293+
std::vector<const char*> args{"./app", "--value"};
292294
cppcommandline::Parser parser;
293295
bool value;
294296
parser.option("value").bindTo(value);
@@ -298,7 +300,7 @@ void CppCommandLineTest::parse()
298300

299301
{
300302
SCENARIO("Multiple long name options")
301-
std::vector<const char*> args{"--value", "--option=file", "--yetanother", "10"};
303+
std::vector<const char*> args{"./app", "--value", "--option=file", "--yetanother", "10"};
302304
cppcommandline::Parser parser;
303305
bool value;
304306
std::string option;
@@ -314,7 +316,7 @@ void CppCommandLineTest::parse()
314316

315317
{
316318
SCENARIO("Single short name option")
317-
std::vector<const char*> args{"-v"};
319+
std::vector<const char*> args{"./app", "-v"};
318320
cppcommandline::Parser parser;
319321
bool value;
320322
parser.option("value").asShortName("v").bindTo(value);
@@ -324,7 +326,7 @@ void CppCommandLineTest::parse()
324326

325327
{
326328
SCENARIO("Multiple short name options")
327-
std::vector<const char*> args{"-v", "-o=file", "-y", "10"};
329+
std::vector<const char*> args{"./app", "-v", "-o=file", "-y", "10"};
328330
cppcommandline::Parser parser;
329331
bool value;
330332
std::string option;
@@ -340,7 +342,7 @@ void CppCommandLineTest::parse()
340342

341343
{
342344
SCENARIO("Mixed options")
343-
std::vector<const char*> args{"-v", "-o=file", "--yetanother", "10", "somefile"};
345+
std::vector<const char*> args{"./app", "-v", "-o=file", "--yetanother", "10", "somefile"};
344346
cppcommandline::Parser parser;
345347
bool value;
346348
std::string option;
@@ -362,13 +364,13 @@ void CppCommandLineTest::parseFailed()
362364
{
363365
{
364366
SCENARIO("Unmatched argument")
365-
std::vector<const char*> args{"-v"};
367+
std::vector<const char*> args{"./app", "-v"};
366368
QVERIFY_EXCEPTION_THROWN(cppcommandline::Parser().parse(static_cast<int>(args.size()), const_cast<char**>(args.data())), std::logic_error);
367369
}
368370

369371
{
370372
SCENARIO("Unmatched required option")
371-
std::vector<const char*> args{"-v"};
373+
std::vector<const char*> args{"./app", "-v"};
372374
cppcommandline::Parser parser;
373375
parser.option("longName").asShortName("v");
374376
parser.option().required();
@@ -377,7 +379,7 @@ void CppCommandLineTest::parseFailed()
377379

378380
{
379381
SCENARIO("Type mismatch")
380-
std::vector<const char*> args{"-v=hello"};
382+
std::vector<const char*> args{"./app", "-v=hello"};
381383
cppcommandline::Parser parser;
382384
int value = 0;
383385
parser.option("value").asShortName("v").bindTo(value);
@@ -386,7 +388,7 @@ void CppCommandLineTest::parseFailed()
386388

387389
{
388390
SCENARIO("Missing value")
389-
std::vector<const char*> args{"-v"};
391+
std::vector<const char*> args{"./app", "-v"};
390392
cppcommandline::Parser parser;
391393
int value = 0;
392394
parser.option("value").asShortName("v").bindTo(value);

0 commit comments

Comments
 (0)