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