| 1 |
/* |
| 2 |
Copyright (C) 2002 Kai Sterker <[email protected]> |
| 3 |
Part of the Adonthell Project <http://adonthell.nongnu.org> |
| 4 |
|
| 5 |
Dlgedit is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation; either version 2 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
Dlgedit is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with Dlgedit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @file dlg_cmdline.cc |
| 21 |
* |
| 22 |
* @author Kai Sterker |
| 23 |
* @brief Methods to parse the dlgedit commandline. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <iostream> |
| 27 |
#include <dirent.h> |
| 28 |
#include <unistd.h> |
| 29 |
#include "dlg_cmdline.h" |
| 30 |
|
| 31 |
// flag indicating whether to compile the given scripts |
| 32 |
bool DlgCmdline::compile = false; |
| 33 |
|
| 34 |
// the directory to look for project files |
| 35 |
std::string DlgCmdline::datadir = DATA_DIR"/games"; |
| 36 |
|
| 37 |
// the default project |
| 38 |
std::string DlgCmdline::project = "none"; |
| 39 |
|
| 40 |
// index of the first dialgoue source in argv[] |
| 41 |
int DlgCmdline::sources; |
| 42 |
|
| 43 |
// examine the parameters passed to dlgedit |
| 44 |
bool DlgCmdline::parse (int argc, char* argv[]) |
| 45 |
{ |
| 46 |
int c; |
| 47 |
|
| 48 |
// Check for options |
| 49 |
while ((c = getopt (argc, argv, "cdhvg:p:")) != -1) |
| 50 |
{ |
| 51 |
switch (c) |
| 52 |
{ |
| 53 |
case 'd': |
| 54 |
{ |
| 55 |
std::cout << datadir << std::endl; |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
case 'v': |
| 60 |
{ |
| 61 |
std::cout << _VERSION_ << std::endl; |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
case 'c': |
| 66 |
{ |
| 67 |
compile = true; |
| 68 |
break; |
| 69 |
} |
| 70 |
|
| 71 |
case 'p': |
| 72 |
{ |
| 73 |
project = optarg; |
| 74 |
break; |
| 75 |
} |
| 76 |
|
| 77 |
case 'g': |
| 78 |
{ |
| 79 |
datadir = optarg; |
| 80 |
|
| 81 |
if (datadir[datadir.size () - 1] == '/') |
| 82 |
datadir.erase (datadir.size () - 1); |
| 83 |
|
| 84 |
// Check whether the requested game directory exists |
| 85 |
DIR * mydir = opendir (datadir.c_str ()); |
| 86 |
|
| 87 |
if (!mydir) |
| 88 |
{ |
| 89 |
std::cerr << "No such directory " << datadir << "!" << std::endl; |
| 90 |
return false; |
| 91 |
} |
| 92 |
closedir (mydir); |
| 93 |
|
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
case '?': |
| 98 |
case 'h': |
| 99 |
{ |
| 100 |
help (argv[0]); |
| 101 |
return false; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
sources = optind; |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
// prints the help message |
| 111 |
void DlgCmdline::help (const std::string &program) |
| 112 |
{ |
| 113 |
std::cout << "Usage: " << program << " [OPTIONS] [SOURCES]" << std::endl; |
| 114 |
std::cout << std::endl; |
| 115 |
std::cout << "Where [OPTIONS] can be:\n"; |
| 116 |
std::cout << "-h print this help message and exit" << std::endl; |
| 117 |
std::cout << "-d print the project directory and exit" << std::endl; |
| 118 |
std::cout << "-v print version and exit" << std::endl; |
| 119 |
std::cout << "-c compile all SOURCES and exit" << std::endl; |
| 120 |
std::cout << "-g dir specify a custom project directory" << std::endl; |
| 121 |
std::cout << "-p project specify a default project" << std::endl; |
| 122 |
} |