From c647dd4cc1d6265f6a80e064e7d9d4e9b5680490 Mon Sep 17 00:00:00 2001 From: Nitin Kumar <59679977+lazysegtree@users.noreply.github.com> Date: Thu, 22 Jan 2026 10:44:47 +0530 Subject: [PATCH] fix: Update option parsing to allow = with short options --- include/cxxopts.hpp | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 537b2e7..6b9f0fd 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -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()) {