File indexing completed on 2026-05-27 07:24:17
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011
0012 #include "detray/utils/logging.hpp"
0013
0014
0015 #include "detray/options/boost_program_options.hpp"
0016
0017
0018 #include <cstdlib>
0019 #include <iostream>
0020 #include <stdexcept>
0021 #include <string>
0022
0023 namespace detray::options {
0024
0025
0026 template <typename T>
0027 void add_options(boost::program_options::options_description& ,
0028 const T& );
0029
0030 template <typename T>
0031 void configure_options(const boost::program_options::variables_map& ,
0032 T& );
0033
0034
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
0047 (add_options(desc, cfgs), ...);
0048
0049
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
0062 DETRAY_FATAL_HOST(ex.what() << "\n" << desc);
0063 std::exit(EXIT_FAILURE);
0064 }
0065
0066
0067 if (vm.count("help") != 0u) {
0068 std::clog << desc << std::endl;
0069 std::exit(EXIT_SUCCESS);
0070 }
0071
0072
0073 (configure_options(vm, cfgs), ...);
0074
0075 (print_options(cfgs), ...);
0076
0077 return vm;
0078 }
0079
0080
0081 template <typename... CONFIGS>
0082 auto parse_options(const std::string& description, int argc, char* argv[],
0083 CONFIGS&... cfgs) {
0084
0085 boost::program_options::options_description desc(description);
0086
0087
0088 return parse_options(desc, argc, argv, cfgs...);
0089 }
0090
0091 }