ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
parse_options.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
12#include "detray/utils/logging.hpp"
13
14// Boost
15#include "detray/options/boost_program_options.hpp"
16
17// System include(s).
18#include <cstdlib>
19#include <iostream>
20#include <stdexcept>
21#include <string>
22
23namespace detray::options {
24
25// Forward declare the options handling for different configuration types
26template <typename T>
27void add_options(boost::program_options::options_description& /*desc*/,
28 const T& /*opt*/);
29
30template <typename T>
31void configure_options(const boost::program_options::variables_map& /*map*/,
32 T& /*opt*/);
33
35template <typename... CONFIGS>
36auto parse_options(boost::program_options::options_description& desc, int argc,
37 char* argv[], CONFIGS&... cfgs) {
38 static_assert(sizeof...(CONFIGS) > 0, "No commandline options configured");
39
40 if (!argv) {
41 throw std::invalid_argument("Invalid command line arguments passed");
42 }
43
44 desc.add_options()("help", "Produce help message");
45
46 // Add options according to the configurations that were passed
47 (add_options(desc, cfgs), ...);
48
49 // Parse options
50 boost::program_options::variables_map vm;
51 try {
52 boost::program_options::store(
53 parse_command_line(
54 argc, argv, desc,
55 boost::program_options::command_line_style::unix_style ^
56 boost::program_options::command_line_style::allow_short),
57 vm);
58
59 boost::program_options::notify(vm);
60 } catch (const std::exception& ex) {
61 // Print help message in case of error
62 DETRAY_FATAL_HOST(ex.what() << "\n" << desc);
63 std::exit(EXIT_FAILURE);
64 }
65
66 // Print help message when requested
67 if (vm.count("help") != 0u) {
68 std::clog << desc << std::endl;
69 std::exit(EXIT_SUCCESS);
70 }
71
72 // Add the options to the configurations
73 (configure_options(vm, cfgs), ...);
74 // Make sure everything is configured correctly
75 (print_options(cfgs), ...);
76
77 return vm;
78}
79
81template <typename... CONFIGS>
82auto parse_options(const std::string& description, int argc, char* argv[],
83 CONFIGS&... cfgs) {
84 // Options description
85 boost::program_options::options_description desc(description);
86
87 // Run options parsing
88 return parse_options(desc, argc, argv, cfgs...);
89}
90
91} // namespace detray::options