cepgen is hosted by Hepforge, IPPP Durham
CepGen N/A
Central exclusive processes event generator
ArgumentsParser.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2019-2025 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CepGen_Utils_ArgumentsParser_h
20#define CepGen_Utils_ArgumentsParser_h
21
22#include <string>
23
24#include "CepGen/Utils/Limits.h"
25
26namespace cepgen {
31 public:
35 explicit ArgumentsParser(int argc, char* argv[]);
37 template <typename... Args>
38 inline ArgumentsParser& addArgument(Args&&... args) {
39 params_.emplace_back(std::forward<Args>(args)...);
40 params_.rbegin()->optional = false;
41 return *this;
42 }
44 template <typename... Args>
45 inline ArgumentsParser& addOptionalArgument(Args&&... args) {
46 params_.emplace_back(std::forward<Args>(args)...);
47 params_.rbegin()->optional = true;
48 return *this;
49 }
51 std::string operator[](const std::string& name) const;
52 void dump() const;
53 void print_help() const;
54 static void print_version();
55 std::string help_message() const;
56 inline bool debugging() const { return debug_req_; }
57 inline const std::vector<std::string>& extra_config() const { return extra_config_; }
58
59 private:
61 class Parameter {
62 public:
63 explicit Parameter(const std::string&,
64 const std::string& = "",
65 std::string* = nullptr,
66 const std::string& = "");
67 explicit Parameter(const std::string&,
68 const std::string&,
69 unsigned int* = nullptr,
70 const unsigned int& = 0);
71 explicit Parameter(const std::string&,
72 const std::string&,
73 int* = nullptr,
74 const int& = 0);
75 explicit Parameter(const std::string&,
76 const std::string&,
77 bool* = nullptr,
78 const bool& = false);
79 explicit Parameter(const std::string&,
80 const std::string&,
81 double* = nullptr,
82 const double& = -999.999);
83 explicit Parameter(const std::string&,
84 const std::string&,
85 std::vector<std::string>* = nullptr,
86 const std::vector<std::string>& = {});
87 explicit Parameter(const std::string&,
88 const std::string&,
89 std::vector<int>* = nullptr,
90 const std::vector<int>& = {});
91 explicit Parameter(const std::string&,
92 const std::string&,
93 std::vector<double>* = nullptr,
94 const std::vector<double>& = {});
95 explicit Parameter(const std::string&,
96 const std::string&,
97 Limits* = nullptr,
98 const Limits& = Limits{});
99
100 Parameter& parse();
101 inline bool boolean() const { return bool_variable_ != nullptr; }
102 bool matches(const std::string&) const;
103
104 // parameters attributes
105 std::vector<std::string> name;
106 std::string description;
107 std::string value;
108 bool optional{true};
109
110 private:
111 // parameters containers
112 std::string* str_variable_{nullptr};
113 double* float_variable_{nullptr};
114 int* int_variable_{nullptr};
115 unsigned int* uint_variable_{nullptr};
116 bool* bool_variable_{nullptr};
117 Limits* lim_variable_{nullptr};
118 std::vector<std::string>* vec_str_variable_{nullptr};
119 std::vector<int>* vec_int_variable_{nullptr};
120 std::vector<double>* vec_float_variable_{nullptr};
121 };
122 using ParametersCollection = std::vector<Parameter>;
123
124 const std::string command_name_;
125 bool help_req_{false}, version_req_{false}, debug_req_{false};
126 ParametersCollection params_;
127 std::vector<std::pair<std::string, std::string> > args_;
128 std::vector<std::string> add_ons_;
129 std::vector<std::string> extra_config_;
130 };
131} // namespace cepgen
132
133#endif
A generic command line arguments parser.
const std::vector< std::string > & extra_config() const
Extra config flags.
bool debugging() const
Is the debugging flag set?
ArgumentsParser(int argc, char *argv[])
Arguments parser constructor.
ArgumentsParser & addArgument(Args &&... args)
Add a parameter required for the parser.
std::string operator[](const std::string &name) const
Read required and optional parameters.
static void print_version()
Show version.
ArgumentsParser & parse()
Associate command-line arguments to parameters.
void print_help() const
Show usage.
std::string help_message() const
Usage message.
ArgumentsParser & addOptionalArgument(Args &&... args)
Add a non-mandatory parameters that can be parsed.
void dump() const
Dump the list of arguments into the terminal.
Common namespace for this Monte Carlo generator.
Definition Handler.h:26