| 146 | } |
| 147 | |
| 148 | int cCommandlineParser::parse(bool ignoreDuplicates, bool ignoreUnknown) |
| 149 | { |
| 150 | bool showusage = false; |
| 151 | |
| 152 | // foreach argv element with(--) or (-), find the thing in opt |
| 153 | SMILE_DBG(3,"about to parse %i commandline options ",argc-1); |
| 154 | for (int i = 1; i < argc; i++) { |
| 155 | SMILE_DBG(5,"parsing option %i : '%s'",i,argv[i]); |
| 156 | |
| 157 | if ((unsigned char)argv[i][0] > 127) { |
| 158 | SMILE_ERR(0, "Please don't be lazy and copy & paste the commandlines from the SMILE book PDF. Your commandline contains invalid ASCII characters (probably incorrect '-'es) as a result of this and might not be parsed correctly! Type the commandline from scratch to avoid errors."); |
| 159 | SMILE_ERR(0, "The offending argument is: '%s'", argv[i]); |
| 160 | COMP_ERR("Parse error."); |
| 161 | } |
| 162 | |
| 163 | // skip if it's no option, i.e. doesn't start with a hyphen |
| 164 | if (argv[i][0] != '-') |
| 165 | continue; |
| 166 | |
| 167 | // usage (-h) |
| 168 | if (strcmp(argv[i], "-h") == 0) { |
| 169 | showusage = 1; |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | const char *o = argv[i]+1; |
| 174 | |
| 175 | // skip second hyphen if present |
| 176 | if (o[0] == '-') o++; |
| 177 | |
| 178 | sCmdlineOpt *opt = findOpt(o); |
| 179 | |
| 180 | if (opt == NULL) { // not found! |
| 181 | if (!ignoreUnknown) { |
| 182 | SMILE_ERR(0,"parse: unknown option '%s' on commandline!",argv[i]); |
| 183 | } |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | if (opt->isSet) { // option specified twice...! |
| 188 | if (!ignoreDuplicates) { // if ignoreDuplicates, we suppress this error message |
| 189 | SMILE_ERR(0,"duplicate option '%s' on commandline (ignoring duplicate!) ",o); |
| 190 | } |
| 191 | if (opt->numArgs > 0) { |
| 192 | i += opt->numArgs; |
| 193 | } |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | // now look for value.... |
| 198 | if (i+1 < argc) { |
| 199 | bool optCand = false; |
| 200 | if (argv[i+1][0] == '-') |
| 201 | optCand = true; // argument might be an option... |
| 202 | |
| 203 | // parse value according to type... |
| 204 | switch (opt->type) { |
| 205 | case eCmdlineOptType::Boolean: { |
no test coverage detected