24 #include <boost/regex.hpp> 25 #include <boost/foreach.hpp> 67 for (GetoptParameterList::const_iterator opt_it = parameters.begin();
68 opt_it != parameters.end(); ++opt_it)
88 for (GetoptPositionalParameterList::const_iterator opt_it = parameters.begin();
89 opt_it != parameters.end(); ++opt_it)
110 std::cerr <<
"GETOPT WARNING: The commandline option framework is already initialized!" <<
std::endl;
124 for (
int index = 1; index <
argc; ++index)
126 arguments.push_back(argv[index]);
131 size_t positional_parameters_counter = 0;
132 bool extra_cmd_params_reached =
false;
133 boost::regex long_opt_regex(
"--([^-][^=]*)(=(.*))?");
134 boost::regex short_opt_regex(
"-([^-].*)");
137 arg_it != arguments.end(); ++arg_it)
139 if (extra_cmd_params_reached)
143 else if (boost::regex_match(*arg_it, mres, long_opt_regex))
147 ParameterMap::const_iterator find_it =
m_parameters.find(name);
150 if (find_it->second.hasValue())
154 if (mres.size() == 4)
160 std::cerr <<
"Found option " << *arg_it <<
" but the value is missing." <<
std::endl;
175 boost::smatch prefix_res;
179 if (boost::regex_match(name, prefix_res, boost::regex(prefix_it->first +
"(.*)")))
183 if (prefix_it->second.hasValue())
185 if (mres.size() == 4)
191 std::cerr <<
"Found prefix option " << name <<
" but the value is missing." <<
std::endl;
208 std::cerr <<
"Found unknown option " << *arg_it <<
"." <<
std::endl;
219 else if (boost::regex_match(*arg_it, mres, short_opt_regex))
226 if (find_it->second.hasValue())
230 if (arg_it == arguments.end())
232 std::cerr <<
"Found option -" << name <<
" but the value is missing." <<
std::endl;
251 boost::smatch prefix_res;
255 if (boost::regex_match(name, prefix_res, boost::regex(prefix_it->first +
"(.*)")))
259 if (prefix_it->second.hasValue())
263 if (arg_it == arguments.end())
265 std::cerr <<
"Found prefix option " << name <<
" but the value is missing." <<
std::endl;
286 std::cerr <<
"Found unknown option " << *arg_it <<
"." <<
std::endl;
299 extra_cmd_params_reached =
true;
306 positional_parameters_counter++;
313 positional_parameters_counter++;
337 std::cerr <<
"Not all required parameters are given. Aborting." <<
std::endl;
359 for (
int move = check + 1; move <
argc; ++move)
361 argv[move - 1] = argv[move];
404 return find_it->second;
422 return find_it->second;
486 std::cerr <<
"Usage: ";
489 std::cerr <<
" [OPTIONS...]";
495 std::cerr <<
" [<" << param.
name() <<
">]";
499 std::cerr <<
" <" << param.
name() <<
">";
505 if (positional_parameters.size() > 0 )
507 std::cerr <<
"Positional Parameters:" <<
std::endl;
511 std::cerr <<
" <" << param.
name() <<
">" <<
":" << std::endl <<
"\t" 512 << boost::regex_replace(param.
help(), boost::regex(
"\\n"),
"\n\t")
518 for (
int i=0; i<2; ++i)
520 std::cerr << (i == 0 ?
"Generic options:" :
"Options:") << std::endl;
523 bool const is_generic =
524 it->second.option() ==
"configfile" ||
525 it->second.option() ==
"dump-config" ||
526 it->second.option() ==
"help" ||
527 it->second.option() ==
"log-level" ||
528 it->second.option() ==
"quick-debug";
533 if (it->second.shortOption() !=
"")
535 std::cerr <<
"-" << it->second.shortOption();
536 if (it->second.hasValue())
538 std::cerr <<
" <value>";
544 std::cerr <<
"--" << it->second.option();
545 if (it->second.hasValue())
547 std::cerr <<
"=<value>";
551 std::cerr <<
":" << std::endl <<
"\t" 552 << boost::regex_replace(it->second.help(), boost::regex(
"\\n"),
"\n\t")
561 #ifdef _IC_BUILDER_DEPRECATED_STYLE_ 566 Getopt& Getopt::Instance()
600 return initialize(argc, argv, remove_read_arguments);
610 return initialize(argc, argv, cleanup, registration_check);
616 bool Getopt::IsInitialized()
const icl_core::String m_extra_cmd_param_delimiter
all options have to be registered
ParameterMap m_parameters
char ** argv() const
Get the original argv.
ParameterMap m_short_prefix_parameters
void setProgramDescription(icl_core::String const &description)
icl_core::Map< icl_core::String, icl_core::String > m_param_opt
Handles commandline parameters.
ICL_CORE_VC_DEPRECATE_STYLE icl_core::String ExtraCmdParam(size_t index) ICL_CORE_GCC_DEPRECATE_STYLE
icl_core::Vector< icl_core::String > m_extra_cmd_param
icl_core::String m_program_version
icl_core::Vector< icl_core::String > m_param_non_opt
known command line options are removed
ICL_CORE_VC_DEPRECATE_STYLE void AddParameter(const ConfigParameter ¶meter) ICL_CORE_GCC_DEPRECATE_STYLE
icl_core::String paramOpt(const icl_core::String &name) const
icl_core::List< KeyValue > KeyValueList
bool paramOptPresent(const icl_core::String &name) const
bool m_extra_cmd_param_activated
icl_core::String programVersion() const
KeyValueList paramPrefixOpt(const icl_core::String &prefix) const
bool isPrefixOption() const
Check if this is a prefix option.
bool isInitialized() const
icl_core::String programName() const
ICL_CORE_VC_DEPRECATE_STYLE size_t ExtraCmdParamCount() ICL_CORE_GCC_DEPRECATE_STYLE
size_t paramNonOptCount() const
ParameterMap m_short_parameters
ICL_CORE_VC_DEPRECATE_STYLE bool Initialize(int &argc, char *argv[], bool remove_read_arguments) ICL_CORE_GCC_DEPRECATE_STYLE
icl_core::String name() const
Get the option name.
ParameterRegistrationCheck
int & argc()
Get the original argc.
size_t extraCmdParamCount() const
GetoptPositionalParameterList m_required_positional_parameters
icl_core::String help() const
Get the help text.
ParameterMap m_prefix_parameters
void setProgramVersion(icl_core::String const &version)
ThreadStream & endl(ThreadStream &stream)
icl_core::String m_program_description
bool ParamOpt(const icl_core::String &name, typename icl_core::ConvertToRef< T >::ToRef value) ICL_CORE_GCC_DEPRECATE_STYLE
icl_core::String extraCmdParam(size_t index) const
ConfigIterator find(const ::icl_core::String &query)
icl_core::String m_program_name
command line options are left untouched
icl_core::String option() const
Get the long option name.
bool initialize(int &argc, char *argv[], bool remove_read_arguments)
void activateExtraCmdParams(const icl_core::String &delimiter="--")
bool isOptional() const
Get if the parameter is optional.
bool ParamNonOpt(size_t index, typename icl_core::ConvertToRef< T >::ToRef value) ICL_CORE_GCC_DEPRECATE_STYLE
ICL_CORE_VC_DEPRECATE_STYLE size_t ParamNonOptCount() ICL_CORE_GCC_DEPRECATE_STYLE
static Getopt & instance()
icl_core::Map< icl_core::String, KeyValueList > m_prefix_param_opt
void addParameter(const GetoptParameter ¶meter)
bool paramPrefixOptPresent(const icl_core::String &prefix) const
icl_core::String programDescription() const
icl_core::String shortOption() const
Get the short option name.
ICL_CORE_VC_DEPRECATE_STYLE void ActivateExtraCmdParams(const icl_core::String &delimiter="--") ICL_CORE_GCC_DEPRECATE_STYLE
ICL_CORE_VC_DEPRECATE_STYLE bool ParamOptPresent(const icl_core::String &name) ICL_CORE_GCC_DEPRECATE_STYLE
ICL_CORE_VC_DEPRECATE_STYLE void PrintHelp() ICL_CORE_GCC_DEPRECATE_STYLE
GetoptPositionalParameterList m_optional_positional_parameters
icl_core::String paramNonOpt(size_t index) const