brbzull0 commented on code in PR #13570:
URL: https://github.com/apache/trafficserver/pull/13570#discussion_r3860627453


##########
src/tscore/ArgParser.cc:
##########
@@ -518,23 +518,56 @@ ArgParser::Command::output_option() const
   }
 }
 
+bool
+ArgParser::Command::is_registered_option(std::string const &token) const
+{
+  if (_option_list.find(token) != _option_list.end() || 
_option_map.find(token) != _option_map.end()) {
+    return true;
+  }
+  // The --option=value form.
+  if (token.size() > 2 && token[0] == '-' && token[1] == '-') {
+    if (auto const pos = token.find_first_of('='); pos != std::string::npos) {
+      return _option_list.find(token.substr(0, pos)) != _option_list.end();
+    }
+  }
+  return false;
+}
+
 // helper method to handle the arguments and put them nicely in arguments
 // can be switched to ts::errata
-static std::string
-handle_args(Arguments &ret, AP_StrVec &args, std::string const &name, unsigned 
arg_num, unsigned &index)
+std::string
+ArgParser::Command::handle_args(Arguments &ret, AP_StrVec &args, std::string 
const &name, unsigned arg_num, unsigned &index) const
 {
   ArgumentData data;
   ret.append(name, data);
   // handle the args
   if (arg_num == MORE_THAN_ZERO_ARG_N || arg_num == MORE_THAN_ONE_ARG_N) {
-    // infinite arguments
-    if (arg_num == MORE_THAN_ONE_ARG_N && args.size() <= index + 1) {
-      return "at least one argument expected by " + name;
-    }
-    for (unsigned j = index + 1; j < args.size(); j++) {
+    // Variable number of arguments. Stop collecting at a token that names 
another option
+    // of this command, so that following options and this command's own 
positional
+    // arguments are left in place for the caller. A "--" token ends option 
recognition,
+    // which is how a value that starts with '-' can be passed.

Review Comment:
   Good catch, the comment overclaimed. Collection stops only at a token naming 
another option (or after `--`), so any other token is still taken as a value, 
including a positional argument of the command. That residual greediness is 
exactly why the third commit adds `AT_MOST_ONE_ARG_N`, which is what an option 
with an optional value should use.
   
   Reworded in 087d7b6506 to describe what the loop actually does and to point 
at `AT_MOST_ONE_ARG_N`. No behavior change.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to