Skip to content

Commit 6e9689d

Browse files
committed
Minor fixes
1 parent 6a0a934 commit 6e9689d

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ if(NOT CMAKE_BUILD_TYPE)
1111
endif()
1212

1313
include(GNUInstallDirs)
14-
add_library(BazPO INTERFACE
15-
include/BazPO.hpp)
14+
15+
add_library(BazPO INTERFACE)
1616

1717
target_include_directories(
1818
BazPO

include/BazPO.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,12 +454,14 @@ namespace BazPO
454454
{
455455
if (pair.second.Mandatory && !pair.second.Exists)
456456
{
457-
*m_outputStream << pair.second.Parameter << " is a required parameter" << std::endl;
457+
PrintOptionUsage(pair.second);
458+
*m_outputStream << " is a required parameter" << std::endl;
458459
if (m_askInputForMandatoryOptions)
459460
{
461+
PrintOptionUsage(pair.second);
462+
*m_outputStream << ": ";
460463
std::string temp;
461-
*m_outputStream << pair.second.Parameter << ": " << std::endl;
462-
*m_inputStream >> temp;
464+
std::getline(*m_inputStream, temp);
463465

464466
m_inputStorage.push_back(temp);
465467
pair.second.Value = m_inputStorage.back().c_str();
@@ -566,10 +568,18 @@ namespace BazPO
566568
str.append("[").append(value).append("]");
567569
return str;
568570
}
569-
571+
namespace _detail
572+
{
573+
class OptionMismatchException
574+
: public std::exception
575+
{
576+
const char* err = "Tagless options cannot be combined with other options!";
577+
virtual const char* what() const noexcept override { return err; };
578+
};
579+
}
570580
void Cli::ThrowOnOptionMismatch(bool tagless)
571581
{
572-
std::exception e("Tagless options cannot be combined with other options!");
582+
_detail::OptionMismatchException e;
573583
if ((IsTagless() && IsNormal()) || (!IsTagless() && tagless) || (IsNormal() && tagless))
574584
throw e;
575585
}

manualtest/BazPOManual.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ int main(int argc,const char* argv[])
1515
auto aoption = po.GetOption("-a");
1616

1717
std::cout << "EXISTS:" << aoption.exists() << std::endl;
18+
std::cout << "VALUE:" << aoption.value() << std::endl;
1819
std::cout << "INT:" << aoption.value_as<int>() << std::endl;
1920
std::cout << "BOOL:" << aoption.value_bool() << std::endl;
20-
std::cout << aoption.value();
21+
2122
while (1);
2223
}

test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ FetchContent_Declare(
88
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
99
FetchContent_MakeAvailable(googletest)
1010
enable_testing()
11-
1211
add_executable(
1312
BazPOTest
1413
test.cpp

test/test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,38 @@ TEST_F(ProgramOptionsTest, mandatory_option_asked_when_not_provided) {
359359
EXPECT_EQ(std::string("input"), a.value());
360360
}
361361

362+
TEST_F(ProgramOptionsTest, multi_line_input_parsed_correctly) {
363+
int argc = 1;
364+
const char* argv[] = { "programoptions" };
365+
std::stringstream str("input input2 input3");
366+
Cli po(argc, argv);
367+
po.UserInputRequiredForAbsentMandatoryOptions();
368+
po.Add("-a", "--alpha", "Option A", true);
369+
po.ChangeIO(&std::cout, &str);
370+
371+
po.ParseArguments();
372+
373+
auto a = po.GetOption("-a");
374+
EXPECT_EQ(true, a.exists());
375+
EXPECT_EQ(std::string("input input2 input3"), a.value());
376+
}
377+
378+
TEST_F(ProgramOptionsTest, multi_line_double_parsed_correctly) {
379+
int argc = 1;
380+
const char* argv[] = { "programoptions" };
381+
std::stringstream str("15.87396509125677 \r\n");
382+
Cli po(argc, argv);
383+
po.UserInputRequiredForAbsentMandatoryOptions();
384+
po.Add("-a", "--alpha", "Option A", true);
385+
po.ChangeIO(&std::cout, &str);
386+
387+
po.ParseArguments();
388+
389+
auto a = po.GetOption("-a");
390+
EXPECT_EQ(true, a.exists());
391+
EXPECT_EQ(15.87396509125677, a.value_as<double>());
392+
}
393+
362394
TEST_F(ProgramOptionsTest, function_option_executes_successfully) {
363395
bool executed = false;
364396
int argc = 2;

0 commit comments

Comments
 (0)