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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
|
/* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2016 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include <miaconfig.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <map>
#include <ctype.h>
#include <stdexcept>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_TBB
#include <tbb/task_scheduler_init.h>
#if defined(__PPC__) && ( TBB_INTERFACE_VERSION < 6101 )
#define TBB_PREFERE_ONE_THREAD 1
#endif
#else
#include <mia/core/parallelcxx11.hh>
#endif
#include <mia/core/xmlinterface.hh>
#include <mia/core/tools.hh>
#include <mia/core/msgstream.hh>
#include <mia/core/cmdstringoption.hh>
#include <mia/core/cmdbooloption.hh>
#include <mia/core/cmdlineparser.hh>
#include <mia/core/fixedwidthoutput.hh>
extern void print_full_copyright(const char *name, const char *author);
NS_MIA_BEGIN
using std::ostream;
using std::ofstream;
using std::ostringstream;
using std::string;
using std::invalid_argument;
using std::runtime_error;
using std::logic_error;
using std::vector;
using std::map;
using std::unique_ptr;
using std::setiosflags;
#define ENTRY(X) {X, #X }
const std::map<EProgramDescriptionEntry, const char *> g_DescriptionEntryNames = {
ENTRY(pdi_group),
ENTRY(pdi_short),
ENTRY(pdi_description),
ENTRY(pdi_example_descr),
ENTRY(pdi_example_code),
ENTRY(pdi_author)
};
struct CCmdOptionListData {
CShortoptionMap short_map;
CLongoptionMap long_map;
typedef vector<PCmdOption> COptionsGroup;
typedef map<string, COptionsGroup > COptionsMap;
COptionsMap options;
COptionsMap::iterator current_group;
vector<string> remaining;
bool help;
string help_xml;
bool usage;
bool version;
bool copyright;
vstream::Level verbose;
int max_threads;
bool m_selftest_run;
bool m_stdout_is_result;
CCmdOptionListData(const SProgramDescription& description);
void add(PCmdOption opt);
void add(const string& group, PCmdOption opt);
CHistoryRecord get_values() const;
void set_current_group(const string& name);
CCmdOption *find_option(const char *key) const;
CCmdOption *find_option(char key) const;
void post_set();
void print_help_xml(const char *progname, const CPluginHandlerBase *additional_help) const;
void print_help(const char *name_help, bool has_additional) const;
void print_usage(const char *name_help) const;
void print_version(const char *name_help) const;
vector<const char *> has_unset_required_options() const;
void set_logstream(std::ostream& os);
string set_description_value(EProgramDescriptionEntry entry,
const SProgramDescription& description, const char *default_value = "");
const char *get_author() const;
string m_program_group;
string m_short_descr;
string m_general_help;
string m_program_example_descr;
string m_program_example_code;
string m_free_parametertype;
string m_author;
ostream *m_log;
};
/**
This option is used to run a selftest.
A program can only have one selftest option and all other options that are not
in the Help & Info section are ignored when the selftest option is set.
The selftest is done within a class derived from the CSelftestOption::Callback
class that overrides the Callback::do_run() method.
*/
class EXPORT_CORE CSelftestOption: public CCmdOption {
public:
/**
Test callback base class.
The tests to be run must be implemented in the "int do_run() const" method, and
it must return 0 if the test run successfull.
*/
private:
friend class CCmdOptionList;
/**
Constructor of the selftest function
*/
CSelftestOption(bool& run, int& test_result, CSelftestCallback *callback);
virtual bool do_set_value(const char *str_value);
virtual void do_write_value(std::ostream& os) const;
virtual size_t do_get_needed_args() const;
std::unique_ptr<CSelftestCallback> m_callback;
int& m_test_result;
bool& m_run;
};
void CCmdOptionListData::set_logstream(ostream& os)
{
m_log = &os;
}
string CCmdOptionListData::set_description_value(EProgramDescriptionEntry entry,
const SProgramDescription& description, const char *default_value)
{
auto id = description.find(entry);
if (id != description.end())
return string( id->second);
else {
auto ed = g_DescriptionEntryNames.find(entry);
assert(ed != g_DescriptionEntryNames.end());
if (!default_value) {
cvwarn() << "Description value '" << ed->second << "' not set\n";
return string("");
} else {
cvdebug() << "Description value '" << ed->second
<< "' not set, using default '" << default_value << "'\n";
return string(default_value);
}
}
}
const char *g_help_optiongroup="Help & Info";
const char *g_default_author = "Gert Wollny";
const char *g_basic_copyright1 = "This software is Copyright (c) ";
const char *g_basic_copyright2 = " 1999-2016 Leipzig, Germany and Madrid, Spain. "
"It comes with ABSOLUTELY NO WARRANTY and you may redistribute it "
"under the terms of the GNU GENERAL PUBLIC LICENSE Version 3 (or later). "
"For more information run the program with the option '--copyright'.\n";
CCmdOptionListData::CCmdOptionListData(const SProgramDescription& description):
help(false),
usage(false),
version(false),
copyright(false),
verbose(vstream::ml_warning),
#if HAVE_TBB
#if TBB_PREFERE_ONE_THREAD
max_threads(1),
#else
max_threads(tbb::task_scheduler_init::automatic),
#endif
#else
max_threads(-1),
#endif
m_selftest_run(false),
m_stdout_is_result(false),
m_log(&std::cout)
{
m_program_group = set_description_value(pdi_group, description);
m_short_descr = set_description_value(pdi_short, description);
m_general_help = set_description_value(pdi_description, description);
m_program_example_descr = set_description_value(pdi_example_descr, description);
m_program_example_code = set_description_value(pdi_example_code, description);
m_author = set_description_value(pdi_author, description, g_default_author);
options[""] = vector<PCmdOption>();
set_current_group(g_help_optiongroup);
add(make_opt(verbose, g_verbose_dict, "verbose", 'V',
"verbosity of output, print messages of given level and higher priorities."
" Supported priorities starting at lowest level are:"));
add(make_opt(copyright, "copyright", 0, "print copyright information", CCmdOptionFlags::nonipype));
add(make_opt(help, "help", 'h', "print this help", CCmdOptionFlags::nonipype));
add(make_opt(help_xml, "help-xml", 0, "print help formatted as XML", CCmdOptionFlags::nonipype));
add(make_opt(usage, "usage", '?', "print a short help", CCmdOptionFlags::nonipype));
add(make_opt(version, "version", 0, "print the version number and exit", CCmdOptionFlags::nonipype));
set_current_group("Processing");
add(make_opt(max_threads, "threads", 0, "Maxiumum number of threads to use for processing,"
"This number should be lower or equal to the number of logical processor cores in the machine. "
"(-1: automatic estimation)."));
set_current_group("");
}
const char *CCmdOptionListData::get_author() const
{
return m_author.c_str();
}
void CCmdOptionListData::add(PCmdOption opt)
{
opt->add_option(short_map, long_map);
current_group->second.push_back(opt);
}
CCmdOption *CCmdOptionListData::find_option(const char *key) const
{
CLongoptionMap::const_iterator i = long_map.find(key);
return (i != long_map.end()) ? i->second : 0;
}
CCmdOption *CCmdOptionListData::find_option(char key) const
{
CShortoptionMap::const_iterator i = short_map.find(key);
return (i != short_map.end()) ? i->second : 0;
}
void CCmdOptionListData::set_current_group(const string& name)
{
current_group = options.find(name);
if (current_group == options.end()) {
options[name] = vector<PCmdOption>();
current_group = options.find(name);
}
}
void CCmdOptionListData::add(const string& group, PCmdOption opt)
{
if (options.find(group) == options.end())
options[group] = vector<PCmdOption>();
options[group].push_back(opt);
opt->add_option(short_map, long_map);
}
CHistoryRecord CCmdOptionListData::get_values() const
{
CHistoryRecord result;
for(auto o_i = options.begin(); o_i != options.end(); ++o_i)
for(auto g_i = o_i->second.begin(); g_i != o_i->second.end(); ++g_i)
result[(*g_i)->get_long_option()] = (*g_i)->get_value_as_string();
return result;
}
void CCmdOptionListData::post_set()
{
for(auto o_i = options.begin(); o_i != options.end(); ++o_i)
for(auto g_i = o_i->second.begin(); g_i != o_i->second.end(); ++g_i)
(*g_i)->post_set();
}
vector<const char *> CCmdOptionListData::has_unset_required_options() const
{
vector<const char *> result;
for(auto o_i = options.begin(); o_i != options.end(); ++o_i)
for(auto g_i = o_i->second.begin(); g_i != o_i->second.end(); ++g_i)
if ((*g_i)->is_required())
result.push_back((*g_i)->get_long_option());
return result;
}
void CCmdOptionListData::print_help_xml(const char *name_help, const CPluginHandlerBase *additional_help) const
{
HandlerHelpMap handler_help_map;
if (additional_help)
additional_help->add_dependend_handlers(handler_help_map);
unique_ptr<CXMLDocument> doc(new CXMLDocument);
auto nodeRoot = doc->create_root_node("program");
auto program_name = nodeRoot->add_child("name");
program_name->set_child_text(name_help);
auto version_string = nodeRoot->add_child("version");
version_string->set_child_text(get_revision());
auto program_group = nodeRoot->add_child("section");
program_group->set_child_text(m_program_group);
auto description = nodeRoot->add_child("description");
description->set_child_text(m_general_help);
auto basic_usage = nodeRoot->add_child("basic_usage");
auto short_descr = nodeRoot->add_child("whatis");
short_descr->set_child_text(m_short_descr);
ostringstream usage_text;
usage_text << " " << name_help << " ";
for (auto g = options.begin(); g != options.end(); ++g) {
// no need to store empty groups
if (g->second.empty())
continue;
auto group = nodeRoot->add_child("group");
group->set_attribute("name", g->first);
for (auto iopt= g->second.begin(); iopt != g->second.end(); ++iopt) {
const CCmdOption& opt = **iopt;
if (opt.get_long_option() == string("help-xml"))
continue;
opt.add_option_xml(*group, handler_help_map);
if (opt.is_required()) {
if (opt.get_short_option())
usage_text << "-" << opt.get_short_option() << " <"
<< opt.get_long_option() << "> ";
else
usage_text << "--" << opt.get_long_option()
<< " <value> ";
}
}
}
if (additional_help) {
auto free_parameters = nodeRoot->add_child("freeparams");
free_parameters->set_attribute("name", additional_help->get_descriptor());
free_parameters->set_attribute("type", "factory");
}
if (m_stdout_is_result) {
nodeRoot->add_child("stdout-is-result");
}
usage_text << "[options]";
if (additional_help)
usage_text << " <PLUGINS:" << additional_help->get_descriptor() <<">";
basic_usage->set_child_text(usage_text.str());
for (auto h = handler_help_map.begin(); h != handler_help_map.end(); ++h)
h->second->get_xml_help(*nodeRoot);
auto example = nodeRoot->add_child("Example");
example->set_child_text(m_program_example_descr);
auto example_code = example->add_child("Code");
example_code->set_child_text(m_program_example_code);
auto cr = nodeRoot->add_child("Author");
cr->set_child_text(m_author);
if (help_xml != "-") {
ofstream xmlfile(help_xml.c_str());
xmlfile << doc->write_to_string_formatted();
xmlfile << std::endl;
if (!xmlfile.good()) {
throw create_exception<runtime_error>("Unable to write '", help_xml, "'");
}
}else{
std::cout << doc->write_to_string_formatted();
}
}
/**
This help printing is a mess ...
*/
void CCmdOptionListData::print_help(const char *name_help, bool has_additional) const
{
const size_t max_opt_width = 23;
size_t max_width = 70;
#ifdef HAVE_SYS_IOCTL_H
// the test cases require a fixed width handling and write to a ostringstream
auto log_to_string = dynamic_cast<ostringstream*>(m_log);
if (!log_to_string) {
struct winsize ws;
if (ioctl(0,TIOCGWINSZ,&ws)==0) {
max_width = ws.ws_col;
if (max_width < max_opt_width + 20)
max_width = max_opt_width + 20;
}
if (max_width > 100)
max_width = 100;
}
#endif
CFixedWidthOutput console(*m_log, max_width);
vector<string> opt_table;
vector<string> help_table;
console.write("\nProgram group: ");
console.write(m_program_group);
console.push_offset(4);
console.write("\n\n");
console.write(m_general_help);
console.pop_offset();
console.newline();
ostringstream usage_text;
usage_text <<name_help << " ";
size_t opt_size = 0;
*m_log << setiosflags(std::ios_base::left);
for (auto i = options.begin(); i != options.end(); ++i) {
if (i->second.empty())
continue;
ostringstream group;
group << "\n" << i->first;
opt_table.push_back(group.str());
help_table.push_back(" ");
for (auto g_i = i->second.begin(); g_i != i->second.end(); ++g_i) {
ostringstream opt;
ostringstream shelp;
const PCmdOption& k = *g_i;
k->get_opt_help(opt);
opt_table.push_back(opt.str());
size_t size = opt.str().size();
if (opt_size < size)
opt_size = size;
k->get_long_help(shelp);
help_table.push_back(shelp.str());
if (k->is_required()) {
if (k->get_short_option())
usage_text << "-" << k->get_short_option() << " <" << k->get_long_option() << "> ";
else
usage_text << "--" << k->get_long_option() << " <value> ";
}
}
}
if (opt_size > max_opt_width)
opt_size = max_opt_width;
usage_text << "[options]";
if (has_additional) {
usage_text << " [<" << m_free_parametertype <<">]"
<< " [<" << m_free_parametertype <<">]"
<< " ...";
}
console.write("\nBasic usage:");
console.push_offset(4);
console.newline();
console.push_offset(4);
console.set_linecontinue(true);
console.write(usage_text.str());
console.set_linecontinue(false);
console.pop_offset();
console.pop_offset();
console.newline();
console.write("\nThe program supports the following command line options:\n");
auto t = opt_table.begin();
for (auto i = help_table.begin(); i != help_table.end(); ++i, ++t) {
console.write(*t);
console.push_offset(opt_size+1);
if (t->length() > opt_size)
console.newline();
else
for (size_t i = t->length(); i <= opt_size; ++i)
console.write(" ");
console.write(*i);
console.pop_offset();
console.newline();
}
if (!m_program_example_descr.empty() && !m_program_example_code.empty()) {
console.newline();
console.push_offset(2);
console.write("Example usage:\n");
console.write(m_program_example_descr);
console.push_offset(2);
console.newline();
console.newline();
console.write(name_help);
console.write(" ");
console.set_linecontinue(true);
console.push_offset(4);
console.write(m_program_example_code);
console.set_linecontinue(false);
console.reset_offset();
console.newline();
}
console.write("\n");
console.push_offset(2);
console.write("Copyright:\n");
console.write(g_basic_copyright1);
console.write(get_author());
console.write(g_basic_copyright2);
console.pop_offset();
console.write("\n");
*m_log << setiosflags(std::ios_base::right);
}
void CCmdOptionListData::print_usage(const char *name) const
{
*m_log << "Usage:\n";
*m_log << " " << name << " ";
for (COptionsMap::const_iterator i = options.begin();
i != options.end(); ++i) {
COptionsGroup::const_iterator g_i = i->second.begin();
COptionsGroup::const_iterator g_e = i->second.end();
while (g_i != g_e) {
const PCmdOption& k = *g_i;
k->print_short_help(*m_log);
++g_i;
}
}
*m_log << '\n';
}
void CCmdOptionListData::print_version(const char *name_help) const
{
*m_log << name_help << " version: " << get_revision() << "\n\n";
*m_log << g_basic_copyright1;
*m_log << get_author();
*m_log << g_basic_copyright2 << "\n";
}
CCmdOptionList::CCmdOptionList(const SProgramDescription& description):
m_impl(new CCmdOptionListData(description))
{
}
void CCmdOptionList::add(PCmdOption opt)
{
m_impl->add(opt);
}
void CCmdOptionList::add(const std::string& table, PCmdOption opt)
{
m_impl->add(table, opt);
}
void CCmdOptionList::add_selftest(int& test_result, CSelftestCallback *callback)
{
m_impl->add("Test", PCmdOption(new CSelftestOption(m_impl->m_selftest_run, test_result, callback)));
}
void CCmdOptionList::set_group(const std::string& group)
{
m_impl->set_current_group(group);
}
int CCmdOptionList::handle_shortargs(const char *arg, size_t remaining_args, const char *args[])
{
cvdebug() << "entering with argument " << *arg << " and " << remaining_args << " remaining arguments\n";
bool bool_options_only = false;
do {
CCmdOption *opt = m_impl->find_option(*arg);
if (!opt ) {
if ( bool_options_only ) {
throw invalid_argument(string("bad flag combination:'-") +
string(arg) + string("'"));
}
return -1;
}
++arg;
size_t nargs = opt->get_needed_args();
if (*arg) {
switch (nargs) {
case 0:
opt->set_value(NULL);
bool_options_only = true;
break;
case 1:
// this handles when the option value is attacted to the flag
// like in -bsomevalue
opt->set_value(arg);
bool_options_only = false;
break;
default:
throw invalid_argument("need a space between option and "
"parameter for multiple parameters");
}
}else {
cvdebug() << "remaining_args = " << remaining_args << ", and " << nargs << " needed\n";
if (remaining_args < nargs ) {
throw create_exception<invalid_argument>("Option -", opt->get_short_option(),
": requires ", nargs, " arguments, but only ",
remaining_args, " remaining.");
}
switch (nargs) {
case 0:
opt->set_value(NULL);
bool_options_only = true;
break;
case 1:
// this handles when the option value is not attacted to the flag
// like in -b somevalue
opt->set_value(args[0]);
bool_options_only = false;
break;
default:// actually currently multiple
throw logic_error("Command line parameters that take more then"
" one parameter are not yet implemented");
}
return nargs;
}
} while (*arg && bool_options_only);
return 0;
}
CCmdOptionList::EHelpRequested
CCmdOptionList::parse(size_t argc, const char *args[])
{
return do_parse(argc, args, false, NULL);
}
CCmdOptionList::EHelpRequested
CCmdOptionList::parse(size_t argc, char *args[])
{
return do_parse(argc, (const char **)args, false, NULL);
}
CCmdOptionList::EHelpRequested
CCmdOptionList::parse(size_t argc, const char *args[], const string& additional_type,
const CPluginHandlerBase *additional_help)
{
m_impl->m_free_parametertype = additional_type;
return do_parse(argc, args, true, additional_help);
}
CCmdOptionList::EHelpRequested
CCmdOptionList::parse(size_t argc, char *args[], const string& additional_type,
const CPluginHandlerBase *additional_help)
{
m_impl->m_free_parametertype = additional_type;
return do_parse(argc, (const char **)args, true, additional_help);
}
void CCmdOptionList::set_stdout_is_result()
{
m_impl->m_stdout_is_result = true;
}
#ifdef HAVE_TBB
struct TBBTaskScheduler {
static const tbb::task_scheduler_init& initialize(int max_threads);
};
const tbb::task_scheduler_init& TBBTaskScheduler::initialize(int max_threads)
{
#if TBB_PREFERE_ONE_THREAD
if (max_threads > 1)
cvwarn() << "You use an old version (interface="
<< TBB_INTERFACE_VERSION
<< ") of Intel Threading Building Blocks."
<< " This version may hang og powerpc when using more than one thread.\n";
#endif
static tbb::task_scheduler_init init(max_threads);
return init;
}
#endif
CCmdOptionList::EHelpRequested
CCmdOptionList::do_parse(size_t argc, const char *args[], bool has_additional,
const CPluginHandlerBase *additional_help)
{
cvdebug() << "Parse " << argc << " parameters \n";
size_t idx = 1;
while (idx < argc ) {
const char *cur_arg = args[idx++];
const char *ccarg = cur_arg;
size_t remaining_args = argc - idx;
// if we don't scan an option, we deal with a left over argument
if (*ccarg != '-') {
m_impl->remaining.push_back(cur_arg);
continue;
}
// we found a singular dash, this is also stored as a left over argument
++ccarg;
if (!*ccarg) {
m_impl->remaining.push_back(cur_arg);
continue;
}
// now check if this is a long option
if( *ccarg == '-') {
cvdebug() << "handle opt '" << ccarg << "'\n";
++ccarg;
CCmdOption *opt = m_impl->find_option(ccarg);
if (opt) {
size_t nargs = opt->get_needed_args();
// currently only one argument value is supported
assert(nargs <= 1);
if (remaining_args < nargs ) {
throw create_exception<invalid_argument>("Option --" , opt->get_long_option()
, ": requires "
, nargs , " arguments, but only " , remaining_args
, " remaining.");
}
opt->set_value(args[idx]);
idx += nargs;
continue;
}
} else {
cvdebug() << "handle shortargs '" << ccarg << "'\n";
int r = handle_shortargs(ccarg, argc - idx, &args[idx]);
if (r >= 0) {
idx += r;
continue;
}
}
// current option was not found
m_impl->remaining.push_back(cur_arg);
}
m_impl->post_set();
const char *name_help = strrchr(args[0], '/');
name_help = name_help ? name_help + 1 : args[0];
if (m_impl->help) {
m_impl->print_help(name_help, has_additional);
return hr_help;
}else if (!m_impl->help_xml.empty()) {
cvdebug() << "Write XML help\n";
m_impl->print_help_xml(name_help, additional_help);
return hr_help_xml;
} else if (m_impl->usage) {
m_impl->print_usage(name_help);
return hr_usage;
} else if (m_impl->version) {
m_impl->print_version(name_help);
return hr_version;
} else if (m_impl->copyright) {
::print_full_copyright(name_help, m_impl->get_author());
return hr_copyright;
} else if (m_impl->m_selftest_run) {
return hr_selftest;
}
cverb.set_verbosity(m_impl->verbose);
if (!has_additional && !m_impl->remaining.empty()) {
ostringstream msg;
msg << "Unknown options given: ";
for (auto i = m_impl->remaining.begin(); m_impl->remaining.end() != i; ++i)
msg << " '" << *i << "' ";
throw invalid_argument(msg.str());
}
auto unset_but_required = m_impl->has_unset_required_options();
if (!unset_but_required.empty()) {
ostringstream msg;
if (unset_but_required.size() > 1)
msg << "Some required options were not set:";
else
msg << "A required options was not set:";
for (auto i = unset_but_required.begin(); unset_but_required.end() != i; ++i)
msg << " '--" << *i << "' ";
msg << "\n";
msg << "run '" << args[0] << " --help' for more information\n";
msg << g_basic_copyright1;
msg << m_impl->get_author();
msg << g_basic_copyright2;
throw invalid_argument(msg.str());
}
#ifdef HAVE_TBB
// the return value and info output is mostly used to make sure the compiler
// doesn't optimize anything away.
const auto& ts = TBBTaskScheduler::initialize(m_impl->max_threads);
cvinfo() << "Task scheduler set to " << (ts.is_active() ? "active":"inactive") << "\n";
#else
CMaxTasks::set_max_tasks(m_impl->max_threads);
#endif
return hr_no;
}
void CCmdOptionList::set_logstream(std::ostream& os)
{
m_impl->set_logstream(os);
}
const vector<string>& CCmdOptionList::get_remaining() const
{
return m_impl->remaining;
}
CCmdOptionList::~CCmdOptionList()
{
delete m_impl;
}
CHistoryRecord CCmdOptionList::get_values() const
{
return m_impl->get_values();
}
CCmdFlagOption::CCmdFlagOption(int& val, const CFlagString& map, char short_opt,
const char *long_opt, const char *long_help,
const char *short_help,
CCmdOptionFlags flags):
CCmdOption(short_opt, long_opt, long_help,short_help, flags),
m_value(val),
m_map(map)
{
}
void CCmdFlagOption::do_write_value(std::ostream& os) const
{
os << m_map.get(m_value);
}
void CCmdFlagOption::do_get_long_help(std::ostream& os) const
{
os << " supported flags:(" <<m_map.get_flagnames() << ")";
}
const std::string CCmdFlagOption::do_get_value_as_string() const
{
return m_map.get(m_value);
}
bool CCmdFlagOption::do_set_value(const char *str_value)
{
m_value = m_map.get(str_value);
return true;
}
size_t CCmdFlagOption::do_get_needed_args() const
{
return 1;
}
CSelftestOption::CSelftestOption(bool& run, int& test_result, CSelftestCallback *callback):
CCmdOption(0, "selftest", "run a self test of the program", 0,
CCmdOptionFlags::nonipype),
m_callback(callback),
m_test_result(test_result),
m_run(run)
{
}
bool CSelftestOption::do_set_value(const char * /* str_value */)
{
assert(m_callback);
m_test_result = m_callback->run();
m_run = true;
return true;
}
void CSelftestOption::do_write_value(std::ostream& /* os */) const
{
}
size_t CSelftestOption::do_get_needed_args() const
{
return 0;
}
PCmdOption EXPORT_CORE make_opt(int& value, const CFlagString& map, const char *long_opt,
char short_opt,const char *long_help,
const char *short_help, CCmdOptionFlags flags)
{
return PCmdOption(new CCmdFlagOption(value, map, short_opt, long_opt,
long_help, short_help, flags ));
}
PCmdOption EXPORT_CORE make_opt(std::string& value, const char *long_opt, char short_opt, const char *long_help,
CCmdOptionFlags flags, const CPluginHandlerBase *plugin_hint)
{
return PCmdOption(new CCmdStringOption(value, short_opt, long_opt, long_help,
flags, plugin_hint));
}
PCmdOption EXPORT_CORE make_opt(bool& value, const char *long_opt, char short_opt, const char *help,
CCmdOptionFlags flags)
{
return PCmdOption(new CCmdBoolOption(value, short_opt, long_opt, help, flags ));
}
NS_MIA_END
|