Skip to content

[13884] Use a single optionparser header for all tools and examples #2508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/statistics_coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
--exclude='.*test/.*' \
--exclude='.*/sqlite3.c' \
--exclude='.*/optionparser.h' \
--exclude='.*/optionparser.hpp' \
--exclude-unreachable-branches

- name: Upload coverage
Expand Down
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ option(FASTDDS_STATISTICS "Enable Fast DDS Statistics Module" OFF)
###############################################################################
add_subdirectory(src/cpp)

###############################################################################
# Add http://optionparser.sourceforge.net/ as unified cli parser
###############################################################################
add_subdirectory(thirdparty/optionparser)

###############################################################################
# Testing options
###############################################################################
Expand Down
2 changes: 1 addition & 1 deletion cmake/packaging/Config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ else()
endif()
endif()

@INCLUDE_FASTDS_TARGETS@
@INCLUDE_FASTDDS_TARGETS@
3 changes: 2 additions & 1 deletion examples/C++/DDS/BasicConfigurationExample/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cmake_minimum_required(VERSION 2.8.12)

if(NOT CMAKE_VERSION VERSION_LESS 3.0)
cmake_policy(SET CMP0048 NEW)
cmake_policy(SET CMP0028 NEW)
endif()

project(BasicConfigurationExample)
Expand Down Expand Up @@ -48,6 +49,6 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
)
target_link_libraries(${PROJECT_NAME} fastrtps fastcdr)
target_link_libraries(${PROJECT_NAME} fastrtps fastcdr fastdds::optionparser)
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION examples/C++/DDS/${PROJECT_NAME}/${BIN_INSTALL_DIR})
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <iostream>
#include <string>

#include "optionparser.h"
#include <optionparser.hpp>

namespace option = eprosima::option;

struct Arg : public option::Arg
{
Expand Down
3 changes: 2 additions & 1 deletion examples/C++/DDS/HelloWorldExampleTCP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cmake_minimum_required(VERSION 2.8.12)

if(NOT CMAKE_VERSION VERSION_LESS 3.0)
cmake_policy(SET CMP0048 NEW)
cmake_policy(SET CMP0028 NEW)
endif()

project("HelloWorldExampleTCP")
Expand Down Expand Up @@ -60,6 +61,6 @@ target_compile_definitions(DDSHelloWorldExampleTCP PRIVATE
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
)
target_link_libraries(DDSHelloWorldExampleTCP fastrtps fastcdr foonathan_memory)
target_link_libraries(DDSHelloWorldExampleTCP fastrtps fastcdr foonathan_memory fastdds::optionparser)
install(TARGETS DDSHelloWorldExampleTCP
RUNTIME DESTINATION examples/C++/DDS/HelloWorldExampleTCP/${BIN_INSTALL_DIR})
107 changes: 66 additions & 41 deletions examples/C++/DDS/HelloWorldExampleTCP/HelloWorld_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,52 @@

#include <string>

#include "optionparser.h"
#include <optionparser.hpp>

struct Arg: public option::Arg
namespace option = eprosima::option;

struct Arg : public option::Arg
{
static void print_error(const char* msg1, const option::Option& opt, const char* msg2)
static void print_error(
const char* msg1,
const option::Option& opt,
const char* msg2)
{
fprintf(stderr, "%s", msg1);
fwrite(opt.name, opt.namelen, 1, stderr);
fprintf(stderr, "%s", msg2);
}

static option::ArgStatus Unknown(const option::Option& option, bool msg)
static option::ArgStatus Unknown(
const option::Option& option,
bool msg)
{
if (msg) print_error("Unknown option '", option, "'\n");
if (msg)
{
print_error("Unknown option '", option, "'\n");
}
return option::ARG_ILLEGAL;
}

static option::ArgStatus Required(const option::Option& option, bool msg)
static option::ArgStatus Required(
const option::Option& option,
bool msg)
{
if (option.arg != 0 && option.arg[0] != 0)
return option::ARG_OK;
{
return option::ARG_OK;
}

if (msg) print_error("Option '", option, "' requires an argument\n");
if (msg)
{
print_error("Option '", option, "' requires an argument\n");
}
return option::ARG_ILLEGAL;
}

static option::ArgStatus Numeric(const option::Option& option, bool msg)
static option::ArgStatus Numeric(
const option::Option& option,
bool msg)
{
char* endptr = 0;
if (option.arg != 0 && strtol(option.arg, &endptr, 10))
Expand All @@ -69,7 +88,9 @@ struct Arg: public option::Arg
return option::ARG_ILLEGAL;
}

static option::ArgStatus String(const option::Option& option, bool msg)
static option::ArgStatus String(
const option::Option& option,
bool msg)
{
if (option.arg != 0)
{
Expand All @@ -81,11 +102,13 @@ struct Arg: public option::Arg
}
return option::ARG_ILLEGAL;
}

};



enum optionIndex {
enum optionIndex
{
UNKNOWN_OPT,
HELP,
SAMPLES,
Expand Down Expand Up @@ -118,38 +141,40 @@ enum optionIndex {
std::cout << "The optional arguments are: subscriber [server_ip] [port] " << std::endl;
std::cout << "\t- server_ip: IP Address of the publisher. " << std::endl;
std::cout << "\t- port: Physical Port where the publisher is listening for connections." << std::endl << std::endl;
*/
*/

const option::Descriptor usage[] = {
{ UNKNOWN_OPT, 0,"", "", Arg::None,
"Usage: HelloWorldExampleTCP <publisher|subscriber>\n\nGeneral options:" },
{ HELP, 0,"h", "help", Arg::None, " -h \t--help \tProduce help message." },
{ UNKNOWN_OPT, 0, "", "", Arg::None,
"Usage: HelloWorldExampleTCP <publisher|subscriber>\n\nGeneral options:" },
{ HELP, 0, "h", "help", Arg::None, " -h \t--help \tProduce help message." },
{ TLS, 0, "t", "tls", Arg::None, " -t \t--tls \tUse TLS." },
{ WHITELIST, 0, "w", "whitelist", Arg::String, " -w \t--whitelist \tUse Whitelist." },

{ UNKNOWN_OPT, 0,"", "", Arg::None, "\nPublisher options:"},
{ SAMPLES,0,"s","samples", Arg::Numeric,
" -s <num>, \t--samples=<num> \tNumber of samples (0, default, infinite)." },
{ INTERVAL,0,"i","interval", Arg::Numeric,
" -i <num>, \t--interval=<num> \tTime between samples in milliseconds (Default: 100)." },
{ IP,0,"a","address", Arg::String,
" -a <address>, \t--address=<address> \tPublic IP Address of the publisher (Default: None)." },
{ UNKNOWN_OPT, 0, "", "", Arg::None, "\nPublisher options:"},
{ SAMPLES, 0, "s", "samples", Arg::Numeric,
" -s <num>, \t--samples=<num> \tNumber of samples (0, default, infinite)." },
{ INTERVAL, 0, "i", "interval", Arg::Numeric,
" -i <num>, \t--interval=<num> \tTime between samples in milliseconds (Default: 100)." },
{ IP, 0, "a", "address", Arg::String,
" -a <address>, \t--address=<address> \tPublic IP Address of the publisher (Default: None)." },
{ PORT, 0, "p", "port", Arg::Numeric,
" -p <num>, \t--port=<num> \tPhysical Port to listening incoming connections (Default: 5100)." },
" -p <num>, \t--port=<num> \tPhysical Port to listening incoming connections (Default: 5100)." },

{ UNKNOWN_OPT, 0,"", "", Arg::None, "\nSubscriber options:"},
{ IP,0,"a","address", Arg::String,
" -a <address>, \t--address=<address> \tIP Address of the publisher (Default: 127.0.0.1)." },
{ UNKNOWN_OPT, 0, "", "", Arg::None, "\nSubscriber options:"},
{ IP, 0, "a", "address", Arg::String,
" -a <address>, \t--address=<address> \tIP Address of the publisher (Default: 127.0.0.1)." },
{ PORT, 0, "p", "port", Arg::Numeric,
" -p <num>, \t--port=<num> \tPhysical Port where the publisher is listening for connections (Default: 5100)." },
" -p <num>, \t--port=<num> \tPhysical Port where the publisher is listening for connections (Default: 5100)." },

{ 0, 0, 0, 0, 0, 0 }
};

using namespace eprosima;
using namespace fastrtps;
using namespace rtps;
int main(int argc, char** argv)
int main(
int argc,
char** argv)
{
int columns;

Expand All @@ -167,7 +192,7 @@ int main(int argc, char** argv)
}
#else
columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 80;
#endif
#endif // if defined(_WIN32)

std::cout << "Starting " << std::endl;
int type = 1;
Expand Down Expand Up @@ -268,23 +293,23 @@ int main(int argc, char** argv)
switch (type)
{
case 1:
{
HelloWorldPublisher mypub;
if (mypub.init(wan_ip, static_cast<uint16_t>(port), use_tls, whitelist))
{
HelloWorldPublisher mypub;
if (mypub.init(wan_ip, static_cast<uint16_t>(port), use_tls, whitelist))
{
mypub.run(count, sleep);
}
break;
mypub.run(count, sleep);
}
break;
}
case 2:
{
HelloWorldSubscriber mysub;
if (mysub.init(wan_ip, static_cast<uint16_t>(port), use_tls, whitelist))
{
HelloWorldSubscriber mysub;
if (mysub.init(wan_ip, static_cast<uint16_t>(port), use_tls, whitelist))
{
mysub.run();
}
break;
mysub.run();
}
break;
}
}
Domain::stopAll();
return 0;
Expand Down
Loading