Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:17

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 // Project include(s)
0012 #include "detray/utils/logging.hpp"
0013 
0014 // Boost
0015 #include "detray/options/boost_program_options.hpp"
0016 
0017 // System include(s).
0018 #include <cstdlib>
0019 #include <iostream>
0020 #include <stdexcept>
0021 #include <string>
0022 
0023 namespace detray::options {
0024 
0025 // Forward declare the options handling for different configuration types
0026 template <typename T>
0027 void add_options(boost::program_options::options_description& /*desc*/,
0028                  const T& /*opt*/);
0029 
0030 template <typename T>
0031 void configure_options(const boost::program_options::variables_map& /*map*/,
0032                        T& /*opt*/);
0033 
0034 /// Parse commandline options and add them to detray configuration types
0035 template <typename... CONFIGS>
0036 auto parse_options(boost::program_options::options_description& desc, int argc,
0037                    char* argv[], CONFIGS&... cfgs) {
0038   static_assert(sizeof...(CONFIGS) > 0, "No commandline options configured");
0039 
0040   if (!argv) {
0041     throw std::invalid_argument("Invalid command line arguments passed");
0042   }
0043 
0044   desc.add_options()("help", "Produce help message");
0045 
0046   // Add options according to the configurations that were passed
0047   (add_options(desc, cfgs), ...);
0048 
0049   // Parse options
0050   boost::program_options::variables_map vm;
0051   try {
0052     boost::program_options::store(
0053         parse_command_line(
0054             argc, argv, desc,
0055             boost::program_options::command_line_style::unix_style ^
0056                 boost::program_options::command_line_style::allow_short),
0057         vm);
0058 
0059     boost::program_options::notify(vm);
0060   } catch (const std::exception& ex) {
0061     // Print help message in case of error
0062     DETRAY_FATAL_HOST(ex.what() << "\n" << desc);
0063     std::exit(EXIT_FAILURE);
0064   }
0065 
0066   // Print help message when requested
0067   if (vm.count("help") != 0u) {
0068     std::clog << desc << std::endl;
0069     std::exit(EXIT_SUCCESS);
0070   }
0071 
0072   // Add the options to the configurations
0073   (configure_options(vm, cfgs), ...);
0074   // Make sure everything is configured correctly
0075   (print_options(cfgs), ...);
0076 
0077   return vm;
0078 }
0079 
0080 /// Parse commandline options and add them to detray configuration types
0081 template <typename... CONFIGS>
0082 auto parse_options(const std::string& description, int argc, char* argv[],
0083                    CONFIGS&... cfgs) {
0084   // Options description
0085   boost::program_options::options_description desc(description);
0086 
0087   // Run options parsing
0088   return parse_options(desc, argc, argv, cfgs...);
0089 }
0090 
0091 }  // namespace detray::options