| 41 | CommandLineInterface() : verbose_(false) {} |
| 42 | |
| 43 | int Run(int argc, const char* argv[]) { |
| 44 | if (argc < 2) { |
| 45 | PrintUsage(); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | std::string cmd_name = (argc >= 2) ? argv[1] : ""; |
| 50 | |
| 51 | // List of supported handler-based commands |
| 52 | static const std::vector<std::string> handler_commands = { |
| 53 | "run", "delete", "list", "search", "download", "model_info", "serve", "benchmark", "config", "info" |
| 54 | }; |
| 55 | |
| 56 | // Check if this is a handler-based command |
| 57 | bool is_handler_command = std::find(handler_commands.begin(), handler_commands.end(), cmd_name) != handler_commands.end(); |
| 58 | |
| 59 | if (is_handler_command) { |
| 60 | try { |
| 61 | // Initialize dispatcher and parser |
| 62 | mnncli::CommandDispatcher dispatcher; |
| 63 | mnncli::CommandParser parser(argc, argv); |
| 64 | |
| 65 | // Register all handlers |
| 66 | dispatcher.Register(std::make_unique<mnncli::RunCommandHandler>()); |
| 67 | dispatcher.Register(std::make_unique<mnncli::DeleteCommandHandler>()); |
| 68 | dispatcher.Register(std::make_unique<mnncli::ListCommandHandler>()); |
| 69 | dispatcher.Register(std::make_unique<mnncli::SearchCommandHandler>()); |
| 70 | dispatcher.Register(std::make_unique<mnncli::DownloadCommandHandler>()); |
| 71 | dispatcher.Register(std::make_unique<mnncli::ModelInfoCommandHandler>()); |
| 72 | dispatcher.Register(std::make_unique<mnncli::ServeCommandHandler>()); |
| 73 | dispatcher.Register(std::make_unique<mnncli::BenchmarkCommandHandler>()); |
| 74 | dispatcher.Register(std::make_unique<mnncli::ConfigCommandHandler>()); |
| 75 | dispatcher.Register(std::make_unique<mnncli::InfoCommandHandler>()); |
| 76 | |
| 77 | // Get spec for the command |
| 78 | const auto& spec = mnncli::GetSpec(cmd_name); |
| 79 | parser.SetCommandSpec(spec); |
| 80 | |
| 81 | // Parse the command |
| 82 | auto cmd = parser.Parse(); |
| 83 | |
| 84 | // Set verbose logging |
| 85 | mnn::downloader::LogUtils::SetVerbose(cmd.verbose); |
| 86 | |
| 87 | // Dispatch to handler |
| 88 | int result = dispatcher.Dispatch(cmd); |
| 89 | if (result == -2) { |
| 90 | mnncli::UserInterface::ShowError("Internal error: handler not found"); |
| 91 | return 1; |
| 92 | } |
| 93 | return result; |
| 94 | } catch (const std::exception& e) { |
| 95 | std::string err_msg = e.what(); |
| 96 | if (err_msg == "help_requested") { |
| 97 | const auto& spec = mnncli::GetSpec(cmd_name); |
| 98 | std::cout << mnncli::MakeUsage(spec) << "\n"; |
| 99 | return 0; |
| 100 | } |