aboutsummaryrefslogtreecommitdiff
path: root/tests/default-options/driver.cxx
blob: 009cddb58d68630e52822043a319176212629d75 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// file      : tests/default-options/driver.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <string>
#include <vector>
#include <iostream>
#include <stdexcept> // invalid_argument
#endif

// Other includes.

#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.path;
import butl.path_io;
import butl.optional;
import butl.fdstream;
import butl.default_options;
#else
#include <libbutl/path.mxx>
#include <libbutl/path-io.mxx>
#include <libbutl/utility.mxx>         // eof()
#include <libbutl/optional.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/default-options.mxx>
#endif

using namespace std;
using namespace butl;

// Usage: argv[0] [-f <file>] [-d <start-dir>] [-s <sys-dir>] [-h <home-dir>]
//                [-x <extra-dir>] [-a] [-e] [-t] <cmd-options>
//
// Parse default options files, merge them with the command line options, and
// print the resulting options to STDOUT one per line. Note that the options
// instance is a vector of arbitrary strings.
//
// -f
//    Default options file name. Can be specified multiple times.
//
// -d
//    Directory to start the default options files search from. Can be
//    specified multiple times, in which case a common start (parent)
//    directory is deduced.
//
// -s
//    System directory.
//
// -h
//    Home directory.
//
// -x
//    Extra directory.
//
// -a
//    Default arguments are allowed in the options files.
//
// -e
//    Print the default options entries (rather than the merged options) to
//    STDOUT one per line in the following format:
//
//    <file>,<space-separated-options>,<remote>
//
// -t
//    Trace the default options files search to STDERR.
//
int
main (int argc, const char* argv[])
{
  using butl::optional;

  class scanner
  {
  public:
    scanner (const string& f, const string& option)
        : option_ (option) {load (path (f));}

    bool
    more ()
    {
      return i_ < args_.size ();
    }

    string
    peek ()
    {
      assert (more ());
      return args_[i_];
    }

    string
    next ()
    {
      assert (more ());
      return args_[i_++];
    }

  private:
    void
    load (const path& f)
    {
      ifdstream is (f, fdopen_mode::in, ifdstream::badbit);

      for (string l; !eof (getline (is, l)); )
      {
        if (option_ && *option_ == l)
        {
          assert (!eof (getline (is, l)));

          // If the path of the file being parsed is not simple and the path
          // of the file that needs to be loaded is relative, then complete
          // the latter using the former as a base.
          //
          path p (l);

          if (!f.simple () && p.relative ())
            p = f.directory () / p;

          load (p);
        }
        else
          args_.push_back (move (l));
      }
    }

  private:
    optional<string> option_;
    vector<string> args_;
    size_t i_ = 0;
  };

  enum class unknow_mode
  {
    stop,
    fail
  };

  class options: public vector<string>
  {
  public:
    bool
    parse (scanner& s, unknow_mode, unknow_mode m)
    {
      bool r (false);
      while (s.more ())
      {
        string a (s.peek ());

        if (a.compare (0, 2, "--") != 0)
        {
          switch (m)
          {
          case unknow_mode::stop: return r;
          case unknow_mode::fail: throw invalid_argument (a);
          }
        }

        s.next ();

        if (a == "--no-default-options")
          no_default_options_ = true;

        push_back (move (a));
        r = true;
      }
      return r;
    }

    void
    merge (const options& o)
    {
      insert (end (), o.begin (), o.end ());
    }

    bool
    no_default_options () const noexcept
    {
      return no_default_options_;
    }

  private:
    bool no_default_options_ = false;
  };

  // Parse and validate the arguments.
  //
  default_options_files fs;
  optional<dir_path> sys_dir;
  optional<dir_path> home_dir;
  optional<dir_path> extra_dir;
  bool args (false);
  vector<dir_path> dirs;
  options cmd_ops;
  vector<string> cmd_args;
  bool print_entries (false);
  bool trace (false);

  for (int i (1); i != argc; ++i)
  {
    string a (argv[i]);

    if (a == "-f")
    {
      assert (++i != argc);
      fs.files.push_back (path (argv[i]));
    }
    else if (a == "-d")
    {
      assert (++i != argc);
      dirs.emplace_back (argv[i]);
    }
    else if (a == "-s")
    {
      assert (++i != argc);
      sys_dir = dir_path (argv[i]);
    }
    else if (a == "-h")
    {
      assert (++i != argc);
      home_dir = dir_path (argv[i]);
    }
    else if (a == "-x")
    {
      assert (++i != argc);
      extra_dir = dir_path (argv[i]);
    }
    else if (a == "-a")
    {
      args = true;
    }
    else if (a == "-e")
    {
      print_entries = true;
    }
    else if (a == "-t")
    {
      trace = true;
    }
    else if (a.compare (0, 2, "--") == 0)
      cmd_ops.push_back (move (a));
    else
      cmd_args.push_back (move (a));
  }

  // Deduce a common start directory.
  //
  fs.start = default_options_start (home_dir, dirs.begin (), dirs.end ());

  // Load and print the default options.
  //
  default_options<options> def_ops;

  try
  {
    def_ops = load_default_options<options, scanner, unknow_mode> (
      sys_dir,
      home_dir,
      extra_dir,
      fs,
      [trace] (const path& f, bool remote, bool overwrite)
      {
        if (trace)
          cerr << (overwrite ? "overwriting " : "loading ")
               << (remote ? "remote " : "local ") << f << endl;
      },
      "--options-file",
      args);
  }
  catch (const invalid_argument& e)
  {
    cerr << "error: unexpected argument '" << e.what () << "'" << endl;
    return 1;
  }

  if (print_entries)
  {
    for (const default_options_entry<options>& e: def_ops)
    {
      cout << e.file << ',';

      for (const string& o: e.options)
      {
        if (&o != &e.options[0])
          cout << ' ';

        cout << o;
      }

      if (args)
      {
        cout << "|";

        for (const string& a: e.arguments)
        {
          if (&a != &e.arguments[0])
            cout << ' ';

          cout << a;
        }

      }

      cout << (e.remote ? ",true" : ",false") << endl;
    }
  }

  // Merge the options and print the result.
  //
  if (!print_entries)
  {
    options ops (merge_default_options (def_ops, cmd_ops));

    for (const string& o: ops)
      cout << o << endl;

    if (args)
    {
      vector<string> as (merge_default_arguments (def_ops, cmd_args));

      for (const string& a: as)
        cout << a << endl;
    }
  }

  return 0;
}