fix: Update option parsing to allow = with short options

This commit is contained in:
Nitin Kumar 2026-01-22 10:44:47 +05:30
parent 61a90273be
commit c647dd4cc1
1 changed files with 33 additions and 7 deletions

View File

@ -780,7 +780,20 @@ const char* const falsy_pattern =
"(f|F)(alse)?|0";
CXXOPTS_LINKONCE
const char* const option_pattern =
"--([[:alnum:]][-_[:alnum:]\\.]+)(=(.*))?|-([[:alnum:]].*)";
"--([[:alnum:]][-_[:alnum:]\\.]+)(=(.*))?|-([[:alnum:]])((=(.*))|(.*))";
// <-------Long Option--------------------> <-----Short Option------->
// Groups :
// <---------1------------------><--2--> <--4--------><-----5------>
// <-3> <--6--> <-8>
// <-7>
const int LONG_NAME_IDX=1;
const int LONG_MATCH_IDX=2;
const int LONG_MATCH_VALUE_IDX=3;
const int SHORT_NAME_IDX=4;
const int SHORT_MATCH_IDX=6;
const int SHORT_MATCH_VALUE_IDX=7;
const int SHORT_GROUPING_IDX=8;
CXXOPTS_LINKONCE
const char* const option_specifier_pattern =
"([[:alnum:]][-_[:alnum:]\\.]*)(,[ ]*[[:alnum:]][-_[:alnum:]]*)*";
@ -862,13 +875,21 @@ inline ArguDesc ParseArgument(const char *arg, bool &matched)
ArguDesc argu_desc;
if (matched) {
argu_desc.arg_name = result[1].str();
argu_desc.set_value = result[2].length() > 0;
argu_desc.value = result[3].str();
if (result[4].length() > 0)
if(result[LONG_NAME_IDX].length() > 0) {
argu_desc.arg_name = result[LONG_NAME_IDX].str();
argu_desc.set_value = result[LONG_MATCH_IDX].length() > 0;
argu_desc.value = result[LONG_MATCH_VALUE_IDX].str();
}
else if (result[SHORT_NAME_IDX].length() > 0)
{
argu_desc.grouping = true;
argu_desc.arg_name = result[4].str();
argu_desc.arg_name = result[SHORT_NAME_IDX].str();
if(result[SHORT_MATCH_IDX].length() > 0){
argu_desc.set_value = true;
argu_desc.value = result[SHORT_MATCH_VALUE_IDX].str();
} else {
argu_desc.arg_name += result[SHORT_GROUPING_IDX].str();
}
}
}
@ -2579,7 +2600,12 @@ OptionParser::parse(int argc, const char* const* argv)
if (i + 1 == s.size())
{
//it must be the last argument
checked_parse_arg(argc, argv, current, value, name);
if (argu_desc.set_value) {
parse_option(value, name, argu_desc.value);
}
else{
checked_parse_arg(argc, argv, current, value, name);
}
}
else if (value->value().has_implicit())
{