Mercurial > hg > orthanc
changeset 7052:f9e92fe0c493
using boost::program_options in the Orthanc executable
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Tue, 11 Aug 2026 18:57:05 +0200 |
| parents | 4f42a9f8e078 |
| children | 29f895d70f3e |
| files | OrthancServer/CMakeLists.txt OrthancServer/Resources/AdvancedConfiguration.json OrthancServer/Sources/main.cpp |
| diffstat | 3 files changed, 334 insertions(+), 186 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancServer/CMakeLists.txt Tue Aug 11 17:15:23 2026 +0200 +++ b/OrthancServer/CMakeLists.txt Tue Aug 11 18:57:05 2026 +0200 @@ -32,6 +32,7 @@ include(${CMAKE_SOURCE_DIR}/../OrthancFramework/Resources/CMake/OrthancFrameworkParameters.cmake) # Enable all the optional components of the Orthanc framework +set(ENABLE_BOOST_PROGRAM_OPTIONS ON) set(ENABLE_CRYPTO_OPTIONS ON) set(ENABLE_DCMTK ON) set(ENABLE_DCMTK_NETWORKING ON) @@ -261,6 +262,7 @@ set(ORTHANC_EMBEDDED_FILES CONFIGURATION_SAMPLE ${CMAKE_SOURCE_DIR}/Resources/Configuration.json + ADVANCED_CONFIGURATION_SAMPLE ${CMAKE_SOURCE_DIR}/Resources/AdvancedConfiguration.json DICOM_CONFORMANCE_STATEMENT ${CMAKE_SOURCE_DIR}/Resources/DicomConformanceStatement.txt FONT_UBUNTU_MONO_BOLD_16 ${CMAKE_SOURCE_DIR}/Resources/Fonts/UbuntuMonoBold-16.json LUA_TOOLBOX ${CMAKE_SOURCE_DIR}/Resources/Toolbox.lua
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancServer/Resources/AdvancedConfiguration.json Tue Aug 11 18:57:05 2026 +0200 @@ -0,0 +1,9 @@ +{ + /** + * Advanced configuration options of Orthanc + * + * This file gathers advanced options that were part of the main + * configuration file until Orthanc 1.12.11. + **/ + +}
--- a/OrthancServer/Sources/main.cpp Tue Aug 11 17:15:23 2026 +0200 +++ b/OrthancServer/Sources/main.cpp Tue Aug 11 18:57:05 2026 +0200 @@ -56,6 +56,7 @@ #include "StorageCommitmentReports.h" #include <boost/algorithm/string/predicate.hpp> +#include <boost/program_options.hpp> #if defined(_WIN32) || defined(__CYGWIN__) #include <windows.h> @@ -725,13 +726,14 @@ }; -static void PrintHelp(const boost::filesystem::path& path) +static void PrintHelp(const boost::filesystem::path& path, + const boost::program_options::options_description& all) { std::cout << "Usage: " << SystemToolbox::PathToUtf8(path) << " [OPTION]... [CONFIGURATION]" << std::endl << "Orthanc, lightweight, RESTful DICOM server for healthcare and medical research." << std::endl << std::endl - << "The \"CONFIGURATION\" argument can be a single file or a directory. In the " << std::endl + << "The \"CONFIGURATION\" argument can be a list of files or directories. In the " << std::endl << "case of a directory, all the JSON files it contains will be merged. " << std::endl << "If no configuration path is given on the command line, a set of default " << std::endl << "parameters is used. Please refer to the Orthanc Book for the full " << std::endl @@ -742,40 +744,9 @@ << "reset the value of other log-related options that were read before." << std::endl << std::endl << "The recommended set of options to debug DICOM communications is " << std::endl - << "\"--verbose --trace-dicom --logfile=dicom.log\"" << std::endl - << std::endl - << "Command-line options:" << std::endl - << " --help\t\tdisplay this help and exit" << std::endl - << " --logdir=[dir]\tdirectory where to store the log files" << std::endl - << "\t\t\t(by default, the log is dumped to stderr)" << std::endl - << " --logfile=[file]\tfile where to store the log of Orthanc" << std::endl - << "\t\t\t(by default, the log is dumped to stderr)" << std::endl - << " --config=[file]\tcreate a sample configuration file and exit" << std::endl - << "\t\t\t(if \"file\" is \"-\", dumps to stdout)" << std::endl - << " --errors\t\tprint the supported error codes and exit" << std::endl - << " --verbose\t\tbe verbose in logs" << std::endl - << " --trace\t\thighest verbosity in logs (for debug)" << std::endl - << " --upgrade\t\tallow Orthanc to upgrade the version of the" << std::endl - << "\t\t\tdatabase (beware that the database will become" << std::endl - << "\t\t\tincompatible with former versions of Orthanc)" << std::endl - << " --no-jobs\t\tdon't restart the jobs that were stored during" << std::endl - << "\t\t\tthe last execution of Orthanc" << std::endl - << " --openapi=[file]\twrite the OpenAPI documentation and exit" << std::endl - << "\t\t\t(if \"file\" is \"-\", dumps to stdout)" << std::endl - << " --cheatsheet=[file]\twrite the cheat sheet of REST API as CSV" << std::endl - << "\t\t\tand exit (if \"file\" is \"-\", dumps to stdout)" << std::endl - << " --version\t\toutput version information and exit" << std::endl - << std::endl - << "Fine-tuning of log categories:" << std::endl; + << "\"--verbose --trace-dicom --logfile=dicom.log\"" << std::endl; - for (size_t i = 0; i < Logging::GetCategoriesCount(); i++) - { - const std::string name = Logging::GetCategoryName(i); - std::cout << " --verbose-" << name - << "\tbe verbose in logs of category \"" << name << "\"" << std::endl; - std::cout << " --trace-" << name - << "\tuse highest verbosity for logs of category \"" << name << "\"" << std::endl; - } + std::cout << all; std::cout << std::endl @@ -1727,7 +1698,13 @@ if (context.IsReadOnly()) { - LOG(WARNING) << "READ-ONLY SYSTEM: ignoring these configurations: " << ORTHANC_CONFIG_STORAGE_COMPRESSION << ", " << ORTHANC_CONFIG_STORE_MD5_FOR_ATTACHMENTS << ", " << ORTHANC_CONFIG_OVERWRITE_INSTANCES << ", " << ORTHANC_CONFIG_MAXIMUM_PATIENT_COUNT << ", " << ORTHANC_CONFIG_MAXIMUM_STORAGE_SIZE <<", " << ORTHANC_CONFIG_MAXIMUM_STORAGE_MODE << ", SaveJobs"; + LOG(WARNING) << "READ-ONLY SYSTEM: ignoring these configurations: " + << ORTHANC_CONFIG_STORAGE_COMPRESSION << ", " + << ORTHANC_CONFIG_STORE_MD5_FOR_ATTACHMENTS << ", " + << ORTHANC_CONFIG_OVERWRITE_INSTANCES << ", " + << ORTHANC_CONFIG_MAXIMUM_PATIENT_COUNT << ", " + << ORTHANC_CONFIG_MAXIMUM_STORAGE_SIZE << ", " + << ORTHANC_CONFIG_MAXIMUM_STORAGE_MODE << ", SaveJobs"; } else { @@ -1746,7 +1723,9 @@ overwriteInstancesMode = StringToOverwriteInstancesMode(strOverwriteInstancesMode); if (overwriteInstancesMode == OverwriteInstancesMode_IfChanged && !context.IsStoreMD5ForAttachments()) { - LOG(ERROR) << "Can not set \"" << ORTHANC_CONFIG_OVERWRITE_INSTANCES << "\" to \"IfChanged\" when \"" << ORTHANC_CONFIG_STORE_MD5_FOR_ATTACHMENTS << "\" is set to false."; + LOG(ERROR) << "Can not set \"" << ORTHANC_CONFIG_OVERWRITE_INSTANCES + << "\" to \"IfChanged\" when \"" << ORTHANC_CONFIG_STORE_MD5_FOR_ATTACHMENTS + << "\" is set to false."; return false; } } @@ -1998,7 +1977,7 @@ } -static void ExportOpenApi(const std::string& target) +static int ExportOpenApi(const std::string& target) { Json::Value openapi; @@ -2029,18 +2008,28 @@ std::string s; Toolbox::WriteStyledJson(s, openapi); - if (target == "-") + try { - std::cout << s; // Print to stdout + if (target == "-") + { + std::cout << s; // Print to stdout + } + else + { + SystemToolbox::WriteFile(s, SystemToolbox::PathFromUtf8(target)); + } + + return 0; } - else + catch (OrthancException&) { - SystemToolbox::WriteFile(s, SystemToolbox::PathFromUtf8(target)); + LOG(ERROR) << "Cannot export OpenAPI documentation as file \"" << target << "\""; + return -1; } } -static void ExportCheatSheet(const std::string& target) +static int ExportCheatSheet(const std::string& target) { std::string cheatsheet; @@ -2054,13 +2043,56 @@ context.Stop(); } - if (target == "-") + try { - std::cout << cheatsheet; // Print to stdout + if (target == "-") + { + std::cout << cheatsheet; // Print to stdout + } + else + { + SystemToolbox::WriteFile(cheatsheet, SystemToolbox::PathFromUtf8(target)); + } + + return 0; + } + catch (OrthancException&) + { + LOG(ERROR) << "Cannot export REST cheat sheet as file \"" << target << "\""; + return -1; } - else +} + + +static int ExportResource(const std::string& target, + Orthanc::ServerResources::FileResourceId resource) +{ + try { - SystemToolbox::WriteFile(cheatsheet, SystemToolbox::PathFromUtf8(target)); + std::string content; + GetFileResource(content, resource); + +#if defined(_WIN32) + // Replace UNIX newlines with DOS newlines + boost::replace_all(content, "\n", "\r\n"); +#endif + + if (target == "-") + { + // New in 1.5.8: Print to stdout + std::cout << content; + } + else + { + SystemToolbox::WriteFile(content, SystemToolbox::PathFromUtf8(target)); + } + + return 0; + } + catch (OrthancException&) + { + LOG(ERROR) << "Cannot write to file " << SystemToolbox::PathFromUtf8(target) << ", aborting."; + return -1; } } @@ -2104,171 +2136,276 @@ /** * Parse the command-line options. - **/ + **/ - for (size_t i = 1; i < arguments.size(); i++) - { - const std::string& argument = arguments[i]; + static const char* const OPTION_HELP = "help"; + static const char* const OPTION_VERSION = "version"; + static const char* const OPTION_CONFIG = "config"; + static const char* const OPTION_ADVANCED_CONFIG = "advanced-config"; + static const char* const OPTION_ERRORS = "errors"; + static const char* const OPTION_UPGRADE = "upgrade"; + static const char* const OPTION_NO_JOBS = "no-jobs"; + static const char* const OPTION_OPENAPI = "openapi"; + static const char* const OPTION_CHEATSHEET = "cheatsheet"; + static const char* const OPTION_INPUTS = "inputs"; + + static const char* const OPTION_LOGDIR = "logdir"; + static const char* const OPTION_LOGFILE = "logfile"; + static const char* const OPTION_VERBOSE = "verbose"; + static const char* const OPTION_TRACE = "trace"; + + boost::program_options::options_description allWithoutHidden; + std::vector<std::string> orderSensitiveArguments; + std::vector<std::string> unrecognized; + boost::program_options::variables_map options; - if (argument.empty()) + { + boost::program_options::options_description generic("Generic options"); + generic.add_options() + (OPTION_HELP, "display this help and exit") + (OPTION_VERSION, "output version information and exit") + (OPTION_CONFIG, boost::program_options::value<std::string>()->value_name("file"), + "create a sample configuration file and exit (if \"file\" is \"-\", dumps to stdout)") + (OPTION_ADVANCED_CONFIG, boost::program_options::value<std::string>()->value_name("file"), + "create a file containing the advanced configuration options and exit (if \"file\" is \"-\", dumps to stdout)") + (OPTION_ERRORS, "print the supported error codes and exit") + (OPTION_UPGRADE, "allow Orthanc to upgrade the version of the database " + "(beware that the database will become incompatible with former versions of Orthanc)") + (OPTION_NO_JOBS, "don't restart the jobs that were stored during the last execution of Orthanc") + (OPTION_OPENAPI, boost::program_options::value<std::string>()->value_name("file"), + "write the OpenAPI documentation and exit (if \"file\" is \"-\", dumps to stdout)") + (OPTION_CHEATSHEET, boost::program_options::value<std::string>()->value_name("file"), + "write the cheat sheet of REST API as CSV and exit (if \"file\" is \"-\", dumps to stdout)"); + + boost::program_options::options_description logging("Logging options"); + logging.add_options() + (OPTION_LOGDIR, boost::program_options::value<std::string>()->value_name("dir"), + "directory where to store the log files (by default, the log is dumped to stderr)") + (OPTION_LOGFILE, boost::program_options::value<std::string>()->value_name("file"), + "file where to store the log of Orthanc (by default, the log is dumped to stderr)") + (OPTION_VERBOSE, "be verbose in logs") + (OPTION_TRACE, "highest verbosity in logs (for debug)"); + + boost::program_options::options_description finetuning("Fine-tuning of log categories"); + + std::set<std::string> orderSensitiveOptions; + orderSensitiveOptions.insert(OPTION_VERBOSE); + orderSensitiveOptions.insert(OPTION_TRACE); + + for (size_t i = 0; i < Logging::GetCategoriesCount(); i++) { - // Ignore empty arguments + const std::string name = Logging::GetCategoryName(i); + const std::string verboseName = "verbose-" + name; + const std::string verboseDescription = "be verbose in logs of category \"" + name + "\""; + const std::string traceName = "trace-" + name; + const std::string traceDescription = "use highest verbosity for logs of category \"" + name + "\""; + + finetuning.add_options() (verboseName.c_str(), verboseDescription.c_str()); + finetuning.add_options() (traceName.c_str(), traceDescription.c_str()); + + orderSensitiveOptions.insert(verboseName); + orderSensitiveOptions.insert(traceName); } - else if (argument[0] != '-') - { - boost::filesystem::path configurationFile = SystemToolbox::PathFromUtf8(argument); + + allWithoutHidden.add(generic); + allWithoutHidden.add(logging); + allWithoutHidden.add(finetuning); -// // TODO WHAT IS THE ENCODING? -//#if defined(_WIN32) -// //configurationFileUtf8Str = SystemToolbox::WStringToUtf8(SystemToolbox::WStringFromCharPtr(argv[i])); -//#else -// configurationFileUtf8Str = std::string(argv[i]); -//#endif + boost::program_options::options_description hidden; + hidden.add_options() + (OPTION_INPUTS, boost::program_options::value< std::vector<std::string> >(), + "Input configuration files"); + + boost::program_options::options_description all; + all.add(allWithoutHidden); + all.add(hidden); + + boost::program_options::positional_options_description positional; + positional.add(OPTION_INPUTS, -1); // -1 = all remaining positional arguments - configurationPaths.push_back(configurationFile); + if (arguments.empty()) + { + LOG(ERROR) << "The program name is not available, aborting"; + return -1; } - else if (argument == "--errors") + + std::vector<std::string> orderNonSensitiveArguments; + orderSensitiveArguments.reserve(arguments.size()); + orderNonSensitiveArguments.reserve(arguments.size()); + + for (size_t i = 1 /* skip the program name */; i < arguments.size(); i++) { - PrintErrors(SystemToolbox::PathFromUtf8(arguments[0])); - return 0; + bool isOrderSensitive = false; + + if (boost::starts_with(arguments[i], "--")) + { + const std::string key = arguments[i].substr(2); + + if (orderSensitiveOptions.find(key) != orderSensitiveOptions.end()) + { + isOrderSensitive = true; + orderSensitiveArguments.push_back(key); + } + } + + if (!isOrderSensitive) + { + orderNonSensitiveArguments.push_back(arguments[i]); + } } - else if (argument == "--help") + + try { - PrintHelp(SystemToolbox::PathFromUtf8(arguments[0])); - return 0; + boost::program_options::parsed_options parsed = boost::program_options::command_line_parser(orderNonSensitiveArguments) + .options(all) + .positional(positional) + .allow_unregistered() // don't throw on unknown options + .run(); + + // Pull out everything that didn't match a declared option + unrecognized = boost::program_options::collect_unrecognized(parsed.options, boost::program_options::exclude_positional); + + boost::program_options::store(parsed, options); + boost::program_options::notify(options); } - else if (argument == "--version") + catch (boost::program_options::error& e) { - PrintVersion(SystemToolbox::PathFromUtf8(arguments[0])); - return 0; + LOG(ERROR) << "Error while parsing the command-line arguments: " << e.what(); + return -1; } - else if (argument == "--verbose") + } + + + /** + * Process the command-line options. + **/ + + for (size_t i = 0; i < unrecognized.size(); i++) + { + LOG(WARNING) << "Option unsupported by the core of Orthanc: " << unrecognized[i]; + } + + for (size_t i = 0; i < orderSensitiveArguments.size(); i++) + { + const std::string& arg = orderSensitiveArguments[i]; + + if (arg == OPTION_VERBOSE) { SetGlobalVerbosity(Verbosity_Verbose); } - else if (argument == "--logs-no-thread") - { - Logging::SetThreadNamesEnabled(false); - } - else if (argument == "--trace") + else if (arg == OPTION_TRACE) { SetGlobalVerbosity(Verbosity_Trace); } - else if (boost::starts_with(argument, "--verbose-") && - SetCategoryVerbosity(Verbosity_Verbose, argument.substr(10))) - { - // New in Orthanc 1.8.1 - } - else if (boost::starts_with(argument, "--trace-") && - SetCategoryVerbosity(Verbosity_Trace, argument.substr(8))) + else if (boost::starts_with(arg, "verbose-") && + SetCategoryVerbosity(Verbosity_Verbose, arg.substr(arg.find('-') + 1))) { // New in Orthanc 1.8.1 } - else if (boost::starts_with(argument, "--logdir=")) - { - // TODO WHAT IS THE ENCODING? - const std::string directory = argument.substr(9); - - try - { - Logging::SetTargetFolder(directory); - } - catch (OrthancException&) - { - LOG(ERROR) << "The directory where to store the log files (" - << directory << ") is inexistent, aborting."; - return -1; - } - } - else if (boost::starts_with(argument, "--logfile=")) - { - // TODO WHAT IS THE ENCODING? - const std::string file = argument.substr(10); - - try - { - Logging::SetTargetFile(file); - } - catch (OrthancException&) - { - LOG(ERROR) << "Cannot write to the specified log file (" - << file << "), aborting."; - return -1; - } - } - else if (argument == "--upgrade") - { - upgradeDatabase = true; - } - else if (argument == "--no-jobs") - { - loadJobsFromDatabase = false; - } - else if (boost::starts_with(argument, "--config=")) + else if (boost::starts_with(arg, "trace-") && + SetCategoryVerbosity(Verbosity_Trace, arg.substr(arg.find('-') + 1))) { - // TODO WHAT IS THE ENCODING? - std::string configurationSample; - GetFileResource(configurationSample, ServerResources::CONFIGURATION_SAMPLE); - -#if defined(_WIN32) - // Replace UNIX newlines with DOS newlines - boost::replace_all(configurationSample, "\n", "\r\n"); -#endif - - std::string target = argument.substr(9); - - try - { - if (target == "-") - { - // New in 1.5.8: Print to stdout - std::cout << configurationSample; - } - else - { - SystemToolbox::WriteFile(configurationSample, SystemToolbox::PathFromUtf8(target)); - } - return 0; - } - catch (OrthancException&) - { - LOG(ERROR) << "Cannot write sample configuration as file \"" << target << "\""; - return -1; - } - } - else if (boost::starts_with(argument, "--openapi=")) - { - const std::string target = argument.substr(argument.find('=') + 1); - - try - { - ExportOpenApi(target); - return 0; - } - catch (OrthancException&) - { - LOG(ERROR) << "Cannot export OpenAPI documentation as file \"" << target << "\""; - return -1; - } - } - else if (boost::starts_with(argument, "--cheatsheet=")) - { - std::string target = argument.substr(13); - - try - { - ExportCheatSheet(target); - return 0; - } - catch (OrthancException&) - { - LOG(ERROR) << "Cannot export REST cheat sheet as file \"" << target << "\""; - return -1; - } + // New in Orthanc 1.8.1 } else { - LOG(WARNING) << "Option unsupported by the core of Orthanc: " << argument; + // Should never happen + LOG(ERROR) << "Unsupported command-line option: " << arg; + return -1; + } + } + + if (options.count(OPTION_HELP) == 1) + { + PrintHelp(arguments[0], allWithoutHidden); + return 0; + } + + if (options.count(OPTION_VERSION) == 1) + { + PrintVersion(arguments[0]); + return 0; + } + + if (options.count(OPTION_LOGDIR) == 1) + { + const std::string directory = options[OPTION_LOGDIR].as<std::string>(); + + try + { + Logging::SetTargetFolder(directory); + } + catch (OrthancException&) + { + LOG(ERROR) << "The directory where to store the log files (" + << SystemToolbox::PathFromUtf8(directory) << ") is inexistent, aborting."; + return -1; + } + } + + if (options.count(OPTION_LOGFILE) == 1) + { + const std::string file = options[OPTION_LOGFILE].as<std::string>(); + + try + { + Logging::SetTargetFile(file); + } + catch (OrthancException&) + { + LOG(ERROR) << "Cannot write to the specified log file (" + << SystemToolbox::PathFromUtf8(file) << "), aborting."; + return -1; + } + } + + if (options.count(OPTION_CONFIG) == 1) + { + const std::string file = options[OPTION_CONFIG].as<std::string>(); + return ExportResource(file, Orthanc::ServerResources::CONFIGURATION_SAMPLE); + } + + if (options.count(OPTION_ADVANCED_CONFIG) == 1) + { + const std::string file = options[OPTION_ADVANCED_CONFIG].as<std::string>(); + return ExportResource(file, Orthanc::ServerResources::ADVANCED_CONFIGURATION_SAMPLE); + } + + if (options.count(OPTION_ERRORS) == 1) + { + PrintErrors(arguments[0]); + return 0; + } + + if (options.count(OPTION_UPGRADE) == 1) + { + upgradeDatabase = true; + } + + if (options.count(OPTION_NO_JOBS) == 1) + { + loadJobsFromDatabase = false; + } + + if (options.count(OPTION_OPENAPI) == 1) + { + const std::string file = options[OPTION_OPENAPI].as<std::string>(); + return ExportOpenApi(file); + } + + if (options.count(OPTION_CHEATSHEET) == 1) + { + const std::string file = options[OPTION_CHEATSHEET].as<std::string>(); + return ExportCheatSheet(file); + } + + if (options.count(OPTION_INPUTS) != 0) + { + const std::vector<std::string>& inputs = options[OPTION_INPUTS].as<std::vector<std::string> >(); + + for (size_t i = 0; i < inputs.size(); i++) + { + configurationPaths.push_back(SystemToolbox::PathFromUtf8(inputs[i])); } }
