Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/CommandLine.cc @ 1690

Last change on this file since 1690 was 1690, checked in by rgrieder, 16 years ago

Added support to specify the command line arguments in start.ini (uses the same formatting). '#' is the comment character.

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "CommandLine.h"
30
31#include "util/String.h"
32
33namespace orxonox
34{
35    /**
36    @brief
37        Destructor destroys all CommandLineArguments with it.
38    */
39    CommandLine::~CommandLine()
40    {
41        for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
42            it != cmdLineArgs_.end(); ++it)
43        {
44            delete it->second;
45        }
46    }
47
48    /**
49    @brief
50        Returns a unique instance (Meyers Singleton).
51    */
52    CommandLine& CommandLine::_getInstance()
53    {
54        static CommandLine instance;
55        return instance;
56    }
57
58    /**
59    @brief
60        Reads the command line parses the values of each argument.
61        It is then stored in the corresponding CommandLineArgument.
62    @note
63        The reason that you have to provide the string to be parsed as
64        space separted list is because of argc and argv. If you only have
65        a whole string, simply use getAllStrings() of SubString.
66    @param arguments
67        Vector of space separated strings.
68    */
69    void CommandLine::_parse(const std::vector<std::string>& arguments)
70    {
71        // why this? See bFirstTimeParse_ declaration.
72        if (bFirstTimeParse_)
73        {
74            // first shove all the shortcuts in a map
75            for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.begin();
76                it != cmdLineArgs_.end(); ++it)
77            {
78                OrxAssert(cmdLineArgsShortcut_.find(it->second->getShortcut()) == cmdLineArgsShortcut_.end(),
79                    "Cannot have two command line shortcut with the same name.");
80                if (it->second->getShortcut() != "")
81                    cmdLineArgsShortcut_[it->second->getShortcut()] = it->second;
82            }
83            bFirstTimeParse_ = false;
84        }
85
86        std::string name;
87        std::string shortcut;
88        std::string value;
89        for (unsigned int i = 0; i < arguments.size(); ++i)
90        {
91            if (arguments[i].size() != 0)
92            {
93                // sure not ""
94                if (arguments[i][0] == '-')
95                {
96                    // start with "-"
97                    if (arguments[i].size() == 1)
98                    {
99                        // argument[i] is "-", probably a minus sign
100                        value += "- ";
101                    }
102                    else if (arguments[i][1] <= 57 && arguments[i][1] >= 48)
103                    {
104                        // negative number as a value
105                        value += arguments[i] + " ";
106                    }
107                    else
108                    {
109                        // can be shortcut or full name argument
110
111                        // save old data first
112                        value = removeTrailingWhitespaces(value);
113                        if (name != "")
114                        {
115                            checkFullArgument(name, value);
116                            name = "";
117                            assert(shortcut == "");
118                        }
119                        else if (shortcut != "")
120                        {
121                            checkShortcut(shortcut, value);
122                            shortcut = "";
123                            assert(name == "");
124                        }
125
126                        if (arguments[i][1] == '-')
127                        {
128                            // full name argument with "--name"
129                            name = arguments[i].substr(2);
130                        }
131                        else
132                        {
133                            // shortcut with "-s"
134                            shortcut = arguments[i].substr(1);
135                        }
136
137                        // reset value string
138                        value = "";
139                    }
140                }
141                else
142                {
143                    // value string
144
145                    if (name == "" && shortcut == "")
146                    {
147                        ThrowException(Argument, "Expected \"-\" or \"-\" in command line arguments.\n");
148                    }
149
150                    // Concatenate strings as long as there's no new argument by "-" or "--"
151                    value += arguments[i] + ' ';
152                }
153            }
154        }
155
156        // parse last argument
157        value = removeTrailingWhitespaces(value);
158        if (name != "")
159        {
160            checkFullArgument(name, value);
161            assert(shortcut == "");
162        }
163        else if (shortcut != "")
164        {
165            checkShortcut(shortcut, value);
166            assert(name == "");
167        }
168    }
169
170    /**
171    @brief
172        Parses an argument based on its full name.
173    @param name
174        Full name of the argument
175    @param value
176        String containing the value
177    */
178    void CommandLine::checkFullArgument(const std::string& name, const std::string& value)
179    {
180        std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgs_.find(name);
181        if (it == cmdLineArgs_.end())
182            ThrowException(Argument, "Command line argument '" + name + "' does not exist.");
183
184        it->second->parse(value);
185    }
186
187    /**
188    @brief
189        Parses an argument based on its shortcut.
190    @param shortcut
191        Shotcut to the argument
192    @param value
193        String containing the value
194    */
195    void CommandLine::checkShortcut(const std::string& shortcut, const std::string& value)
196    {
197        std::map<std::string, BaseCommandLineArgument*>::const_iterator it = cmdLineArgsShortcut_.find(shortcut);
198        if (it == cmdLineArgsShortcut_.end())
199            ThrowException(Argument, "Command line shortcut '" + shortcut + "' does not exist.");
200
201        it->second->parse(value);
202    }
203
204    std::string CommandLine::getUsageInformation()
205    {
206        CommandLine* inst = &_getInstance();
207        std::string infoStr;
208        for (std::map<std::string, BaseCommandLineArgument*>::const_iterator it = inst->cmdLineArgs_.begin();
209            it != inst->cmdLineArgs_.end(); ++it)
210        {
211            infoStr += "[--" + it->second->getName() + " " + it->second->getInformation() + "] ";
212        }
213        return infoStr;
214    }
215
216}
Note: See TracBrowser for help on using the repository browser.