Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file OptionsIO.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date Mon, 17 Dec 2001
18 : ///
19 : // Helper for parsing command line arguments and reading configuration files
20 : /****************************************************************************/
21 : #pragma once
22 : #include <config.h>
23 :
24 : #include <vector>
25 : #include <string>
26 : #include <chrono>
27 :
28 :
29 : // ===========================================================================
30 : // class declarations
31 : // ===========================================================================
32 : class OptionsCont;
33 :
34 :
35 : // ===========================================================================
36 : // class definitions
37 : // ===========================================================================
38 : /**
39 : * @class OptionsIO
40 : *
41 : * Helping methods for parsing of command line arguments and reading a
42 : * configuration file.
43 : * Any errors are reported by throwing a ProcessError exception which
44 : * contains a description about the failure.
45 : */
46 : class OptionsIO {
47 : public:
48 : /** @brief Stores the command line arguments for later parsing
49 : *
50 : * @param[in] argc number of arguments given at the command line
51 : * @param[in] argv arguments given at the command line
52 : */
53 : static void setArgs(int argc, char** argv);
54 :
55 : /** @brief Stores faked command line arguments for later parsing
56 : *
57 : * @param[in] args arguments given as substitute for the command line
58 : */
59 : static void setArgs(const std::vector<std::string>& args);
60 :
61 : /** @brief Return the number of command line arguments
62 : */
63 : static int getArgC() {
64 36247 : return (int)myArgs.size();
65 : }
66 :
67 :
68 : /** @brief Parses the command line arguments and loads the configuration
69 : *
70 : * Command line arguments are parsed, first, throwing a ProcessError
71 : * if something fails. Then options are reset to being writeable and the
72 : * configuration is loaded using "loadConfiguration". After this,
73 : * the options are reset again and the command line arguments are
74 : * reparsed.
75 : *
76 : * This workflow allows to read the name of a configuration file from
77 : * command line arguments, first, then to load values from this configuration
78 : * file and reset them by other values from the command line.
79 : */
80 : static void getOptions(const bool commandLineOnly = false);
81 :
82 :
83 : /** @brief Loads and parses the configuration
84 : *
85 : * The name of the configuration file is extracted from the global
86 : * OptionsCont ("configuration-file" is used as the name of the option to get
87 : * the name of the configuration).
88 : */
89 : static void loadConfiguration();
90 :
91 :
92 : /** @brief Retrieves the XML root element of a supposed configuration or net
93 : *
94 : * @param[in] filename the XML file to parse
95 : * @return the root element if any
96 : */
97 : static std::string getRoot(const std::string& filename);
98 :
99 : /** @brief Return the time stamp of the last init
100 : */
101 : static const std::chrono::time_point<std::chrono::system_clock>& getLoadTime() {
102 : return myLoadTime;
103 : }
104 :
105 :
106 : private:
107 : static std::vector<std::string> myArgs;
108 : static std::chrono::time_point<std::chrono::system_clock> myLoadTime;
109 :
110 : };
|