| 11 | namespace PO { |
| 12 | |
| 13 | cxx20::expected<bool, Error> ArgumentParser::SubCommandDescriptor::parse( |
| 14 | std::FILE *Out, Span<const char *> ProgramNamePrefix, int Argc, |
| 15 | const char *Argv[], int ArgP, const bool &VersionOpt) noexcept { |
| 16 | ProgramNames.reserve(ProgramNamePrefix.size() + 1); |
| 17 | ProgramNames.assign(ProgramNamePrefix.begin(), ProgramNamePrefix.end()); |
| 18 | if (ArgP < Argc) { |
| 19 | ProgramNames.push_back(Argv[ArgP]); |
| 20 | } |
| 21 | ArgumentDescriptor *CurrentDesc = nullptr; |
| 22 | bool FirstNonOption = true; |
| 23 | bool Escaped = false; |
| 24 | auto PositionalIter = PositionalList.cbegin(); |
| 25 | for (int ArgI = ArgP + 1; ArgI < Argc; ++ArgI) { |
| 26 | std::string_view Arg = Argv[ArgI]; |
| 27 | if (!Escaped && Arg.size() >= 2 && Arg[0] == '-') { |
| 28 | if (Arg[1] == '-') { |
| 29 | if (Arg.size() == 2) { |
| 30 | Escaped = true; |
| 31 | } else { |
| 32 | // long option |
| 33 | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
| 34 | CurrentDesc->default_value(); |
| 35 | } |
| 36 | if (auto Res = consume_long_option_with_argument(Arg); !Res) { |
| 37 | return cxx20::unexpected(Res.error()); |
| 38 | } else { |
| 39 | CurrentDesc = *Res; |
| 40 | } |
| 41 | } |
| 42 | } else { |
| 43 | // short options |
| 44 | if (CurrentDesc && CurrentDesc->nargs() == 0) { |
| 45 | CurrentDesc->default_value(); |
| 46 | } |
| 47 | if (auto Res = consume_short_options(Arg); !Res) { |
| 48 | return cxx20::unexpected(Res.error()); |
| 49 | } else { |
| 50 | CurrentDesc = *Res; |
| 51 | } |
| 52 | } |
| 53 | } else if (!Escaped && CurrentDesc) { |
| 54 | if (auto Res = consume_argument(*CurrentDesc, Arg); !Res) { |
| 55 | return cxx20::unexpected(Res.error()); |
| 56 | } |
| 57 | CurrentDesc = nullptr; |
| 58 | } else { |
| 59 | // no more options |
| 60 | if (FirstNonOption) { |
| 61 | FirstNonOption = false; |
| 62 | if (!SubCommandMap.empty()) { |
| 63 | if (auto Iter = SubCommandMap.find(Arg); |
| 64 | Iter != SubCommandMap.end()) { |
| 65 | auto &Child = this[Iter->second]; |
| 66 | Child.SC->select(); |
| 67 | return Child.parse(Out, ProgramNames, Argc, Argv, ArgI, VersionOpt); |
| 68 | } |
| 69 | } |
| 70 | } |
no test coverage detected