Added --debug and --verbose command line arguments
[supertux.git] / src / supertux / command_line_arguments.cpp
1 //  SuperTux
2 //  Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/command_line_arguments.hpp"
18
19 #include <stdexcept>
20 #include <physfs.h>
21 #include <boost/format.hpp>
22
23 #include "supertux/gameconfig.hpp"
24 #include "supertux/main.hpp"
25 #include "util/gettext.hpp"
26 #include "version.h"
27
28 CommandLineArguments::CommandLineArguments() :
29   m_action(NO_ACTION),
30   m_log_level(LOG_WARNING),
31   fullscreen_size(),
32   fullscreen_refresh_rate(),
33   window_size(),
34   aspect_size(),
35   use_fullscreen(),
36   video(),
37   show_fps(),
38   sound_enabled(),
39   music_enabled(),
40   console_enabled(),
41   start_level(),
42   enable_script_debugger(),
43   start_demo(),
44   record_demo()
45 {
46 }
47
48 CommandLineArguments::~CommandLineArguments()
49 {
50 }
51
52 void
53 CommandLineArguments::print_datadir()
54 {
55   // Print the datadir searchpath to stdout, one path per
56   // line. Then exit. Intended for use by the supertux-editor.
57   char **sp;
58   size_t sp_index;
59   sp = PHYSFS_getSearchPath();
60   if (sp)
61     for (sp_index = 0; sp[sp_index]; sp_index++)
62       std::cout << sp[sp_index] << std::endl;
63   PHYSFS_freeList(sp);
64 }
65
66 void
67 CommandLineArguments::print_help(const char* argv0)
68 {
69   std::string default_user_data_dir =
70       std::string(PHYSFS_getUserDir()) + WRITEDIR_NAME;
71
72   std::cerr << boost::format(_(
73                  "\n"
74                  "Usage: %s [OPTIONS] [LEVELFILE]\n\n"
75                  "CommandLineArguments:\n"
76                  "  --verbose                    Print verbose messages\n"
77                  "  --debug                      Print extra verbose messages\n"
78                  "  -f, --fullscreen             Run in fullscreen mode\n"
79                  "  -w, --window                 Run in window mode\n"
80                  "  -g, --geometry WIDTHxHEIGHT  Run SuperTux in given resolution\n"
81                  "  -a, --aspect WIDTH:HEIGHT    Run SuperTux with given aspect ratio\n"
82                  "  -d, --default                Reset video settings to default values\n"
83                  "  --renderer RENDERER          Use sdl, opengl, or auto to render\n"
84                  "  --disable-sound              Disable sound effects\n"
85                  "  --disable-music              Disable music\n"
86                  "  -h, --help                   Show this help message and quit\n"
87                  "  -v, --version                Show SuperTux version and quit\n"
88                  "  --console                    Enable ingame scripting console\n"
89                  "  --noconsole                  Disable ingame scripting console\n"
90                  "  --show-fps                   Display framerate in levels\n"
91                  "  --no-show-fps                Do not display framerate in levels\n"
92                  "  --record-demo FILE LEVEL     Record a demo to FILE\n"
93                  "  --play-demo FILE LEVEL       Play a recorded demo\n"
94                  "  -s, --debug-scripts          Enable script debugger.\n"
95                  "  --print-datadir              Print supertux's primary data directory.\n"
96                  "\n"
97                  "Environment variables:\n"
98                  "  SUPERTUX2_USER_DIR           Directory for user data (savegames, etc.);\n"
99                  "                               default %s\n"
100                  "\n"
101                  ))
102             % argv0 % default_user_data_dir
103             << std::flush;
104 }
105
106 void
107 CommandLineArguments::print_version()
108 {
109   std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
110 }
111
112 void
113 CommandLineArguments::parse_args(int argc, char** argv)
114 {
115   for(int i = 1; i < argc; ++i)
116   {
117     std::string arg = argv[i];
118
119     if (arg == "--version" || arg == "-v")
120     {
121       m_action = PRINT_VERSION;
122
123     }
124     else if (arg == "--help" || arg == "-h")
125     {
126       m_action = PRINT_HELP;
127     }
128     else if (arg == "--print-datadir")
129     {
130       m_action = PRINT_DATADIR;
131     }
132     else if (arg == "--debug")
133     {
134       m_log_level = LOG_DEBUG;
135     }
136     else if (arg == "--verbose")
137     {
138       if (m_log_level < LOG_INFO)
139       {
140         m_log_level = LOG_INFO;
141       }
142     }
143     else if (arg == "--fullscreen" || arg == "-f")
144     {
145       use_fullscreen = true;
146     }
147     else if (arg == "--default" || arg == "-d")
148     {
149       use_fullscreen = false;
150
151       window_size = Size(800, 600);
152       fullscreen_size = Size(800, 600);
153       fullscreen_refresh_rate = 0;
154       aspect_size = Size(0, 0);  // auto detect
155     }
156     else if (arg == "--window" || arg == "-w")
157     {
158       use_fullscreen = false;
159     }
160     else if (arg == "--geometry" || arg == "-g")
161     {
162       i += 1;
163       if (i >= argc)
164       {
165         throw std::runtime_error("Need to specify a size (WIDTHxHEIGHT) for geometry argument");
166       }
167       else
168       {
169         int width, height;
170         if (sscanf(argv[i], "%dx%d", &width, &height) != 2)
171         {
172           throw std::runtime_error("Invalid geometry spec, should be WIDTHxHEIGHT");
173         }
174         else
175         {
176           window_size     = Size(width, height);
177           fullscreen_size = Size(width, height);
178           fullscreen_refresh_rate = 0;
179         }
180       }
181     }
182     else if (arg == "--aspect" || arg == "-a")
183     {
184       i += 1;
185       if (i >= argc)
186       {
187         throw std::runtime_error("Need to specify a ratio (WIDTH:HEIGHT) for aspect ratio");
188       }
189       else
190       {
191         int aspect_width  = 0;
192         int aspect_height = 0;
193         if (strcmp(argv[i], "auto") == 0)
194         {
195           aspect_width  = 0;
196           aspect_height = 0;
197         }
198         else if (sscanf(argv[i], "%d:%d", &aspect_width, &aspect_height) != 2)
199         {
200           throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT or auto");
201         }
202         else
203         {
204           float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
205
206           // use aspect ratio to calculate logical resolution
207           if (aspect_ratio > 1) {
208             aspect_size = Size(static_cast<int>(600 * aspect_ratio + 0.5),
209                                          600);
210           } else {
211             aspect_size = Size(600,
212                                          static_cast<int>(600 * 1/aspect_ratio + 0.5));
213           }
214         }
215       }
216     }
217     else if (arg == "--renderer")
218     {
219       i += 1;
220       if (i >= argc)
221       {
222         throw std::runtime_error("Need to specify a renderer for renderer argument");
223       }
224       else
225       {
226         video = VideoSystem::get_video_system(argv[i]);
227       }
228     }
229     else if (arg == "--show-fps")
230     {
231       show_fps = true;
232     }
233     else if (arg == "--no-show-fps")
234     {
235       show_fps = false;
236     }
237     else if (arg == "--console")
238     {
239       console_enabled = true;
240     }
241     else if (arg == "--noconsole")
242     {
243       console_enabled = false;
244     }
245     else if (arg == "--disable-sound" || arg == "--disable-sfx")
246     {
247       sound_enabled = false;
248     }
249     else if (arg == "--disable-music")
250     {
251       music_enabled = false;
252     }
253     else if (arg == "--play-demo")
254     {
255       if (i+1 >= argc)
256       {
257         throw std::runtime_error("Need to specify a demo filename");
258       }
259       else
260       {
261         start_demo = argv[++i];
262       }
263     }
264     else if (arg == "--record-demo")
265     {
266       if (i+1 >= argc)
267       {
268         throw std::runtime_error("Need to specify a demo filename");
269       }
270       else
271       {
272         record_demo = argv[++i];
273       }
274     }
275     else if (arg == "--debug-scripts" || arg == "-s")
276     {
277       enable_script_debugger = true;
278     }
279     else if (arg[0] != '-')
280     {
281       start_level = arg;
282     }
283     else
284     {
285       throw std::runtime_error((boost::format("Unknown option '%1%''. Use --help to see a list of options") % arg).str());
286     }
287   }
288 }
289
290 void
291 CommandLineArguments::merge_into(Config& config)
292 {
293 #define merge_option(x) if (x) { config.x = *x; }
294
295   merge_option(fullscreen_size);
296   merge_option(fullscreen_refresh_rate);
297   merge_option(window_size);
298   merge_option(aspect_size);
299   merge_option(use_fullscreen);
300   merge_option(video);
301   merge_option(show_fps);
302   merge_option(sound_enabled);
303   merge_option(music_enabled);
304   merge_option(console_enabled);
305   merge_option(start_level);
306   merge_option(enable_script_debugger);
307   merge_option(start_demo);
308   merge_option(record_demo);
309
310 #undef merge_option
311 }
312
313 /* EOF */