[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 |
---|
| 13 | co-programmer: |
---|
| 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; |
---|
| 42 | |
---|
| 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; |
---|
[7241] | 49 | |
---|
| 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]; |
---|
| 60 | |
---|
[7241] | 61 | //put all args in vector |
---|
| 62 | std::vector<std::string> args; |
---|
| 63 | |
---|
| 64 | for ( int i = 1; i<argc; i++ ) |
---|
[7243] | 65 | { |
---|
| 66 | std::string s = argv[i]; |
---|
| 67 | |
---|
| 68 | if ( s.find( "=" ) == std::string::npos ) |
---|
| 69 | { |
---|
| 70 | args.push_back(s); |
---|
| 71 | } |
---|
| 72 | else |
---|
| 73 | { |
---|
| 74 | std::string op = s; |
---|
| 75 | std::string ar = s; |
---|
| 76 | op.erase( op.find("=") ); |
---|
| 77 | ar.erase( 0, ar.find("=")+1); |
---|
| 78 | |
---|
[7246] | 79 | //PRINTF(0)("'%s' '%s'\n", op.c_str(), ar.c_str()); |
---|
[7243] | 80 | args.push_back( op ); |
---|
| 81 | args.push_back( ar ); |
---|
| 82 | } |
---|
| 83 | } |
---|
[7241] | 84 | |
---|
| 85 | int i = 0; |
---|
| 86 | |
---|
| 87 | ArgTable::iterator it; |
---|
| 88 | bool finish; |
---|
| 89 | bool found; |
---|
| 90 | |
---|
| 91 | while ( i < args.size() ) |
---|
| 92 | { |
---|
| 93 | found = false; |
---|
| 94 | for ( it = argTable.begin(); it != argTable.end(); it++ ) |
---|
| 95 | { |
---|
| 96 | if ( matches( *it, args[i], finish ) ) |
---|
| 97 | { |
---|
| 98 | found = true; |
---|
| 99 | |
---|
| 100 | if ( it->numArgs + i >= args.size() ) |
---|
| 101 | { |
---|
[7243] | 102 | PRINTF(1)( "%s needs %d arguments!\n", args[i].c_str(), it->numArgs ); |
---|
| 103 | return false; |
---|
[7241] | 104 | } |
---|
| 105 | |
---|
[7243] | 106 | std::vector<MultiType> argArgs; |
---|
[7241] | 107 | |
---|
[7243] | 108 | for ( int j = 1; j <= it->numArgs; j++ ) |
---|
[7241] | 109 | argArgs.push_back( args[i+j] ); |
---|
| 110 | |
---|
| 111 | if ( !cb( *it, data, args[i], argArgs ) ) |
---|
| 112 | return false; |
---|
| 113 | |
---|
[7243] | 114 | i += it->numArgs + 1; |
---|
[7241] | 115 | |
---|
| 116 | if ( finish ) |
---|
| 117 | break; |
---|
| 118 | else |
---|
| 119 | { |
---|
| 120 | assert( it->numArgs == 0 ); |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if ( !found ) |
---|
| 126 | { |
---|
| 127 | PRINTF(1)("%s: illegal option\n", args[i].c_str()); |
---|
| 128 | return false; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return true; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | bool CmdLineParser::matches( ArgTableEntry entry, std::string arg, bool & finish ) |
---|
| 136 | { |
---|
| 137 | finish = true; |
---|
| 138 | |
---|
| 139 | if ( arg.length() < 2 ) |
---|
| 140 | return false; |
---|
| 141 | |
---|
| 142 | if ( arg[0] == '-' ) |
---|
| 143 | { |
---|
| 144 | if ( arg[1] == '-' ) |
---|
| 145 | { |
---|
| 146 | arg.erase( 0, 2 ); |
---|
| 147 | |
---|
[7243] | 148 | if ( entry.longOption.find('%') != std::string::npos ) |
---|
[7241] | 149 | { |
---|
| 150 | //TODO implement bether match algo |
---|
[7243] | 151 | assert( entry.longOption.find('%') == entry.longOption.length()-1 ); |
---|
| 152 | std::string lo = entry.longOption; |
---|
| 153 | lo.erase( lo.length()-1, 1 ); |
---|
[7246] | 154 | //PRINTF(0)("%s %s\n", arg.c_str(), lo.c_str()); |
---|
[7243] | 155 | return arg.find( lo ) == 0; |
---|
[7241] | 156 | } |
---|
| 157 | else |
---|
| 158 | { |
---|
[7243] | 159 | return arg.find( entry.longOption ) != std::string::npos; |
---|
[7241] | 160 | } |
---|
| 161 | } |
---|
| 162 | else |
---|
| 163 | { |
---|
| 164 | if ( arg.length() != 2 && entry.numArgs != 0 ) |
---|
| 165 | { |
---|
| 166 | PRINTF(1)("using multiple flags together is only alowed if none needs an arugument. %s needs %d arguments\n", entry.shortOption, entry.numArgs); |
---|
| 167 | return false; |
---|
| 168 | } |
---|
[7243] | 169 | finish = arg.length()==2; |
---|
[7241] | 170 | return arg.find(entry.shortOption) != std::string::npos; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|
[7250] | 174 | |
---|
| 175 | void CmdLineParser::showHelp() |
---|
| 176 | { |
---|
| 177 | printf("Usage: %s [options]\n", exeName.c_str()); |
---|
| 178 | printf("\n"); |
---|
| 179 | |
---|
| 180 | std::list<std::vector<std::string> > output; |
---|
| 181 | |
---|
| 182 | for ( ArgTable::iterator it = argTable.begin(); it != argTable.end(); it++ ) |
---|
| 183 | { |
---|
| 184 | output.push_back( std::vector<std::string>() ); |
---|
| 185 | |
---|
| 186 | SubString substr( it->argNames ); |
---|
| 187 | std::string args; |
---|
| 188 | assert( it->numArgs == substr.getCount() ); |
---|
| 189 | |
---|
| 190 | for ( int i = 0; i<it->numArgs; i++ ) |
---|
| 191 | { |
---|
| 192 | args += " [" + substr[i] + "]"; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | if ( it->shortOption != '\0' ) |
---|
| 196 | { |
---|
| 197 | output.back().push_back( " -" + std::string((char*)&it->shortOption, 1) ); |
---|
| 198 | output.back().back() += args; |
---|
| 199 | } |
---|
| 200 | else |
---|
| 201 | output.back().push_back( "" ); |
---|
| 202 | |
---|
| 203 | if ( it->longOption != "" ) |
---|
| 204 | { |
---|
| 205 | output.back().push_back( "--" + it->longOption ); |
---|
| 206 | |
---|
| 207 | output.back().back() += args; |
---|
| 208 | } |
---|
| 209 | else |
---|
| 210 | output.back().push_back( "" ); |
---|
| 211 | |
---|
| 212 | output.back().push_back( it->help ); |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | output.push_back( std::vector<std::string>() ); |
---|
| 216 | output.back().push_back( "Option" ); |
---|
| 217 | output.back().push_back( "Long option" ); |
---|
[7253] | 218 | output.back().push_back( "Description" ); |
---|
[7250] | 219 | |
---|
| 220 | output.reverse(); |
---|
| 221 | |
---|
| 222 | int maxShort = 0; |
---|
| 223 | int maxLong = 0; |
---|
| 224 | |
---|
| 225 | std::list<std::vector<std::string> >::const_iterator it; |
---|
| 226 | |
---|
| 227 | for ( it = output.begin(); it != output.end(); it++ ) |
---|
| 228 | { |
---|
| 229 | if ( (*it)[0].length() > maxShort ) |
---|
| 230 | maxShort = (*it)[0].length(); |
---|
| 231 | |
---|
| 232 | if ( (*it)[1].length() > maxLong ) |
---|
| 233 | maxLong = (*it)[1].length(); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | for ( it = output.begin(); it != output.end(); it++ ) |
---|
| 237 | { |
---|
| 238 | printf("%s ", (*it)[0].c_str()); |
---|
| 239 | |
---|
| 240 | for ( int i = 0; i<maxShort-(*it)[0].length(); i++ ) |
---|
| 241 | printf(" "); |
---|
| 242 | |
---|
| 243 | printf("%s ", (*it)[1].c_str()); |
---|
| 244 | |
---|
| 245 | for ( int i = 0; i<maxLong-(*it)[1].length(); i++ ) |
---|
| 246 | printf(" "); |
---|
| 247 | |
---|
| 248 | printf("%s\n", (*it)[2].c_str()); |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | exit(0); |
---|
| 252 | } |
---|