[7241] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christoph Renner |
---|
[7319] | 13 | co-programmer: |
---|
[7241] | 14 | */ |
---|
| 15 | |
---|
| 16 | #include "cmdline_parser.h" |
---|
| 17 | |
---|
[7250] | 18 | #include "src/lib/util/substring.h" |
---|
| 19 | |
---|
[7241] | 20 | using namespace std; |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * standard constructor |
---|
| 25 | */ |
---|
| 26 | CmdLineParser::CmdLineParser () |
---|
| 27 | { |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * standard deconstructor |
---|
| 33 | */ |
---|
| 34 | CmdLineParser::~CmdLineParser () |
---|
| 35 | { |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | |
---|
[7250] | 39 | bool CmdLineParser::add( int id, const std::string & longOption, char shortOption, int numArgs, const std::string & argNames, const std::string& help, bool back ) |
---|
[7241] | 40 | { |
---|
| 41 | ArgTableEntry entry; |
---|
[7319] | 42 | |
---|
[7241] | 43 | entry.id = id; |
---|
| 44 | entry.longOption = longOption; |
---|
| 45 | entry.shortOption = shortOption; |
---|
| 46 | entry.numArgs = numArgs; |
---|
[7250] | 47 | entry.argNames = argNames; |
---|
| 48 | entry.help = help; |
---|
[7319] | 49 | |
---|
[7241] | 50 | if ( back ) |
---|
| 51 | argTable.push_back( entry ); |
---|
| 52 | else |
---|
| 53 | argTable.push_front( entry ); |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | bool CmdLineParser::parse( ArgParserCallback cb, void * data, int argc, char ** argv ) |
---|
| 58 | { |
---|
[7250] | 59 | this->exeName = argv[0]; |
---|
[7319] | 60 | |
---|
[7241] | 61 | //put all args in vector |
---|
| 62 | std::vector<std::string> args; |
---|
[7319] | 63 | |
---|
[7241] | 64 | for ( int i = 1; i<argc; i++ ) |
---|
[7243] | 65 | { |
---|
| 66 | std::string s = argv[i]; |
---|
[7319] | 67 | |
---|
[7243] | 68 | if ( s.find( "=" ) == std::string::npos ) |
---|
| 69 | { |
---|
[7260] | 70 | if ( s.length() > 2 && s[0] == '-' && s[1] != '-' ) |
---|
| 71 | { |
---|
| 72 | for ( int j = 1; j < s.length(); j++ ) |
---|
| 73 | { |
---|
| 74 | std::string t = "-"; |
---|
| 75 | t += s[j]; |
---|
| 76 | args.push_back( t ); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | { |
---|
| 81 | args.push_back(s); |
---|
| 82 | } |
---|
[7243] | 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | std::string op = s; |
---|
| 87 | std::string ar = s; |
---|
| 88 | op.erase( op.find("=") ); |
---|
| 89 | ar.erase( 0, ar.find("=")+1); |
---|
[7319] | 90 | |
---|
[7246] | 91 | //PRINTF(0)("'%s' '%s'\n", op.c_str(), ar.c_str()); |
---|
[7243] | 92 | args.push_back( op ); |
---|
| 93 | args.push_back( ar ); |
---|
| 94 | } |
---|
| 95 | } |
---|
[7319] | 96 | |
---|
[7241] | 97 | int i = 0; |
---|
[7319] | 98 | |
---|
[7241] | 99 | ArgTable::iterator it; |
---|
| 100 | bool finish; |
---|
| 101 | bool found; |
---|
[7319] | 102 | |
---|
[7241] | 103 | while ( i < args.size() ) |
---|
| 104 | { |
---|
| 105 | found = false; |
---|
| 106 | for ( it = argTable.begin(); it != argTable.end(); it++ ) |
---|
| 107 | { |
---|
| 108 | if ( matches( *it, args[i], finish ) ) |
---|
| 109 | { |
---|
| 110 | found = true; |
---|
[7319] | 111 | |
---|
[7260] | 112 | int posArgs = 1; |
---|
[7319] | 113 | |
---|
[7260] | 114 | while ( i + posArgs < args.size() ) |
---|
[7241] | 115 | { |
---|
[7260] | 116 | if ( args[ i + posArgs ].length() > 0 && args[ i + posArgs ][0] == '-' ) |
---|
| 117 | break; |
---|
| 118 | else |
---|
| 119 | posArgs++; |
---|
| 120 | } |
---|
[7319] | 121 | |
---|
[7260] | 122 | posArgs--; |
---|
[7319] | 123 | |
---|
[7260] | 124 | if ( it->numArgs > posArgs ) |
---|
| 125 | { |
---|
[7243] | 126 | PRINTF(1)( "%s needs %d arguments!\n", args[i].c_str(), it->numArgs ); |
---|
| 127 | return false; |
---|
[7241] | 128 | } |
---|
[7319] | 129 | |
---|
[7243] | 130 | std::vector<MultiType> argArgs; |
---|
[7319] | 131 | |
---|
[7243] | 132 | for ( int j = 1; j <= it->numArgs; j++ ) |
---|
[7241] | 133 | argArgs.push_back( args[i+j] ); |
---|
[7319] | 134 | |
---|
[7241] | 135 | if ( !cb( *it, data, args[i], argArgs ) ) |
---|
| 136 | return false; |
---|
[7319] | 137 | |
---|
[7259] | 138 | i += it->numArgs; |
---|
[7319] | 139 | |
---|
[7241] | 140 | if ( finish ) |
---|
[7259] | 141 | { |
---|
| 142 | i++; |
---|
[7241] | 143 | break; |
---|
[7259] | 144 | } |
---|
[7319] | 145 | else |
---|
| 146 | { |
---|
[7241] | 147 | assert( it->numArgs == 0 ); |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | } |
---|
[7319] | 151 | |
---|
[7241] | 152 | if ( !found ) |
---|
| 153 | { |
---|
| 154 | PRINTF(1)("%s: illegal option\n", args[i].c_str()); |
---|
| 155 | return false; |
---|
| 156 | } |
---|
| 157 | } |
---|
[7319] | 158 | |
---|
[7241] | 159 | return true; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | bool CmdLineParser::matches( ArgTableEntry entry, std::string arg, bool & finish ) |
---|
| 163 | { |
---|
| 164 | finish = true; |
---|
[7319] | 165 | |
---|
[7241] | 166 | if ( arg.length() < 2 ) |
---|
| 167 | return false; |
---|
[7319] | 168 | |
---|
[7241] | 169 | if ( arg[0] == '-' ) |
---|
| 170 | { |
---|
| 171 | if ( arg[1] == '-' ) |
---|
| 172 | { |
---|
| 173 | arg.erase( 0, 2 ); |
---|
[7319] | 174 | |
---|
[7243] | 175 | if ( entry.longOption.find('%') != std::string::npos ) |
---|
[7241] | 176 | { |
---|
| 177 | //TODO implement bether match algo |
---|
[7243] | 178 | assert( entry.longOption.find('%') == entry.longOption.length()-1 ); |
---|
| 179 | std::string lo = entry.longOption; |
---|
| 180 | lo.erase( lo.length()-1, 1 ); |
---|
[7246] | 181 | //PRINTF(0)("%s %s\n", arg.c_str(), lo.c_str()); |
---|
[7243] | 182 | return arg.find( lo ) == 0; |
---|
[7241] | 183 | } |
---|
| 184 | else |
---|
| 185 | { |
---|
[7243] | 186 | return arg.find( entry.longOption ) != std::string::npos; |
---|
[7241] | 187 | } |
---|
| 188 | } |
---|
| 189 | else |
---|
| 190 | { |
---|
[7259] | 191 | if ( arg.find(entry.shortOption) != std::string::npos && arg.length() != 2 && entry.numArgs != 0 ) |
---|
[7241] | 192 | { |
---|
[7259] | 193 | PRINTF(1)("using multiple flags together is only alowed if none needs an arugument. %c needs %d arguments\n", entry.shortOption, entry.numArgs); |
---|
| 194 | //FIXME find beter solution |
---|
| 195 | exit(1); |
---|
[7241] | 196 | return false; |
---|
| 197 | } |
---|
[7243] | 198 | finish = arg.length()==2; |
---|
[7241] | 199 | return arg.find(entry.shortOption) != std::string::npos; |
---|
| 200 | } |
---|
| 201 | } |
---|
[7260] | 202 | else |
---|
| 203 | return false; |
---|
[7241] | 204 | } |
---|
[7250] | 205 | |
---|
| 206 | void CmdLineParser::showHelp() |
---|
| 207 | { |
---|
| 208 | printf("Usage: %s [options]\n", exeName.c_str()); |
---|
| 209 | printf("\n"); |
---|
[7319] | 210 | |
---|
[7250] | 211 | std::list<std::vector<std::string> > output; |
---|
[7319] | 212 | |
---|
[7250] | 213 | for ( ArgTable::iterator it = argTable.begin(); it != argTable.end(); it++ ) |
---|
| 214 | { |
---|
| 215 | output.push_back( std::vector<std::string>() ); |
---|
[7319] | 216 | |
---|
[7250] | 217 | SubString substr( it->argNames ); |
---|
| 218 | std::string args; |
---|
[7319] | 219 | assert( it->numArgs == substr.size() ); |
---|
| 220 | |
---|
[7250] | 221 | for ( int i = 0; i<it->numArgs; i++ ) |
---|
| 222 | { |
---|
| 223 | args += " [" + substr[i] + "]"; |
---|
| 224 | } |
---|
[7319] | 225 | |
---|
[7250] | 226 | if ( it->shortOption != '\0' ) |
---|
| 227 | { |
---|
| 228 | output.back().push_back( " -" + std::string((char*)&it->shortOption, 1) ); |
---|
| 229 | output.back().back() += args; |
---|
| 230 | } |
---|
| 231 | else |
---|
| 232 | output.back().push_back( "" ); |
---|
[7319] | 233 | |
---|
[7250] | 234 | if ( it->longOption != "" ) |
---|
| 235 | { |
---|
| 236 | output.back().push_back( "--" + it->longOption ); |
---|
[7319] | 237 | |
---|
[7250] | 238 | output.back().back() += args; |
---|
| 239 | } |
---|
| 240 | else |
---|
| 241 | output.back().push_back( "" ); |
---|
[7319] | 242 | |
---|
[7250] | 243 | output.back().push_back( it->help ); |
---|
| 244 | } |
---|
[7319] | 245 | |
---|
[7250] | 246 | output.push_back( std::vector<std::string>() ); |
---|
| 247 | output.back().push_back( "Option" ); |
---|
| 248 | output.back().push_back( "Long option" ); |
---|
[7253] | 249 | output.back().push_back( "Description" ); |
---|
[7319] | 250 | |
---|
[7250] | 251 | output.reverse(); |
---|
[7319] | 252 | |
---|
[7250] | 253 | int maxShort = 0; |
---|
| 254 | int maxLong = 0; |
---|
[7319] | 255 | |
---|
[7250] | 256 | std::list<std::vector<std::string> >::const_iterator it; |
---|
[7319] | 257 | |
---|
[7250] | 258 | for ( it = output.begin(); it != output.end(); it++ ) |
---|
| 259 | { |
---|
| 260 | if ( (*it)[0].length() > maxShort ) |
---|
| 261 | maxShort = (*it)[0].length(); |
---|
[7319] | 262 | |
---|
[7250] | 263 | if ( (*it)[1].length() > maxLong ) |
---|
| 264 | maxLong = (*it)[1].length(); |
---|
| 265 | } |
---|
[7319] | 266 | |
---|
[7250] | 267 | for ( it = output.begin(); it != output.end(); it++ ) |
---|
| 268 | { |
---|
| 269 | printf("%s ", (*it)[0].c_str()); |
---|
[7319] | 270 | |
---|
[7250] | 271 | for ( int i = 0; i<maxShort-(*it)[0].length(); i++ ) |
---|
| 272 | printf(" "); |
---|
[7319] | 273 | |
---|
[7250] | 274 | printf("%s ", (*it)[1].c_str()); |
---|
[7319] | 275 | |
---|
[7250] | 276 | for ( int i = 0; i<maxLong-(*it)[1].length(); i++ ) |
---|
| 277 | printf(" "); |
---|
[7319] | 278 | |
---|
[7250] | 279 | printf("%s\n", (*it)[2].c_str()); |
---|
| 280 | } |
---|
[7319] | 281 | |
---|
[7250] | 282 | exit(0); |
---|
| 283 | } |
---|