| 56 | } |
| 57 | |
| 58 | void ProgramOptions::parse(int argc, const char* argv[]) |
| 59 | { |
| 60 | for (int i=1; i<argc; ++i) { |
| 61 | string arg(argv[i]); |
| 62 | |
| 63 | // n = number of dashes ('-') at the beginning of the argument. |
| 64 | size_t n = 0; |
| 65 | for (; arg[n] == '-'; ++n) |
| 66 | ; |
| 67 | size_t len = arg.size()-n; |
| 68 | |
| 69 | // Ignore process serial number argument (-psn...) when the app is run from command line |
| 70 | #if __APPLE__ |
| 71 | if (arg.size() >= 4 && arg.substr(0, 4) == "-psn") |
| 72 | continue; |
| 73 | #endif |
| 74 | |
| 75 | if ((n > 0) && (len > 0)) { |
| 76 | // Use mnemonics |
| 77 | if (n == 1) { |
| 78 | char usedBy = 0; |
| 79 | |
| 80 | for (size_t j=1; j<arg.size(); ++j) { |
| 81 | OptionList::iterator it = |
| 82 | find_if(m_options.begin(), m_options.end(), same_mnemonic(arg[j])); |
| 83 | |
| 84 | if (it == m_options.end()) { |
| 85 | stringstream msg; |
| 86 | msg << "Invalid option '-" << arg[j] << "'"; |
| 87 | throw InvalidProgramOption(msg.str()); |
| 88 | } |
| 89 | |
| 90 | Option* option = *it; |
| 91 | std::string optionValue; |
| 92 | |
| 93 | if (option->doesRequireValue()) { |
| 94 | if (usedBy != 0) { |
| 95 | stringstream msg; |
| 96 | msg << "You cannot use '-" << option->mnemonic() |
| 97 | << "' and '-" << usedBy << "' " |
| 98 | << "together, both options need one extra argument"; |
| 99 | throw InvalidProgramOptionsCombination(msg.str()); |
| 100 | } |
| 101 | |
| 102 | if (i+1 >= argc) { |
| 103 | stringstream msg; |
| 104 | msg << "Option '-" << option->mnemonic() |
| 105 | << "' needs one extra argument"; |
| 106 | throw ProgramOptionNeedsValue(msg.str()); |
| 107 | } |
| 108 | |
| 109 | // Set the value specified for this argument |
| 110 | optionValue = argv[++i]; |
| 111 | usedBy = option->mnemonic(); |
| 112 | } |
| 113 | |
| 114 | m_values.push_back(Value(option, optionValue)); |
| 115 | } |