aboutsummaryrefslogtreecommitdiff
path: root/bpkg/types-parsers.txx
blob: f187fc763b3b862103c19b56e4212adee1ca4091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// file      : bpkg/types-parsers.txx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

namespace bpkg
{
  namespace cli
  {
    template <const char* const* Q, typename V>
    void parser<qualified_option<Q, V>>::
    parse (qualified_option<Q, V>& x, bool& xs, scanner& s)
    {
      xs = true;
      const char* o (s.next ());

      if (!s.more ())
        throw missing_value (o);

      string v (s.next ());

      // Extract the qualifier from the option value.
      //
      string qv;
      size_t n (v.find (':'));

      if (n != string::npos)
      {
        const char* const* q (Q);
        for (; *q != nullptr; ++q)
        {
          if (v.compare (0, n, *q) == 0)
          {
            qv = *q;
            v = string (v, n + 1);
            break;
          }
        }

        // Fail it the qualifier is not recognized, unless it is a special
        // empty qualifier.
        //
        if (*q == nullptr && n != 0)
          throw invalid_value (o, v);
      }

      // Parse the value for the extracted (possibly empty) qualifier.
      //
      int ac (2);
      char* av[] = {const_cast<char*> (o), const_cast<char*> (v.c_str ())};
      bool dummy;

      {
        argv_scanner s (0, ac, av);
        parser<V>::parse (x[qv], dummy, s);
      }

      // Parse an unqualified value for all qualifiers.
      //
      if (qv.empty ())
      {
        for (const char* const* q (Q); *q != nullptr; ++q)
        {
          argv_scanner s (0, ac, av);
          parser<V>::parse (x[*q], dummy, s);
        }
      }
    }

    template <const char* const* Q, typename V>
    void parser<qualified_option<Q, V>>::
    merge (qualified_option<Q, V>& b, const qualified_option<Q, V>& a)
    {
      for (const auto& o: a)
      {
        auto i (b.find (o.first));

        if (i != b.end ())
          i->second = o.second;
        else
          b.emplace (o.first, o.second);
      }
    }
  }
}