Copilot commented on code in PR #12647:
URL: https://github.com/apache/trafficserver/pull/12647#discussion_r2502211910


##########
src/traffic_ctl/traffic_ctl.cc:
##########
@@ -63,7 +63,15 @@ main([[maybe_unused]] int argc, const char **argv)
 {
   ts::ArgParser parser;
 
-  std::shared_ptr<CtrlCommand> command;
+  std::unique_ptr<CtrlCommand> command;
+
+  auto Command_Execute = [&command]() {
+    if (command) {
+      command->execute();
+    } else {
+      throw std::runtime_error("No command provided");

Review Comment:
   The error message "No command provided" is misleading in this context. This 
error is thrown when `Command_Execute` is invoked but the `command` pointer is 
null. However, this lambda is only invoked through `args.invoke()` at line 256 
after the command has been created and validated at line 252. If this error 
were to occur, it would indicate a logic error in the program flow rather than 
user error. Consider a more descriptive message like "Internal error: Command 
was not properly initialized before execution" or document why this check is 
necessary.
   ```suggestion
         throw std::runtime_error("Internal error: Command was not properly 
initialized before execution");
   ```



##########
src/traffic_ctl/traffic_ctl.cc:
##########
@@ -223,27 +249,8 @@ main([[maybe_unused]] int argc, const char **argv)
     argparser_runroot_handler(args.get("run-root").value(), argv[0]);
     Layout::create();
 
-    if (args.get("config")) {
-      if (args.get("cold")) {
-        // We allow to just change a config file
-        command = std::make_shared<FileConfigCommand>(&args);
-      } else {
-        command = std::make_shared<ConfigCommand>(&args);
-      }
-    } else if (args.get("metric")) {
-      command = std::make_shared<MetricCommand>(&args);
-    } else if (args.get("server")) {
-      command = std::make_shared<ServerCommand>(&args);
-    } else if (args.get("storage")) {
-      command = std::make_shared<StorageCommand>(&args);
-    } else if (args.get("plugin")) {
-      command = std::make_shared<PluginCommand>(&args);
-    } else if (args.get("host")) {
-      command = std::make_shared<HostCommand>(&args);
-    } else if (args.get("hostdb")) {
-      command = std::make_shared<HostDBCommand>(&args);
-    } else if (args.get("rpc")) {
-      command = std::make_shared<DirectRPCCommand>(&args);
+    if (command = create_command(args); !command) {
+      throw std::runtime_error("No valid command provided");

Review Comment:
   The error message "No valid command provided" is ambiguous. This error 
occurs when the user hasn't provided a recognized command argument, but the 
message could be interpreted as a validation error. Consider a more specific 
message like "Unrecognized command. Use --help to see available commands." to 
better guide users.
   ```suggestion
         throw std::runtime_error("Unrecognized command. Use --help to see 
available commands.");
   ```



-- 
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