/usr/include/mia-2.2/mia/core/cmdlineparser.hh is in libmia-2.2-dev 2.2.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2015 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/>.
*
*/
#ifndef mia_core_cmdlineparser_hh
#define mia_core_cmdlineparser_hh
//#include <miaconfig.h>
#include <vector>
#include <map>
#include <memory>
#include <sstream>
#include <iostream>
#include <string>
#include <iterator>
#include <mia/core/cmdoption.hh>
#include <mia/core/typedescr.hh>
#include <mia/core/paramoption.hh>
#include <mia/core/dictmap.hh>
#include <mia/core/flagstring.hh>
#include <mia/core/handlerbase.hh>
#include <mia/core/selftestcmdoption.hh>
NS_MIA_BEGIN
/// the string defining the name of the help options
extern EXPORT_CORE const char *g_help_optiongroup;
/** holds the history info of a certain program call */
typedef std::map<std::string, std::string> CHistoryRecord;
/** holds the name of the program and the associated history record */
typedef std::pair<std::string, CHistoryRecord> CHistoryEntry;
/**
\ingroup cmdline
\brief program description entry identifiers
These enumerates identify the parts of the program description
*/
enum EProgramDescriptionEntry {
pdi_group = 0, /*<! The group the program belongs to, in the help the program will be described in this section */
pdi_short = 1, /*<! A sgort description of the program, this will be the head line in the unix man page. */
pdi_description = 2, /*<! The full description of the program */
pdi_example_descr = 3, /*<! description of the example that follows */
pdi_example_code = 4, /*<! The example command line without the program name */
pdi_author = 5 /*<! Author name (if other than main MIA contributer(s) */
};
extern const std::map<EProgramDescriptionEntry, const char *> g_DescriptionEntryNames;
/**
\ingroup cmdline
\brief the map that holds a basic program description
Map of strings to provied information about the program
The following values should be provied:
(1) the program section,
(2) A short description of the program - this will become the whatis entry in the man page,
(3) A long description of the program
(4) A text describing an example usage
(5) The actual exampel call without the program name.
This information is used by the auto-help system to create man-pages and a cross-referenced
help of all the programs.
*/
typedef std::map<EProgramDescriptionEntry, const char *> SProgramDescription;
/**
\ingroup cmdline
\brief Templated version based on CCmdOptionValue for values that can be converted to
and from strings by stream operators
Templated implementation of a command line option to hold a value of type T.
\tparam T value parameter, the type T has to support the operators
"std::ostream& operator << (std::ostream& os, T x)" and
"std::istream& operator >> (std::istream& os, T x)"
must be defined.
f the string passed to the option for translation can not be translation to a
value of type T,
the \a set_value method will throw a \a std::invalid_argument exception
If T is of type template < typename R > std::vector < R >,
the list of N values needs to be given like value1,value2,...,valueN.
*/
template <typename T>
class TCmdOption: public CCmdOption{
public:
/** Constructor of the command option
\param[in,out] val variable to hold the parsed option value - pass in the default value -
exception: \a bool values always default to \a false
\param short_opt short option name (or 0)
\param long_opt long option name (must not be NULL)
\param long_help long help string (must not be NULL)
\param short_help short help string
\param flags support options like required
*/
TCmdOption(T& val, char short_opt, const char *long_opt, const char *long_help,
const char *short_help, CCmdOptionFlags flags = CCmdOptionFlags::none);
private:
virtual void do_get_long_help_xml(std::ostream& os, CXMLElement& parent, HandlerHelpMap& handler_map) const;
virtual bool do_set_value(const char *str_value);
virtual size_t do_get_needed_args() const;
virtual void do_write_value(std::ostream& os) const;
virtual const std::string do_get_value_as_string() const;
T& m_value;
};
/**
\ingroup cmdline
\brief Command line option that translates a string to a set of flags.
*/
class CCmdFlagOption: public CCmdOption{
public:
/** Constructor of the command option
\param[in,out] val variable to hold the parsed option value - pass in the default value -
\param map the lookup table for the option
\param short_opt short option name (or 0)
\param long_opt long option name (must not be NULL)
\param long_help long help string (must not be NULL)
\param short_help short help string
\param flags if this is set to true, extra checking will be done weather
the option is really set
*/
CCmdFlagOption(int& val, const CFlagString& map, char short_opt, const char *long_opt,
const char *long_help, const char *short_help,
CCmdOptionFlags flags = CCmdOptionFlags::none);
private:
virtual bool do_set_value(const char *str_value);
virtual size_t do_get_needed_args() const;
virtual void do_write_value(std::ostream& os) const;
virtual void do_get_long_help(std::ostream& os) const;
virtual const std::string do_get_value_as_string() const;
int& m_value;
const CFlagString m_map;
};
/**
\ingroup cmdline
\brief The class to hold the list of options
This class holds all the user defined and default command line option,
handles the parsing and the printing of help.
\todo the whole command line option class structure needs a code cleanup
*/
class EXPORT_CORE CCmdOptionList {
public:
/**
This enum describes the type of help information that has been requested
*/
enum EHelpRequested {
hr_no = 0, /**< no help has been requested */
hr_help, /**< standard help output has been requested */
hr_help_xml, /**< XML-formatted help has been requested */
hr_usage, /**< a short usage description has been requested */
hr_version, /**< The version information has been requested */
hr_copyright, /**< The long copyright information has been requested */
hr_selftest /**< The selftest was run */
};
/**
Constructor creates the options list and adds some defaut options like
--help, --verbose, --copyright, and --usage
\param description give a description of the program
*/
CCmdOptionList(const SProgramDescription& description);
/// cleanup
~CCmdOptionList();
/** add a new option to the option list
\param opt the option to add
*/
void add(PCmdOption opt);
/** Add a new option to an option group
\param group option group to add this option to
\param opt the option to add
*/
void add(const std::string& group, PCmdOption opt);
/**
Add a selftest option.
The selftest option runs the given self test and then exists. Additional parameters
given on the command line are ignored.
The option is set within the group \a Test and provides the long optionb name \a --selftest.
\param [out] test_result stores the result returned by running by running the test suite
\param callback the test functor that must have CSelftestCallback as a base class
*/
void add_selftest(int& test_result, CSelftestCallback *callback);
/** the work routine, can take the arguemnts straight from \a main
This version parses the command line and allows for additional arguments that can be
read by get_remaining().
\param argc number of arguments
\param args array of arguments strings
\param additional_type will is a help string to describe the type of free parameters
\param additional_help If you use a plug-in handler to process the free parameters
then pass the pointer to the according plug-in handler here, so that the help
system can create proper documentation
*/
EHelpRequested parse(size_t argc, char *args[], const std::string& additional_type,
const CPluginHandlerBase *additional_help = NULL)
__attribute__((warn_unused_result));
/** the work routine, can take the arguemnts straight from \a main
This version parses the command line and allows for additional arguments that can be
read by get_remaining().
\param argc number of arguments
\param args array of arguments strings
\param additional_type will is a help string to describe the type of free parameters
\param additional_help If you use a plug-in handler to process the free
parameters then pass the pointer to the according plug-in handler here,
so that the help system can create proper documentation
*/
EHelpRequested parse(size_t argc, const char *args[], const std::string& additional_type,
const CPluginHandlerBase *additional_help = NULL)
__attribute__((warn_unused_result));
/** the work routine, can take the arguemnts straight from \a main
This version parses doesn't allow additional parameters.
\param argc number of arguments
\param args array of arguments strings
*/
EHelpRequested parse(size_t argc, char *args[]) __attribute__((warn_unused_result));
/** the work routine, can take the arguemnts straight from \a main
This version parses doesn't allow additional parameters.
\param argc number of arguments
\param args array of arguments strings
*/
EHelpRequested parse(size_t argc, const char *args[]) __attribute__((warn_unused_result));
/// \returns a vector of the remaining arguments
const std::vector<std::string>& get_remaining() const;
/** \returns the values of all arguments as a history record to support tracking
of changes on data worked on by \a mia
*/
CHistoryRecord get_values() const;
/**
Set the option group to add subsequent options to
@param group
*/
void set_group(const std::string& group);
/**
Set the output stream for help/usage messages
\param os new output stream
*/
void set_logstream(std::ostream& os);
/** This function sets a flag that indicates that data written
to stdout is an actual result */
void set_stdout_is_result();
private:
EHelpRequested do_parse(size_t argc, const char *args[], bool has_additional,
const CPluginHandlerBase *additional_help)
__attribute__((warn_unused_result));
int handle_shortargs(const char *arg, size_t argc, const char *args[]);
struct CCmdOptionListData *m_impl;
};
// implementation of template classes and functions
/**
@cond INTERNAL
@ingroup traits
@brief Dispatcher to handle the translation of strings to values
Dispatch the translation between a string and value. The standard case assumes that
we have exactly one value to translate and that the type T supports the stream in-and
output operators << and >>
\tparam the type to be translated
\remark most of this is now obsolete since for most types the TParamater class and its
derivatives/instanciantions are used
*/
template <typename T>
struct __dispatch_opt {
/**
Handle the standard initialization, normally do nothing
*/
static void init(T& /*value*/){
}
/**
Translate a string to a value by using the >> operator
\param[in] svalue the value as string
\param[out] value
*/
static bool apply(const char *svalue, T& value) {
std::istringstream sval(svalue);
sval >> value;
while (isspace(sval.peek())) {
char c;
sval >> c;
}
return sval.eof();
}
/// \returns the number of string elements are expected, in the standard case that is one
static size_t size(const T /*value*/) {
return 1;
}
/**
Translate the value to a string for the help output by using the operator << and adding "=" at the beginning
\param os the output stream
\param value the value
*/
static void apply(std::ostream& os, const T& value, bool /*required*/) {
os << "=" << value << " ";
}
/**
translate the value to a string by using the operator >>
\param value the value
\returns the string representation of the value
*/
static const std::string get_as_string(const T& value) {
std::ostringstream os;
os << value;
return os.str();
}
};
/**
@ingroup traits
@brief Dispatcher to handle the translation of vectors of to and from a string
\remark this specialization is still used, since no vector patameter type is defined with TParameter
*/
template <typename T>
struct __dispatch_opt< std::vector<T> > {
static void init(std::vector<T>& /*value*/){
}
static bool apply(const char *svalue, std::vector<T>& value) {
std::string h(svalue);
unsigned int n = 1;
for(std::string::iterator hb = h.begin(); hb != h.end(); ++hb)
if (*hb == ',') {
*hb = ' ';
++n;
}
if (!value.empty()) {
if (n > value.size()) {
throw create_exception<std::invalid_argument>("Expect only ", value.size(),
" coma separated values, but '",
svalue, "' provides ", n);
}
}else{
value.resize(n);
}
std::istringstream sval(h);
auto i = value.begin();
while (!sval.eof()) {
sval >> *i;
++i;
}
return sval.eof();
}
static size_t size(const std::vector<T>& /*value*/) {
return 1;
}
static void apply(std::ostream& os, const std::vector<T>& value, bool required) {
os << "=";
if (value.empty() && required)
os << "[required] ";
else {
for (auto i = value.begin(); i != value.end(); ++i) {
if (i != value.begin())
os << ",";
os << *i;
}
os << " ";
}
}
static const std::string get_as_string(const std::vector<T>& value) {
std::ostringstream os;
for (auto i = value.begin(); i != value.end(); ++i) {
if (i != value.begin())
os << ",";
os << *i;
}
return os.str();
}
};
/**
@ingroup traits
@brief Dispatcher to handle the translation for boolen values
\todo This dispatcher is not really translating anything, since boolean values are flags.
which means, that now, since most other types are handled by TParameter, boolean command line parameters (flags)
should be implemented as a differently.
*/
template <>
struct __dispatch_opt<bool> {
static void init(bool& value) {
value = false;
}
static bool apply(const char */*svalue*/, bool& value) {
value = true;
return true;
}
static size_t size(bool /*value*/) {
return 0;
}
static void apply(std::ostream& /*os*/, bool /*value*/, bool /*required*/) {
}
static const std::string get_as_string(const bool& value) {
return value ? "true" : "false";
}
};
/**
@ingroup traits
@brief Dispatcher to handle the translation for string values
\todo This dispatcher is not really translating anything, since strings are just copied
which means, that now, since most other types are handled by TParameter, string command line parameters
should be implemented a differently.
*/
template <>
struct __dispatch_opt<std::string> {
static void init(std::string& /*value*/) {
}
static bool apply(const char *svalue, std::string& value) {
value = std::string(svalue);
return true;
}
static size_t size(std::string /*value*/) {
return 1;
}
static void apply(std::ostream& os, const std::string& value, bool required) {
if (value.empty())
if (required)
os << "[required] ";
else
os << "=NULL ";
else
os << "=" << value;
}
static const std::string get_as_string(const std::string& value) {
return value;
}
};
/// @endcond
//
// Implementation of the standard option that holds a value
//
template <typename T>
TCmdOption<T>::TCmdOption(T& val, 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)
{
__dispatch_opt<T>::init(m_value);
}
template <typename T>
bool TCmdOption<T>::do_set_value(const char *svalue)
{
return __dispatch_opt<T>::apply(svalue, m_value);
}
template <typename T>
size_t TCmdOption<T>::do_get_needed_args() const
{
return __dispatch_opt<T>::size(m_value);
}
template <typename T>
void TCmdOption<T>::do_write_value(std::ostream& os) const
{
__dispatch_opt<T>::apply( os, m_value, is_required());
}
template <typename T>
void TCmdOption<T>::do_get_long_help_xml(std::ostream& os, CXMLElement& parent,
HandlerHelpMap& /*handler_map*/) const
{
do_get_long_help(os);
parent.set_attribute("type", __type_descr<T>::value);
}
template <typename T>
const std::string TCmdOption<T>::do_get_value_as_string() const
{
return __dispatch_opt<T>::get_as_string(m_value);
}
/**
\ingroup cmdline
\brief Create a standard option that sets a value of the give type
Create a standard option that translates a string given on the command line into a value.
The translation back-end is implemented with CTParameter.
\tparam T the type of thevalue, it must support to be read from (operator >>) and written (operator << ) to a stream,
as the back-end uses these opterator to translate.
\param[in,out] value at input the default value, at output the parsed value if it was given.
\param short_opt short option name (or 0)
\param long_opt long option name (must not be NULL)
\param help long help string (must not be NULL)
\param flags add flags like whether the optionis required to be set
\returns the option warped into a boost::shared_ptr
*/
template <typename T>
PCmdOption make_opt(T& value, const char *long_opt, char short_opt,
const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
return PCmdOption(new CParamOption( short_opt, long_opt, new CTParameter<T>(value, required, help)));
}
/**
\ingroup cmdline
\brief Create an option of a scalar value that can have boundaries
If the given value does not fit into the range an exception will be thrown
\tparam T type of the value to be parsed, supported are float, double, long, int, short, unsigned long, unsigned int, unsigned short.
\param value value variable to hold the parsed option value - pass in the default value -
\param bflags boundary flags
\param bounds vector containing the boundaries of the allowed parameter range (depends on bflags)
\param short_opt short option name (or 0)
\param long_opt long option name (must not be NULL)
\param help long help string (must not be NULL)
\param flags add flags like whether the optionis required to be set
\returns the option warped into a \a boost::shared_ptr
*/
template <typename T>
PCmdOption make_opt(T& value, EParameterBounds bflags, const std::vector<T>& bounds,
const char *long_opt, char short_opt,
const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
return PCmdOption(new CParamOption( short_opt, long_opt,
new TBoundedParameter<T>(value, bflags, bounds, required, help)));
}
/**
\ingroup cmdline
\brief Create an option to set a vector of values,
The parameters on the command line will be separated by ',' and without spaces (or protected from the shell by usinh "".
If the vetcor comes with a pre-allocated size, then the numer of given values must correspond to this size,
Otherwise the size of the vector is deducted from the given command line parameter,.
\tparam T type of the value hold by the vector
\param[in,out] value at input: if not empty, number of expected values and their defaults, at output: the values given on the command line
\param long_opt long option name (must not be NULL)
\param short_opt short option name (or 0)
\param help help string (must not be NULL)
\param flags option flags indicating whether the option is required
\remark be aware that localization of the decimal separator of floating point values is not supported,
it is always the full stop ".".
*/
template <typename T>
PCmdOption make_opt(std::vector<T>& value, const char *long_opt, char short_opt,
const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
return PCmdOption(new TCmdOption<std::vector<T> >(value, short_opt, long_opt, help,
long_opt, flags ));
}
/**
\ingroup cmdline
\brief Create an option that represents a flag.
It is always assumed to be not set if the according option is not given at the command line
\param[out] value the boolean that carries the result
\param long_opt long option name (must not be NULL)
\param short_opt short option name (or 0)
\param help help string (must not be NULL)
\param flags option flags
\returns the option warped into a \a boost::shared_ptr
*/
PCmdOption make_opt(bool& value, const char *long_opt, char short_opt, const char *help,
CCmdOptionFlags flags = CCmdOptionFlags::none);
/**
\ingroup cmdline
\brief Create a table lookup option
Create an option that uses a table to translate between the string given on the command line and
the actual value.
\tparam T the type of the value
\param[in,out] value variable to hold the parsed and translated option value. At entry, the
value must be set to a valid dictionary entry.
\param map the lookup table for the option
\param long_opt long option name (must not be NULL)
\param short_opt short option name (or 0)
\param help help string (must not be NULL)
\returns the option warped into a \a boost::shared_ptr
*/
template <typename T>
PCmdOption make_opt(T& value, const TDictMap<T>& map,
const char *long_opt, char short_opt, const char *help)
{
return PCmdOption(new CParamOption( short_opt, long_opt,
new CDictParameter<T>(value, map, help)));
}
/**
\ingroup cmdline
\brief Create a flag lookup option
\param[in,out] value at input it holds the default value, at output, if the command line option was givem
this value is replaced by the parsed and translated option value,
\param map the lookup table for the option flags
\param long_opt long option name (must not be NULL)
\param short_opt short option name (or 0)
\param long_help long help string (must not be NULL)
\param short_help short help string
\param flags add flags like whether the optionis required to be set
\returns the option warped into a \a boost::shared_ptr
\remark Probably unnecessary
*/
PCmdOption make_opt(int& value, const CFlagString& map, const char *long_opt,
char short_opt, const char *long_help,
const char *short_help,
CCmdOptionFlags flags = CCmdOptionFlags::none);
/**
\ingroup cmdline
\brief Create an option to set a string
Create an option that holds a string
\param[in,out] value at input it holds the default value, at output, if the command line option was givem
this value is replaced by the parsed and translated option value,
\param long_opt long option name (must not be NULL)
\param short_opt short option name (or 0)
\param long_help long help string (must not be NULL)
\param flags add flags like whether the optionis required to be set
\param plugin_hint if the string will later be used to create an object by using plug-in then pass
a pointer to the corresponding plug-in handler to give a hint the help system about this connection.
\returns the option warped into a \a boost::shared_ptr
*/
PCmdOption make_opt(std::string& value, const char *long_opt, char short_opt, const char *long_help,
CCmdOptionFlags flags = CCmdOptionFlags::none, const CPluginHandlerBase *plugin_hint = NULL);
/**
\ingroup cmdline
\brief Create an oĆ¼ption that only takes values from a pre-defined set
Create an option that can only take values from a given set.
\tparam T the type of the value to be set
\param[in,out] value at input it holds the default value, at output, if the command line option was givem
this value is replaced by the parsed and translated option value,
\param valid_set the set of allowed values
\param long_opt long option name (must not be NULL)
\param short_opt short option name (or 0)
\param help long help string (must not be NULL)
\param flags option flags indicating whether the option is required
\returns the option warped into a \a boost::shared_ptr
*/
template <typename T>
PCmdOption make_opt(T& value, const std::set<T>& valid_set,
const char *long_opt, char short_opt,
const char *help,
CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
return PCmdOption(new CParamOption( short_opt, long_opt,
new CSetParameter<T>(value, valid_set, help, required)));
}
/**
\ingroup cmdline
\brief Create a command line option that creates uses a factory to create an object based on the given description
Create a command line option that creates uses a factory to create the value based on the command line arguments given,
\tparam T the non-pointer type of the value
\param[out] value at output, if the command line option was givem
this value is replaced by the parsed and translated option value. If not given but the default_values was given
then this default initializer is used to create the actual value.
If no default value was givem and the option is not given at the command line, then the value is not touched.
\param default_value default value if parameter is not given
\param long_opt long option name
\param short_opt short option char, set to 0 of none givn
\param help the help string for thie option
\param flags indicates whether theoption is required
\remark although passing in an initialized pointer and and empty default_value would act like the the initializer string
was used for the pointer, it is better to pass an empty shared pointer in order to avoid unnecessary initializations
of the pointer is later overwritten.
*/
template <typename T>
PCmdOption make_opt(typename std::shared_ptr<T>& value, const char *default_value, const char *long_opt,
char short_opt, const char *help,
CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
typedef typename FactoryTrait<T>::type F;
return PCmdOption(new CParamOption( short_opt, long_opt,
new TFactoryParameter<F>(value, default_value, required, help)));
}
/**
\ingroup cmdline
\brief Create a command line option that creates uses a factory to create an object based on the given description
Create a command line option that uses a TFactoryPluginHandler to create the actual value hold by a std::unique_ptr.
\tparam T the non-pointer type of the value
\param[out] value at output, if the command line option was given
this value is replaced by the parsed and translated option value. If not given but the default_values was given
then this default initializer is used to create the actual value.
\param default_value default value if parameter is not given
\param long_opt long option name
\param short_opt short option char, set to 0 of none givn
\param help the help string for thie option
\param flags option flags indicating whether the option is required
\remark although passing in an initialized pointer and and empty default_value would act like the the initializer string
was used for the pointer, it is better to pass an empty shared pointer in order to avoid unnecessary initializations
of the pointer is later overwritten.
*/
template <typename T>
PCmdOption make_opt(typename std::unique_ptr<T>& value, const char *default_value, const char *long_opt,
char short_opt, const char *help, CCmdOptionFlags flags = CCmdOptionFlags::none)
{
bool required = has_flag(flags, CCmdOptionFlags::required);
typedef typename FactoryTrait<T>::type F;
return PCmdOption(new CParamOption( short_opt, long_opt,
new TFactoryParameter<F>(value, default_value, required, help)));
}
NS_MIA_END
#endif
|