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    OptionsParser.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Jakob Erdmann
13 /// @author  Michael Behrisch
14 /// @date    Mon, 17 Dec 2001
15 /// @version $Id$
16 ///
17 // Parses command line arguments
18 /****************************************************************************/
19 #ifndef OptionsParser_h
20 #define OptionsParser_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 
29 // ===========================================================================
30 // class declarations
31 // ===========================================================================
32 class OptionsCont;
33 
34 
35 // ===========================================================================
36 // class definitions
37 // ===========================================================================
38 /**
39  * @class OptionsParser
40  * @brief Parses command line arguments
41  *
42  * The only public method parses the given list of arguments. It returns false
43  *  when something failed. This may happen if the syntax of the arguments is
44  *  invalid, a value is tried to be set several times or an unknown option
45  *  is tried to be set.
46  *
47  * The class assumes all options are unset or using default values only.
48  */
49 class OptionsParser {
50 public:
51     /** @brief Parses the given command line arguments
52      *
53      * @param[in] oc The options container to fill
54      * @param[in] argc The number of given command line arguments
55      * @param[in] argv The command line arguments
56      * @return Whether the parsing was successfull
57      * @exception InvalidArgument If a performed setting of an option failed (see Option::set)
58      */
59     static bool parse(int argc, char** argv);
60 
61 private:
62     /** @brief parses the previous arguments
63      *
64      * @param[in] arg1 The first token to parse
65      * @param[in] arg2 The second token to parse, 0 if there is none
66      * @param[in, out] ok Whether the parsing was successfull
67      * @return Number of read tokens (1 or 2)
68      * @exception InvalidArgument If a performed setting of an option failed (see Option::set)
69      */
70     static int check(const char* arg1, const char* arg2, bool& ok);
71 
72 
73     /** @brief Returns the whether the given token is an option
74      *
75      * The given token is assumed to be an option if it starts with a '-'.
76      *
77      * @param[in] arg1 The token to check
78      * @return Whether the token is an option
79      */
80     static bool checkParameter(const char* arg1);
81 
82 
83     /** @brief returns the whether the given token is an abbreviation
84      *
85      * The given token is assumed to be an option if it starts with two '-'.
86      *
87      * @param[in] arg1 The token to check
88      * @return Whether the token is an abbreviation
89      */
90     static bool isAbbreviation(const char* arg1);
91 
92 
93     /** @brief Converts char* to string
94      *
95      * @param[in] arg The c-string to convert
96      * @return The string converted into a std::string
97      */
98     static std::string convert(const char* arg);
99 
100 
101     /** @brief converts char to string
102      *
103      * @param[in] abbr The char to convert
104      * @return The char converted into a std::string
105      */
106     static std::string convert(char abbr);
107 
108 
109     /** @brief Extracts the parameter directly attached to an option
110      *
111      * Parses single tokens which contain an option and the parameter
112      *  (like -c=myconfig.cfg)
113      *
114      * @param[in] oc The container to store the result into
115      * @param[in] arg The token to parse
116      * @exception InvalidArgument If a performed setting of an option failed (see Option::set)
117      */
118     static bool processNonBooleanSingleSwitch(OptionsCont& oc, const char* arg);
119 
120 
121 };
122 
123 
124 #endif
125 
126 /****************************************************************************/
127 
128