Skip to content

Commit 9ce3008

Browse files
committed
Added tests for negative numbers and fixed the interference between argument names and negative positional numeric values.
1 parent e1c5ff5 commit 9ce3008

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

include/cppcommandline.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ class Option
337337
{
338338
KeyValue keyValue;
339339
std::cmatch m;
340-
if(std::regex_match(argument.c_str(), m, std::regex("^(--|-)([a-zA-Z\\d]+)((=(.*))|)")))
340+
if(std::regex_match(argument.c_str(), m, std::regex("^(--|-)([a-zA-Z][a-zA-Z\\d]*)((=(.*))|)")))
341341
{
342342
keyValue.key = m.size() > 2 ? std::string(m[2]) : "";
343343
keyValue.value = m.size() > 4 ? std::string(m[5]) : "";
@@ -354,12 +354,12 @@ class Option
354354

355355
bool isLongNameArgument(std::string argument) const
356356
{
357-
return std::regex_match(argument, std::regex("^--[a-zA-Z\\d]+$"));
357+
return std::regex_match(argument, std::regex("^--[a-zA-Z][a-zA-Z\\d]+$"));
358358
}
359359

360360
bool isShortNameArgument(std::string argument) const
361361
{
362-
return std::regex_match(argument, std::regex("^-[a-zA-Z\\d]$"));
362+
return std::regex_match(argument, std::regex("^-[a-zA-Z]$"));
363363
}
364364

365365
bool isInteger(std::string argument) const

test/cppcommandlinetest.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ void CppCommandLineTest::match()
184184
QCOMPARE(10, value);
185185
}
186186

187+
{
188+
SCENARIO("Option match negative `int` argument")
189+
std::vector<std::string> arguments{"-10"};
190+
int value = 0;
191+
cppcommandline::Option option;
192+
option.bindTo(value);
193+
QCOMPARE(arguments.cend(), option.match(arguments.cbegin(), arguments.cend()));
194+
QCOMPARE(-10, value);
195+
}
196+
187197
{
188198
SCENARIO("Option match `double` argument")
189199
std::vector<std::string> arguments{"5.5"};
@@ -194,6 +204,16 @@ void CppCommandLineTest::match()
194204
QCOMPARE(double(5.5), value);
195205
}
196206

207+
{
208+
SCENARIO("Option match negative `double` argument")
209+
std::vector<std::string> arguments{"-5.5"};
210+
double value = 0;
211+
cppcommandline::Option option;
212+
option.bindTo(value);
213+
QCOMPARE(arguments.cend(), option.match(arguments.cbegin(), arguments.cend()));
214+
QCOMPARE(double(-5.5), value);
215+
}
216+
197217
{
198218
SCENARIO("Option match `long long` argument")
199219
std::vector<std::string> arguments{"999999999999"};
@@ -203,6 +223,16 @@ void CppCommandLineTest::match()
203223
QCOMPARE(arguments.cend(), option.match(arguments.cbegin(), arguments.cend()));
204224
QCOMPARE(qint64(999999999999), value);
205225
}
226+
227+
{
228+
SCENARIO("Option match negative `long long` argument")
229+
std::vector<std::string> arguments{"-999999999999"};
230+
qint64 value = 0;
231+
cppcommandline::Option option;
232+
option.bindTo(value);
233+
QCOMPARE(arguments.cend(), option.match(arguments.cbegin(), arguments.cend()));
234+
QCOMPARE(qint64(-999999999999), value);
235+
}
206236
}
207237

208238
void CppCommandLineTest::ParserOption()

0 commit comments

Comments
 (0)