Skip to content

Commit

Permalink
tray-monitor: replacing custom CLI parsing with CLI11
Browse files Browse the repository at this point in the history
  • Loading branch information
alaaeddineelamri committed Jul 11, 2022
1 parent f829cab commit 8a79834
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 88 deletions.
4 changes: 2 additions & 2 deletions core/src/qt-tray-monitor/CMakeLists.txt
@@ -1,6 +1,6 @@
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2017-2021 Bareos GmbH & Co. KG
# Copyright (C) 2017-2022 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -69,7 +69,7 @@ else()
add_executable(bareos-tray-monitor ${SOURCES} main.qrc)
endif()

target_link_libraries(bareos-tray-monitor PRIVATE bareos)
target_link_libraries(bareos-tray-monitor PRIVATE bareos CLI11::CLI11)

if(Qt4_FOUND)
target_link_libraries(bareos-tray-monitor PRIVATE Qt4::QtGui)
Expand Down
124 changes: 40 additions & 84 deletions core/src/qt-tray-monitor/tray-monitor.cc
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2004-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2020 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -34,6 +34,7 @@
#include "monitoritemthread.h"
#include "lib/bsignal.h"
#include "lib/parse_conf.h"
#include "lib/cli.h"

ConfigurationParser* my_config = nullptr;

Expand All @@ -42,91 +43,46 @@ ConfigurationParser* my_config = nullptr;
* - QCoreApplication for tests when no GUI must be used */
static QCoreApplication* app = nullptr;

static void usage()
{
std::vector<char> copyright(1024);
kBareosVersionStrings.FormatCopyrightWithFsfAndPlanets(
copyright.data(), copyright.size(), 2004);
QString out
= QString(_("%1"
"Usage: tray-monitor [options]\n"
" -c <path> use <path> as configuration file or "
"directory\n"
" -d <nn> set debug level to <nn>\n"
" -dt print timestamp in debug output\n"
" -t test - read configuration and exit\n"
" -rc test - do connection test\n"
" -xc print configuration and exit\n"
" -xs print configuration file schema in JSON "
"format "
"and exit\n"
" -? print this message.\n"
"\n"))
.arg(copyright.data());

#if HAVE_WIN32
QMessageBox::information(0, "Help", out);
#else
fprintf(stderr, "%s", out.toUtf8().data());
#endif
}

static void ParseCommandLine(int argc, char* argv[], cl_opts& cl)
{
int ch;
while ((ch = getopt(argc, argv, "bc:d:th?f:r:s:x:")) != -1) {
switch (ch) {
case 'c': /* configuration file */
if (cl.configfile_) { free(static_cast<void*>(cl.configfile_)); }
cl.configfile_ = strdup(optarg);
break;

case 'd':
if (*optarg == 't') {
dbg_timestamp = true;
} else {
debug_level = atoi(optarg);
if (debug_level <= 0) { debug_level = 1; }
}
break;

case 't':
cl.test_config_only_ = true;
break;

case 'x': /* export configuration/schema and exit */
if (*optarg == 's') {
cl.export_config_schema_ = true;
} else if (*optarg == 'c') {
cl.export_config_ = true;
} else {
usage();
exit(1);
}
break;

case 'r':
if ((*optarg) == 'c') {
cl.do_connection_test_only_ = true;
} else {
usage();
exit(1);
}
break;

case 'h':
case '?':
default:
usage();
exit(1);
}
}
argc -= optind;
// argv += optind;

if (argc) {
usage();
exit(1);
CLI::App tray_monitor_app;
InitCLIApp(tray_monitor_app, "The Bareos Tray Monitor.", 2004);

tray_monitor_app
.add_option(
"-c,--config",
[&cl](std::vector<std::string> val) {
if (cl.configfile_) { free(static_cast<void*>(cl.configfile_)); }
cl.configfile_ = strdup(val.front().c_str());
return true;
},
"Use <path> as configuration file or directory.")
->check(CLI::ExistingPath)
->type_name("<path>");

AddDebugOptions(tray_monitor_app);

tray_monitor_app.add_flag("-t,--test-config", cl.test_config_only_,
"Test - read configuration and exit.");

CLI::Option* xc
= tray_monitor_app.add_flag("--xc,--export-config", cl.export_config_,
"Print configuration resources and exit.");


tray_monitor_app
.add_flag("--xs,--export-schema", cl.export_config_schema_,
"Print configuration schema in JSON format and exit.")
->excludes(xc);

tray_monitor_app.add_flag("--rc,--test-connection",
cl.do_connection_test_only_,
"Test connection only.");

try {
(tray_monitor_app).parse((argc), (argv));
} catch (const CLI::ParseError& e) {
exit((tray_monitor_app).exit(e));
}
}

Expand Down
4 changes: 2 additions & 2 deletions docs/manuals/CMakeLists.txt
Expand Up @@ -79,9 +79,9 @@ add_custom_command(
add_custom_command(
OUTPUT
${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-tray-monitor-config-schema.json
COMMAND bareos-tray-monitor -xs >/dev/null
COMMAND bareos-tray-monitor --xs >/dev/null
COMMAND
bareos-tray-monitor -xs
bareos-tray-monitor --xs
>${PROJECT_SOURCE_DIR}/source/include/autogenerated/bareos-tray-monitor-config-schema.json
COMMAND
sed --in-place --expression="/\\\"version\\\":/d"
Expand Down

0 comments on commit 8a79834

Please sign in to comment.