aboutsummaryrefslogtreecommitdiff
path: root/bpkg/bpkg.cxx
blob: c94dde9b6efa2dabf9d1ce145a4428da57d8d3c8 (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
// file      : bpkg/bpkg.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cstring>   // strcmp()
#include <iostream>

#include <bpkg/types>
#include <bpkg/utility>

#include <bpkg/diagnostics>
#include <bpkg/bpkg-options>
#include <bpkg/bpkg-version>

// Commands.
//
#include <bpkg/help>

#include <bpkg/pkg-build>
#include <bpkg/pkg-clean>
#include <bpkg/pkg-configure>
#include <bpkg/pkg-disfigure>
#include <bpkg/pkg-drop>
#include <bpkg/pkg-fetch>
#include <bpkg/pkg-install>
#include <bpkg/pkg-purge>
#include <bpkg/pkg-status>
#include <bpkg/pkg-test>
#include <bpkg/pkg-uninstall>
#include <bpkg/pkg-unpack>
#include <bpkg/pkg-update>
#include <bpkg/pkg-verify>

#include <bpkg/cfg-add>
#include <bpkg/cfg-create>
#include <bpkg/cfg-fetch>

#include <bpkg/rep-info>
#include <bpkg/rep-create>

using namespace std;
using namespace bpkg;

// Initialize the command option class O with the common options and then
// parse the rest of the command line placing non-option arguments to args.
// Once this is done, use the "final" values of the common options to do
// global initializations (verbosity level, etc).
//
template <typename O>
static O
parse (const common_options& co, cli::scanner& scan, strings& args)
{
  O o;
  static_cast<common_options&> (o) = co;

  // We want to be able to specify options and arguments in any order (it is
  // really handy to just add -v at the end of the command line).
  //
  for (bool opt (true); scan.more (); )
  {
    if (opt)
    {
      // If we see first "--", then we are done parsing options.
      //
      if (strcmp (scan.peek (), "--") == 0)
      {
        scan.next ();
        opt = false;
        continue;
      }

      // Parse the next chunk of options until we reach an argument (or eos).
      //
      o.parse (scan);

      if (!scan.more ())
        break;

      // Fall through.
    }

    args.push_back (scan.next ());
  }

  // Global initializations.
  //

  // Diagnostics verbosity.
  //
  verb = o.verbose_specified () ? o.verbose () : o.v () ? 2 : o.q () ? 0 : 1;

  return o;
}

int
main (int argc, char* argv[])
try
{
  using namespace cli;

  argv_file_scanner scan (argc, argv, "--options-file");

  // First parse common options and --version/--help.
  //
  options o;
  o.parse (scan, unknown_mode::stop);

  if (o.version ())
  {
    cout << "bpkg " << BPKG_VERSION_STR << endl
         << "libbpkg " << LIBBPKG_VERSION_STR << endl
         << "libbutl " << LIBBUTL_VERSION_STR << endl
         << "Copyright (c) 2014-2016 Code Synthesis Ltd" << endl
         << "This is free software released under the MIT license." << endl;
    return 0;
  }

  strings argsv; // To be filled by parse() above.
  vector_scanner args (argsv);

  const common_options& co (o);

  if (o.help ())
    return help (parse<help_options> (co, scan, argsv), "", nullptr);

  // The next argument should be a command.
  //
  if (!scan.more ())
    fail << "bpkg command expected" <<
      info << "run 'bpkg help' for more information";

  int cmd_argc (2);
  char* cmd_argv[] {argv[0], const_cast<char*> (scan.next ())};
  commands cmd;
  cmd.parse (cmd_argc, cmd_argv, true, unknown_mode::stop);

  if (cmd_argc != 1)
    fail << "unknown bpkg command/option '" << cmd_argv[1] << "'" <<
      info << "run 'bpkg help' for more information";

  // If the command is 'help', then what's coming next is another
  // command. Parse it into cmd so that we only need to check for
  // each command in one place.
  //
  bool h (cmd.help ());
  help_options ho;

  if (h)
  {
    ho = parse<help_options> (co, scan, argsv);

    if (args.more ())
    {
      cmd_argc = 2;
      cmd_argv[1] = const_cast<char*> (args.next ());

      // First see if this is a command.
      //
      cmd = commands (); // Clear the help option.
      cmd.parse (cmd_argc, cmd_argv, true, unknown_mode::stop);

      // If not, then it got to be a help topic.
      //
      if (cmd_argc != 1)
        return help (ho, cmd_argv[1], nullptr);
    }
    else
      return help (ho, "", nullptr);
  }

  // Handle commands.
  //
  int r (1);
  for (;;)
  {
    // help
    //
    if (cmd.help ())
    {
      assert (h);
      r = help (ho, "help", print_bpkg_help_usage);
      break;
    }

    // Commands.
    //
    // if (cmd.pkg_verify ())
    // {
    //  if (h)
    //    r = help (ho, "pkg-verify", print_bpkg_pkg_verify_usage);
    //  else
    //    r = pkg_verify (parse<pkg_verify_options> (co, scan, argsv), args);
    //
    //  break;
    // }
    //
#define COMMAND_IMPL(NP, SP, CMD)                                       \
    if (cmd.NP##CMD ())                                                 \
    {                                                                   \
      if (h)                                                            \
        r = help (ho, SP#CMD, print_bpkg_##NP##CMD##_usage);            \
      else                                                              \
        r = NP##CMD (parse<NP##CMD##_options> (co, scan, argsv), args); \
                                                                        \
      break;                                                            \
    }

    // pkg-* commands
    //
#define PKG_COMMAND(CMD) COMMAND_IMPL(pkg_, "pkg-", CMD)

    PKG_COMMAND (build);
    PKG_COMMAND (clean);
    PKG_COMMAND (configure);
    PKG_COMMAND (disfigure);
    PKG_COMMAND (drop);
    PKG_COMMAND (fetch);
    PKG_COMMAND (install);
    PKG_COMMAND (purge);
    PKG_COMMAND (status);
    PKG_COMMAND (test);
    PKG_COMMAND (uninstall);
    PKG_COMMAND (unpack);
    PKG_COMMAND (update);
    PKG_COMMAND (verify);

    // cfg-* commands
    //
#define CFG_COMMAND(CMD) COMMAND_IMPL(cfg_, "cfg-", CMD)

    CFG_COMMAND (add);
    CFG_COMMAND (create);
    CFG_COMMAND (fetch);

    // rep-* commands
    //
#define REP_COMMAND(CMD) COMMAND_IMPL(rep_, "rep-", CMD)

    REP_COMMAND (info);
    REP_COMMAND (create);

    assert (false);
    fail << "unhandled command";
  }

  if (r != 0)
    return r;

  // Warn if args contain some leftover junk. We already successfully
  // performed the command so failing would probably be misleading.
  //
  if (args.more ())
  {
    diag_record dr;
    dr << warn << "ignoring unexpected argument(s)";
    while (args.more ())
      dr << " '" << args.next () << "'";
  }

  return 0;
}
catch (const failed&)
{
  return 1; // Diagnostics has already been issued.
}
catch (const cli::exception& e)
{
  error << e;
  return 1;
}
/*
catch (const std::exception& e)
{
  error << e.what ();
  return 1;
}
*/