Skip to content

Commit 39f57e3

Browse files
Jirous, MichalJirous, Michal
Jirous, Michal
authored and
Jirous, Michal
committed
Added helpDisplayed flag
1 parent cad5e0b commit 39f57e3

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

cppcommandline.qbs

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Project
1212

1313
name: "cppcommandlineexample"
1414
cpp.includePaths: [ "include", "example" ]
15+
cpp.cxxLanguageVersion: "c++11"
1516
files: [ "example/*" ]
1617
}
1718

@@ -20,6 +21,7 @@ Project
2021
Depends { name: "Qt.testlib" }
2122
name: "cppcommandlinetest"
2223
cpp.includePaths: [ "include", "test" ]
24+
cpp.cxxLanguageVersion: "c++11"
2325
files: [ "test/*" ]
2426
}
2527
}

include/cppcommandline.h

+7
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,11 @@ class Parser
467467
return mAppName;
468468
}
469469

470+
bool helpDisplayed() const
471+
{
472+
return mHelpDisplayed;
473+
}
474+
470475
Option &option()
471476
{
472477
mOptions.emplace_back(Option());
@@ -540,6 +545,7 @@ class Parser
540545
}
541546

542547
std::cout << std::endl;
548+
mHelpDisplayed = true;
543549
return;
544550
}
545551
}
@@ -594,6 +600,7 @@ class Parser
594600
std::vector<std::string> mArgs;
595601
std::vector<Option> mOptions;
596602
bool mHelp = true;
603+
bool mHelpDisplayed = false;
597604
};
598605

599606
}

test/cppcommandlinetest.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void CppCommandLineTest::OptionDefaultCtor()
1313
QCOMPARE(option.description(), std::string());
1414
QCOMPARE(option.isRequired(), false);
1515
QCOMPARE(option.defaultValue<std::string>(), std::string());
16-
QCOMPARE(option.boundValue<std::string>(), nullptr);
16+
QVERIFY(option.boundValue<std::string>() == nullptr);
1717
}
1818

1919
void CppCommandLineTest::OptionLongNameCtor()
@@ -359,6 +359,7 @@ void CppCommandLineTest::parse()
359359
QCOMPARE(option, std::string("file"));
360360
QCOMPARE(another, 10);
361361
QCOMPARE(positional, std::string("somefile"));
362+
QVERIFY(!parser.helpDisplayed());
362363
}
363364
}
364365

@@ -398,4 +399,17 @@ void CppCommandLineTest::parseFailed()
398399
}
399400
}
400401

402+
void CppCommandLineTest::help()
403+
{
404+
405+
{
406+
SCENARIO("Unmatched required option")
407+
std::vector<const char*> args{"./app", "-h"};
408+
cppcommandline::Parser parser;
409+
parser.parse(static_cast<int>(args.size()), const_cast<char**>(args.data()));
410+
QVERIFY(parser.helpDisplayed());
411+
}
412+
413+
}
414+
401415
QTEST_APPLESS_MAIN(CppCommandLineTest)

test/cppcommandlinetest.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ private slots:
2222
void ParserOptionLongName();
2323
void parse();
2424
void parseFailed();
25+
void help();
2526

2627
private:
2728
std::unique_ptr<char**, void(*)(char**)> createArguments(std::vector<std::string> arguments);

0 commit comments

Comments
 (0)