@@ -254,6 +254,54 @@ namespace boost { namespace program_options { namespace detail {
254
254
}
255
255
}
256
256
257
+ /* If an key option is followed by a positional option,
258
+ can can consume more tokens (e.g. it's multitoke option),
259
+ give those tokens to it. */
260
+ vector<option> result2;
261
+ for (unsigned i = 0 ; i < result.size (); ++i)
262
+ {
263
+ result2.push_back (result[i]);
264
+ option& opt = result2.back ();
265
+
266
+ if (opt.string_key .empty ())
267
+ continue ;
268
+
269
+ const option_description* xd =
270
+ m_desc->find_nothrow (opt.string_key ,
271
+ (m_style & allow_guessing));
272
+ if (!xd)
273
+ continue ;
274
+
275
+ int min_tokens = xd->semantic ()->min_tokens ();
276
+ int max_tokens = xd->semantic ()->max_tokens ();
277
+ if (min_tokens < max_tokens && opt.value .size () < max_tokens)
278
+ {
279
+ // This option may grab some more tokens.
280
+ // We only allow to grab tokens that are not already
281
+ // recognized as key options.
282
+
283
+ int can_take_more = max_tokens - opt.value .size ();
284
+ int j = i+1 ;
285
+ for (; can_take_more && j < result.size (); --can_take_more, ++j)
286
+ {
287
+ option& opt2 = result[j];
288
+ if (!opt2.string_key .empty ())
289
+ break ;
290
+
291
+ assert (opt2.value .size () == 1 );
292
+
293
+ opt.value .push_back (opt2.value [0 ]);
294
+
295
+ assert (opt2.original_tokens .size () == 1 );
296
+
297
+ opt.original_tokens .push_back (opt2.original_tokens [0 ]);
298
+ }
299
+ i = j-1 ;
300
+ }
301
+ }
302
+ result.swap (result2);
303
+
304
+
257
305
// Assign position keys to positional options.
258
306
int position_key = 0 ;
259
307
for (unsigned i = 0 ; i < result.size (); ++i) {
@@ -327,18 +375,21 @@ namespace boost { namespace program_options { namespace detail {
327
375
invalid_command_line_syntax::extra_parameter);
328
376
}
329
377
330
- max_tokens -= opt.value .size ();
378
+ // If an option wants, at minimum, N tokens, we grab them
379
+ // there and don't care if they look syntactically like an
380
+ // option.
331
381
332
- // A value is optional if min_tokens == 0, but max_tokens > 0.
333
- // If a value is optional, it must appear in opt.value (because
334
- // it was 'adjacent'. Otherwise, remove the expectation of a
335
- // non-adjacent value. (For now, we just check max_tokens == 1,
336
- // as there is no current support for max_tokens>1)
337
- if (min_tokens == 0 && max_tokens == 1 && opt.value .empty ())
338
- --max_tokens;
382
+ if (opt.value .size () <= min_tokens)
383
+ {
384
+ min_tokens -= opt.value .size ();
385
+ }
386
+ else
387
+ {
388
+ min_tokens = 0 ;
389
+ }
339
390
340
391
// Everything's OK, move the values to the result.
341
- for (;!other_tokens.empty () && max_tokens --; ) {
392
+ for (;!other_tokens.empty () && min_tokens --; ) {
342
393
opt.value .push_back (other_tokens[0 ]);
343
394
opt.original_tokens .push_back (other_tokens[0 ]);
344
395
other_tokens.erase (other_tokens.begin ());
0 commit comments