cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ArgumentsParser.h
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2019-2024 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 <map>
23#include <string>
24
25#include "CepGen/Utils/Limits.h"
26
27namespace cepgen {
32 public:
36 ArgumentsParser(int argc, char* argv[]);
38 template <typename... Args>
39 inline ArgumentsParser& addArgument(Args&&... args) {
40 params_.emplace_back(std::forward<Args>(args)...);
41 params_.rbegin()->optional = false;
42 return *this;
43 }
45 template <typename... Args>
46 inline ArgumentsParser& addOptionalArgument(Args&&... args) {
47 params_.emplace_back(std::forward<Args>(args)...);
48 params_.rbegin()->optional = true;
49 return *this;
50 }
52 std::string operator[](std::string name) const;
53 void dump() const;
54 void print_help() const;
55 void print_version() const;
56 std::string help_message() const;
57 inline bool debugging() const { return debug_req_; }
59 inline const std::vector<std::string>& extra_config() const { return extra_config_; }
60
61 private:
63 class Parameter {
64 public:
66 Parameter(std::string, std::string = "", std::string* = nullptr, std::string = "");
68 Parameter(std::string name, std::string, unsigned int* = nullptr, unsigned int = 0);
70 Parameter(std::string, std::string, int* = nullptr, int = 0);
72 Parameter(std::string, std::string, bool* = nullptr, bool = false);
74 Parameter(std::string, std::string, double* = nullptr, double = -999.999);
76 Parameter(std::string, std::string, std::vector<std::string>* = nullptr, std::vector<std::string> = {});
78 Parameter(std::string, std::string, std::vector<int>* = nullptr, std::vector<int> = {});
80 Parameter(std::string, std::string, std::vector<double>* = nullptr, std::vector<double> = {});
82 Parameter(std::string, std::string, Limits* = nullptr, Limits = Limits{});
83
84 Parameter& parse();
85 inline bool boolean() const { return bool_variable_ != nullptr; }
86 bool matches(const std::string&) const;
87
88 //----- parameters attributes
89
90 std::vector<std::string> name;
91 std::string description;
92 std::string value;
93 bool optional{true};
94
95 private:
96 //----- parameters containers
97
98 std::string* str_variable_{nullptr};
99 double* float_variable_{nullptr};
100 int* int_variable_{nullptr};
101 unsigned int* uint_variable_{nullptr};
102 bool* bool_variable_{nullptr};
103 Limits* lim_variable_{nullptr};
104 std::vector<std::string>* vec_str_variable_{nullptr};
105 std::vector<int>* vec_int_variable_{nullptr};
106 std::vector<double>* vec_float_variable_{nullptr};
107 };
108 typedef std::vector<Parameter> ParametersCollection;
109
110 std::string command_name_;
111 const ParametersCollection help_str_;
112 const ParametersCollection version_str_;
113 const ParametersCollection config_str_;
114 const ParametersCollection debug_str_;
115 bool help_req_{false}, version_req_{false}, debug_req_{false};
116 ParametersCollection params_;
117 std::vector<std::pair<std::string, std::string> > args_;
118 std::vector<std::string> extra_config_;
119 };
120} // namespace cepgen
121
122#endif
A generic command line arguments parser.
void print_version() const
Show version.
const std::vector< std::string > & extra_config() const
bool debugging() const
Is the debugging flag set? Extra configuration flags in arguments?
ArgumentsParser & addArgument(Args &&... args)
Add a parameter required for the parser.
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.
std::string operator[](std::string name) const
Read required and optional parameters.
Common namespace for this Monte Carlo generator.