MCPcopy Create free account
hub / github.com/docopt/docopt.cpp / parse_short

Function parse_short

docopt.cpp:284–346  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

282}
283
284static PatternList parse_short(Tokens& tokens, std::vector<Option>& options)
285{
286 // shorts ::= '-' ( chars )* [ [ ' ' ] chars ] ;
287
288 auto token = tokens.pop();
289
290 assert(starts_with(token, "-"));
291 assert(!starts_with(token, "--"));
292
293 auto i = token.begin();
294 ++i; // skip the leading '-'
295
296 PatternList ret;
297 while (i != token.end()) {
298 std::string shortOpt = { '-', *i };
299 ++i;
300
301 std::vector<Option const*> similar;
302 for(auto const& option : options) {
303 if (option.shortOption()==shortOpt)
304 similar.push_back(&option);
305 }
306
307 if (similar.size() > 1) {
308 std::string error = shortOpt + " is specified ambiguously "
309 + std::to_string(similar.size()) + " times";
310 throw Tokens::OptionError(std::move(error));
311 } else if (similar.empty()) {
312 options.emplace_back(shortOpt, "", 0);
313
314 auto o = std::make_shared<Option>(options.back());
315 if (tokens.isParsingArgv()) {
316 o->setValue(value{true});
317 }
318 ret.push_back(o);
319 } else {
320 auto o = std::make_shared<Option>(*similar[0]);
321 value val;
322 if (o->argCount()) {
323 if (i == token.end()) {
324 // consume the next token
325 auto const& ttoken = tokens.current();
326 if (ttoken.empty() || ttoken=="--") {
327 std::string error = shortOpt + " requires an argument";
328 throw Tokens::OptionError(std::move(error));
329 }
330 val = tokens.pop();
331 } else {
332 // consume all the rest
333 val = std::string{i, token.end()};
334 i = token.end();
335 }
336 }
337
338 if (tokens.isParsingArgv()) {
339 o->setValue(val ? std::move(val) : value{true});
340 }
341 ret.push_back(o);

Callers 2

parse_atomFunction · 0.85
parse_argvFunction · 0.85

Calls 6

starts_withFunction · 0.85
OptionErrorClass · 0.85
popMethod · 0.80
isParsingArgvMethod · 0.80
setValueMethod · 0.80
argCountMethod · 0.80

Tested by

no test coverage detected