1 // Copyright Vladimir Prus 2002-2004. 2 // Distributed under the Boost Software License, Version 1.0. 3 // (See accompanying file LICENSE_1_0.txt 4 // or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #define BOOST_PROGRAM_OPTIONS_SOURCE 7 #include <boost/program_options/config.hpp> 8 9 #include <boost/config.hpp> 10 11 #include <boost/program_options/detail/cmdline.hpp> 12 #include <boost/program_options/errors.hpp> 13 #include <boost/program_options/value_semantic.hpp> 14 #include <boost/program_options/options_description.hpp> 15 #include <boost/program_options/positional_options.hpp> 16 #include <boost/throw_exception.hpp> 17 18 #include <boost/bind.hpp> 19 20 #include <string> 21 #include <utility> 22 #include <vector> 23 #include <cassert> 24 #include <cstring> 25 #include <cctype> 26 #include <climits> 27 28 #include <cstdio> 29 30 #include <iostream> 31 32 namespace boost { namespace program_options { 33 34 using namespace std; 35 using namespace boost::program_options::command_line_style; 36 37 38 string get_template(kind_t kind)39 invalid_syntax::get_template(kind_t kind) 40 { 41 // Initially, store the message in 'const char*' variable, 42 // to avoid conversion to string in all cases. 43 const char* msg; 44 switch(kind) 45 { 46 case empty_adjacent_parameter: 47 msg = "the argument for option '%canonical_option%' should follow immediately after the equal sign"; 48 break; 49 case missing_parameter: 50 msg = "the required argument for option '%canonical_option%' is missing"; 51 break; 52 case unrecognized_line: 53 msg = "the options configuration file contains an invalid line '%invalid_line%'"; 54 break; 55 // none of the following are currently used: 56 case long_not_allowed: 57 msg = "the unabbreviated option '%canonical_option%' is not valid"; 58 break; 59 case long_adjacent_not_allowed: 60 msg = "the unabbreviated option '%canonical_option%' does not take any arguments"; 61 break; 62 case short_adjacent_not_allowed: 63 msg = "the abbreviated option '%canonical_option%' does not take any arguments"; 64 break; 65 case extra_parameter: 66 msg = "option '%canonical_option%' does not take any arguments"; 67 break; 68 default: 69 msg = "unknown command line syntax error for '%s'"; 70 } 71 return msg; 72 } 73 74 75 }} 76 77 78 namespace boost { namespace program_options { namespace detail { 79 80 // vc6 needs this, but borland chokes when this is added. 81 #if BOOST_WORKAROUND(_MSC_VER, < 1300) 82 using namespace std; 83 using namespace program_options; 84 #endif 85 86 cmdline(const vector<string> & args)87 cmdline::cmdline(const vector<string>& args) 88 { 89 init(args); 90 } 91 cmdline(int argc,const char * const * argv)92 cmdline::cmdline(int argc, const char*const * argv) 93 { 94 #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS) 95 vector<string> args; 96 copy(argv+1, argv+argc+!argc, inserter(args, args.end())); 97 init(args); 98 #else 99 init(vector<string>(argv+1, argv+argc+!argc)); 100 #endif 101 } 102 103 void init(const vector<string> & args)104 cmdline::init(const vector<string>& args) 105 { 106 this->args = args; 107 m_style = command_line_style::default_style; 108 m_desc = 0; 109 m_positional = 0; 110 m_allow_unregistered = false; 111 } 112 113 void style(int style)114 cmdline::style(int style) 115 { 116 if (style == 0) 117 style = default_style; 118 119 check_style(style); 120 this->m_style = style_t(style); 121 } 122 123 void allow_unregistered()124 cmdline::allow_unregistered() 125 { 126 this->m_allow_unregistered = true; 127 } 128 129 void check_style(int style) const130 cmdline::check_style(int style) const 131 { 132 bool allow_some_long = 133 (style & allow_long) || (style & allow_long_disguise); 134 135 const char* error = 0; 136 if (allow_some_long && 137 !(style & long_allow_adjacent) && !(style & long_allow_next)) 138 error = "boost::program_options misconfiguration: " 139 "choose one or other of 'command_line_style::long_allow_next' " 140 "(whitespace separated arguments) or " 141 "'command_line_style::long_allow_adjacent' ('=' separated arguments) for " 142 "long options."; 143 144 if (!error && (style & allow_short) && 145 !(style & short_allow_adjacent) && !(style & short_allow_next)) 146 error = "boost::program_options misconfiguration: " 147 "choose one or other of 'command_line_style::short_allow_next' " 148 "(whitespace separated arguments) or " 149 "'command_line_style::short_allow_adjacent' ('=' separated arguments) for " 150 "short options."; 151 152 if (!error && (style & allow_short) && 153 !(style & allow_dash_for_short) && !(style & allow_slash_for_short)) 154 error = "boost::program_options misconfiguration: " 155 "choose one or other of 'command_line_style::allow_slash_for_short' " 156 "(slashes) or 'command_line_style::allow_dash_for_short' (dashes) for " 157 "short options."; 158 159 if (error) 160 boost::throw_exception(invalid_command_line_style(error)); 161 162 // Need to check that if guessing and long disguise are enabled 163 // -f will mean the same as -foo 164 } 165 166 bool is_style_active(style_t style) const167 cmdline::is_style_active(style_t style) const 168 { 169 return ((m_style & style) ? true : false); 170 } 171 172 void set_options_description(const options_description & desc)173 cmdline::set_options_description(const options_description& desc) 174 { 175 m_desc = &desc; 176 } 177 178 void set_positional_options(const positional_options_description & positional)179 cmdline::set_positional_options( 180 const positional_options_description& positional) 181 { 182 m_positional = &positional; 183 } 184 185 int get_canonical_option_prefix()186 cmdline::get_canonical_option_prefix() 187 { 188 if (m_style & allow_long) 189 return allow_long; 190 191 if (m_style & allow_long_disguise) 192 return allow_long_disguise; 193 194 if ((m_style & allow_short) && (m_style & allow_dash_for_short)) 195 return allow_dash_for_short; 196 197 if ((m_style & allow_short) && (m_style & allow_slash_for_short)) 198 return allow_slash_for_short; 199 200 return 0; 201 } 202 203 vector<option> run()204 cmdline::run() 205 { 206 // The parsing is done by having a set of 'style parsers' 207 // and trying then in order. Each parser is passed a vector 208 // of unparsed tokens and can consume some of them (by 209 // removing elements on front) and return a vector of options. 210 // 211 // We try each style parser in turn, untill some input 212 // is consumed. The returned vector of option may contain the 213 // result of just syntactic parsing of token, say --foo will 214 // be parsed as option with name 'foo', and the style parser 215 // is not required to care if that option is defined, and how 216 // many tokens the value may take. 217 // So, after vector is returned, we validate them. 218 assert(m_desc); 219 220 vector<style_parser> style_parsers; 221 222 if (m_style_parser) 223 style_parsers.push_back(m_style_parser); 224 225 if (m_additional_parser) 226 style_parsers.push_back( 227 boost::bind(&cmdline::handle_additional_parser, this, _1)); 228 229 if (m_style & allow_long) 230 style_parsers.push_back( 231 boost::bind(&cmdline::parse_long_option, this, _1)); 232 233 if ((m_style & allow_long_disguise)) 234 style_parsers.push_back( 235 boost::bind(&cmdline::parse_disguised_long_option, this, _1)); 236 237 if ((m_style & allow_short) && (m_style & allow_dash_for_short)) 238 style_parsers.push_back( 239 boost::bind(&cmdline::parse_short_option, this, _1)); 240 241 if ((m_style & allow_short) && (m_style & allow_slash_for_short)) 242 style_parsers.push_back(boost::bind(&cmdline::parse_dos_option, this, _1)); 243 244 style_parsers.push_back(boost::bind(&cmdline::parse_terminator, this, _1)); 245 246 vector<option> result; 247 while(!args.empty()) 248 { 249 bool ok = false; 250 for(unsigned i = 0; i < style_parsers.size(); ++i) 251 { 252 unsigned current_size = static_cast<unsigned>(args.size()); 253 vector<option> next = style_parsers[i](args); 254 255 // Check that option names 256 // are valid, and that all values are in place. 257 if (!next.empty()) 258 { 259 vector<string> e; 260 for(unsigned k = 0; k < next.size()-1; ++k) { 261 finish_option(next[k], e, style_parsers); 262 } 263 // For the last option, pass the unparsed tokens 264 // so that they can be added to next.back()'s values 265 // if appropriate. 266 finish_option(next.back(), args, style_parsers); 267 for (unsigned j = 0; j < next.size(); ++j) 268 result.push_back(next[j]); 269 } 270 271 if (args.size() != current_size) { 272 ok = true; 273 break; 274 } 275 } 276 277 if (!ok) { 278 option opt; 279 opt.value.push_back(args[0]); 280 opt.original_tokens.push_back(args[0]); 281 result.push_back(opt); 282 args.erase(args.begin()); 283 } 284 } 285 286 /* If an key option is followed by a positional option, 287 can can consume more tokens (e.g. it's multitoken option), 288 give those tokens to it. */ 289 vector<option> result2; 290 for (unsigned i = 0; i < result.size(); ++i) 291 { 292 result2.push_back(result[i]); 293 option& opt = result2.back(); 294 295 if (opt.string_key.empty()) 296 continue; 297 298 const option_description* xd; 299 try 300 { 301 xd = m_desc->find_nothrow(opt.string_key, 302 is_style_active(allow_guessing), 303 is_style_active(long_case_insensitive), 304 is_style_active(short_case_insensitive)); 305 } 306 catch(error_with_option_name& e) 307 { 308 // add context and rethrow 309 e.add_context(opt.string_key, opt.original_tokens[0], get_canonical_option_prefix()); 310 throw; 311 } 312 313 if (!xd) 314 continue; 315 316 if (xd->semantic()->adjacent_tokens_only()) 317 continue; 318 319 unsigned min_tokens = xd->semantic()->min_tokens(); 320 unsigned max_tokens = xd->semantic()->max_tokens(); 321 if (min_tokens < max_tokens && opt.value.size() < max_tokens) 322 { 323 // This option may grab some more tokens. 324 // We only allow to grab tokens that are not already 325 // recognized as key options. 326 327 int can_take_more = max_tokens - static_cast<int>(opt.value.size()); 328 unsigned j = i+1; 329 for (; can_take_more && j < result.size(); --can_take_more, ++j) 330 { 331 option& opt2 = result[j]; 332 if (!opt2.string_key.empty()) 333 break; 334 335 if (opt2.position_key == INT_MAX) 336 { 337 // We use INT_MAX to mark positional options that 338 // were found after the '--' terminator and therefore 339 // should stay positional forever. 340 break; 341 } 342 343 assert(opt2.value.size() == 1); 344 345 opt.value.push_back(opt2.value[0]); 346 347 assert(opt2.original_tokens.size() == 1); 348 349 opt.original_tokens.push_back(opt2.original_tokens[0]); 350 } 351 i = j-1; 352 } 353 } 354 result.swap(result2); 355 356 357 // Assign position keys to positional options. 358 int position_key = 0; 359 for(unsigned i = 0; i < result.size(); ++i) { 360 if (result[i].string_key.empty()) 361 result[i].position_key = position_key++; 362 } 363 364 if (m_positional) 365 { 366 unsigned position = 0; 367 for (unsigned i = 0; i < result.size(); ++i) { 368 option& opt = result[i]; 369 if (opt.position_key != -1) { 370 if (position >= m_positional->max_total_count()) 371 { 372 boost::throw_exception(too_many_positional_options_error()); 373 } 374 opt.string_key = m_positional->name_for_position(position); 375 ++position; 376 } 377 } 378 } 379 380 // set case sensitive flag 381 for (unsigned i = 0; i < result.size(); ++i) { 382 if (result[i].string_key.size() > 2 || 383 (result[i].string_key.size() > 1 && result[i].string_key[0] != '-')) 384 { 385 // it is a long option 386 result[i].case_insensitive = is_style_active(long_case_insensitive); 387 } 388 else 389 { 390 // it is a short option 391 result[i].case_insensitive = is_style_active(short_case_insensitive); 392 } 393 } 394 395 return result; 396 } 397 398 void finish_option(option & opt,vector<string> & other_tokens,const vector<style_parser> & style_parsers)399 cmdline::finish_option(option& opt, 400 vector<string>& other_tokens, 401 const vector<style_parser>& style_parsers) 402 { 403 if (opt.string_key.empty()) 404 return; 405 406 // 407 // Be defensive: 408 // will have no original token if option created by handle_additional_parser() 409 std::string original_token_for_exceptions = opt.string_key; 410 if (opt.original_tokens.size()) 411 original_token_for_exceptions = opt.original_tokens[0]; 412 413 try 414 { 415 // First check that the option is valid, and get its description. 416 const option_description* xd = m_desc->find_nothrow(opt.string_key, 417 is_style_active(allow_guessing), 418 is_style_active(long_case_insensitive), 419 is_style_active(short_case_insensitive)); 420 421 if (!xd) 422 { 423 if (m_allow_unregistered) { 424 opt.unregistered = true; 425 return; 426 } else { 427 boost::throw_exception(unknown_option()); 428 } 429 } 430 const option_description& d = *xd; 431 432 // Canonize the name 433 opt.string_key = d.key(opt.string_key); 434 435 // We check that the min/max number of tokens for the option 436 // agrees with the number of tokens we have. The 'adjacent_value' 437 // (the value in --foo=1) counts as a separate token, and if present 438 // must be consumed. The following tokens on the command line may be 439 // left unconsumed. 440 441 // We don't check if those tokens look like option, or not! 442 443 unsigned min_tokens = d.semantic()->min_tokens(); 444 unsigned max_tokens = d.semantic()->max_tokens(); 445 446 unsigned present_tokens = static_cast<unsigned>(opt.value.size() + other_tokens.size()); 447 448 if (present_tokens >= min_tokens) 449 { 450 if (!opt.value.empty() && max_tokens == 0) 451 { 452 boost::throw_exception( 453 invalid_command_line_syntax(invalid_command_line_syntax::extra_parameter)); 454 } 455 456 // If an option wants, at minimum, N tokens, we grab them there, 457 // when adding these tokens as values to current option we check 458 // if they look like options 459 if (opt.value.size() <= min_tokens) 460 { 461 min_tokens -= static_cast<unsigned>(opt.value.size()); 462 } 463 else 464 { 465 min_tokens = 0; 466 } 467 468 // Everything's OK, move the values to the result. 469 for(;!other_tokens.empty() && min_tokens--; ) 470 { 471 // check if extra parameter looks like a known option 472 // we use style parsers to check if it is syntactically an option, 473 // additionally we check if an option_description exists 474 vector<option> followed_option; 475 vector<string> next_token(1, other_tokens[0]); 476 for (unsigned i = 0; followed_option.empty() && i < style_parsers.size(); ++i) 477 { 478 followed_option = style_parsers[i](next_token); 479 } 480 if (!followed_option.empty()) 481 { 482 original_token_for_exceptions = other_tokens[0]; 483 const option_description* od = m_desc->find_nothrow(other_tokens[0], 484 is_style_active(allow_guessing), 485 is_style_active(long_case_insensitive), 486 is_style_active(short_case_insensitive)); 487 if (od) 488 boost::throw_exception( 489 invalid_command_line_syntax(invalid_command_line_syntax::missing_parameter)); 490 } 491 opt.value.push_back(other_tokens[0]); 492 opt.original_tokens.push_back(other_tokens[0]); 493 other_tokens.erase(other_tokens.begin()); 494 } 495 } 496 else 497 { 498 boost::throw_exception( 499 invalid_command_line_syntax(invalid_command_line_syntax::missing_parameter)); 500 501 } 502 } 503 // use only original token for unknown_option / ambiguous_option since by definition 504 // they are unrecognised / unparsable 505 catch(error_with_option_name& e) 506 { 507 // add context and rethrow 508 e.add_context(opt.string_key, original_token_for_exceptions, get_canonical_option_prefix()); 509 throw; 510 } 511 512 } 513 514 vector<option> parse_long_option(vector<string> & args)515 cmdline::parse_long_option(vector<string>& args) 516 { 517 vector<option> result; 518 const string& tok = args[0]; 519 if (tok.size() >= 3 && tok[0] == '-' && tok[1] == '-') 520 { 521 string name, adjacent; 522 523 string::size_type p = tok.find('='); 524 if (p != tok.npos) 525 { 526 name = tok.substr(2, p-2); 527 adjacent = tok.substr(p+1); 528 if (adjacent.empty()) 529 boost::throw_exception( invalid_command_line_syntax( 530 invalid_command_line_syntax::empty_adjacent_parameter, 531 name, 532 name, 533 get_canonical_option_prefix()) ); 534 } 535 else 536 { 537 name = tok.substr(2); 538 } 539 option opt; 540 opt.string_key = name; 541 if (!adjacent.empty()) 542 opt.value.push_back(adjacent); 543 opt.original_tokens.push_back(tok); 544 result.push_back(opt); 545 args.erase(args.begin()); 546 } 547 return result; 548 } 549 550 551 vector<option> parse_short_option(vector<string> & args)552 cmdline::parse_short_option(vector<string>& args) 553 { 554 const string& tok = args[0]; 555 if (tok.size() >= 2 && tok[0] == '-' && tok[1] != '-') 556 { 557 vector<option> result; 558 559 string name = tok.substr(0,2); 560 string adjacent = tok.substr(2); 561 562 // Short options can be 'grouped', so that 563 // "-d -a" becomes "-da". Loop, processing one 564 // option at a time. We exit the loop when either 565 // we've processed all the token, or when the remainder 566 // of token is considered to be value, not further grouped 567 // option. 568 for(;;) { 569 const option_description* d; 570 try 571 { 572 573 d = m_desc->find_nothrow(name, false, false, 574 is_style_active(short_case_insensitive)); 575 } 576 catch(error_with_option_name& e) 577 { 578 // add context and rethrow 579 e.add_context(name, name, get_canonical_option_prefix()); 580 throw; 581 } 582 583 584 // FIXME: check for 'allow_sticky'. 585 if (d && (m_style & allow_sticky) && 586 d->semantic()->max_tokens() == 0 && !adjacent.empty()) { 587 // 'adjacent' is in fact further option. 588 option opt; 589 opt.string_key = name; 590 result.push_back(opt); 591 592 if (adjacent.empty()) 593 { 594 args.erase(args.begin()); 595 break; 596 } 597 598 name = string("-") + adjacent[0]; 599 adjacent.erase(adjacent.begin()); 600 } else { 601 602 option opt; 603 opt.string_key = name; 604 opt.original_tokens.push_back(tok); 605 if (!adjacent.empty()) 606 opt.value.push_back(adjacent); 607 result.push_back(opt); 608 args.erase(args.begin()); 609 break; 610 } 611 } 612 return result; 613 } 614 return vector<option>(); 615 } 616 617 vector<option> parse_dos_option(vector<string> & args)618 cmdline::parse_dos_option(vector<string>& args) 619 { 620 vector<option> result; 621 const string& tok = args[0]; 622 if (tok.size() >= 2 && tok[0] == '/') 623 { 624 string name = "-" + tok.substr(1,1); 625 string adjacent = tok.substr(2); 626 627 option opt; 628 opt.string_key = name; 629 if (!adjacent.empty()) 630 opt.value.push_back(adjacent); 631 opt.original_tokens.push_back(tok); 632 result.push_back(opt); 633 args.erase(args.begin()); 634 } 635 return result; 636 } 637 638 vector<option> parse_disguised_long_option(vector<string> & args)639 cmdline::parse_disguised_long_option(vector<string>& args) 640 { 641 const string& tok = args[0]; 642 if (tok.size() >= 2 && 643 ((tok[0] == '-' && tok[1] != '-') || 644 ((m_style & allow_slash_for_short) && tok[0] == '/'))) 645 { 646 try 647 { 648 if (m_desc->find_nothrow(tok.substr(1, tok.find('=')-1), 649 is_style_active(allow_guessing), 650 is_style_active(long_case_insensitive), 651 is_style_active(short_case_insensitive))) 652 { 653 args[0].insert(0, "-"); 654 if (args[0][1] == '/') 655 args[0][1] = '-'; 656 return parse_long_option(args); 657 } 658 } 659 catch(error_with_option_name& e) 660 { 661 // add context and rethrow 662 e.add_context(tok, tok, get_canonical_option_prefix()); 663 throw; 664 } 665 } 666 return vector<option>(); 667 } 668 669 vector<option> parse_terminator(vector<string> & args)670 cmdline::parse_terminator(vector<string>& args) 671 { 672 vector<option> result; 673 const string& tok = args[0]; 674 if (tok == "--") 675 { 676 for(unsigned i = 1; i < args.size(); ++i) 677 { 678 option opt; 679 opt.value.push_back(args[i]); 680 opt.original_tokens.push_back(args[i]); 681 opt.position_key = INT_MAX; 682 result.push_back(opt); 683 } 684 args.clear(); 685 } 686 return result; 687 } 688 689 vector<option> handle_additional_parser(vector<string> & args)690 cmdline::handle_additional_parser(vector<string>& args) 691 { 692 vector<option> result; 693 pair<string, string> r = m_additional_parser(args[0]); 694 if (!r.first.empty()) { 695 option next; 696 next.string_key = r.first; 697 if (!r.second.empty()) 698 next.value.push_back(r.second); 699 result.push_back(next); 700 args.erase(args.begin()); 701 } 702 return result; 703 } 704 705 void set_additional_parser(additional_parser p)706 cmdline::set_additional_parser(additional_parser p) 707 { 708 m_additional_parser = p; 709 } 710 711 void extra_style_parser(style_parser s)712 cmdline::extra_style_parser(style_parser s) 713 { 714 m_style_parser = s; 715 } 716 717 718 719 }}} 720