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

Method docopt_parse

docopt.cpp:611–659  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

609}
610
611DOCOPT_INLINE
612docopt::Options
613docopt::docopt_parse(std::string const& doc,
614 std::vector<std::string> const& argv,
615 bool help,
616 bool version,
617 bool options_first)
618{
619 Required pattern;
620 std::vector<Option> options;
621 try {
622 std::tie(pattern, options) = create_pattern_tree(doc);
623 } catch (Tokens::OptionError const& error) {
624 throw DocoptLanguageError(error.what());
625 }
626
627 PatternList argv_patterns;
628 try {
629 argv_patterns = parse_argv(Tokens(argv), options, options_first);
630 } catch (Tokens::OptionError const& error) {
631 throw DocoptArgumentError(error.what());
632 }
633
634 extras(help, version, argv_patterns);
635
636 std::vector<std::shared_ptr<LeafPattern>> collected;
637 bool matched = pattern.fix().match(argv_patterns, collected);
638 if (matched && argv_patterns.empty()) {
639 docopt::Options ret;
640
641 // (a.name, a.value) for a in (pattern.flat() + collected)
642 for (auto* p : pattern.leaves()) {
643 ret[p->name()] = p->getValue();
644 }
645
646 for (auto const& p : collected) {
647 ret[p->name()] = p->getValue();
648 }
649
650 return ret;
651 }
652
653 if (matched) {
654 std::string leftover = join(argv.begin(), argv.end(), ", ");
655 throw DocoptArgumentError("Unexpected argument: " + leftover);
656 }
657
658 throw DocoptArgumentError("Arguments did not match expected patterns"); // BLEH. Bad error.
659}
660
661DOCOPT_INLINE
662docopt::Options

Callers

nothing calls this directly

Calls 9

create_pattern_treeFunction · 0.85
DocoptLanguageErrorClass · 0.85
parse_argvFunction · 0.85
TokensClass · 0.85
DocoptArgumentErrorClass · 0.85
extrasFunction · 0.85
joinFunction · 0.85
leavesMethod · 0.80
matchMethod · 0.45

Tested by

no test coverage detected