| 511 | } |
| 512 | |
| 513 | int runCliApplication(int argc, char* argv[]) { |
| 514 | auto cliIndex = cliCommandIndex(argc, argv); |
| 515 | if (cliIndex < 0) { |
| 516 | std::cerr << "Dora CLI command not found.\n"; |
| 517 | return 1; |
| 518 | } |
| 519 | auto asset = cliFindAssetArgument(argc, argv); |
| 520 | if (!asset.error.empty()) { |
| 521 | std::cerr << asset.error << "\n"; |
| 522 | return 1; |
| 523 | } |
| 524 | auto scriptPath = cliFindScript(argc, argv, asset.path); |
| 525 | if (!scriptPath) { |
| 526 | std::cerr << "Dora CLI script cli.lua not found. Set DORA_CLI_SCRIPT or run from a Dora asset root.\n"; |
| 527 | return 1; |
| 528 | } |
| 529 | CliLuaState state{luaL_newstate()}; |
| 530 | if (!state.L) { |
| 531 | std::cerr << "Failed to create Lua state for Dora CLI.\n"; |
| 532 | return 1; |
| 533 | } |
| 534 | cliRegisterLua(state.L, argc, argv, *scriptPath, cliIndex, asset.path); |
| 535 | auto path = scriptPath->string(); |
| 536 | if (luaL_loadfile(state.L, path.c_str()) != LUA_OK) { |
| 537 | std::cerr << lua_tostring(state.L, -1) << "\n"; |
| 538 | return 1; |
| 539 | } |
| 540 | if (lua_pcall(state.L, 0, 1, 0) != LUA_OK) { |
| 541 | std::cerr << lua_tostring(state.L, -1) << "\n"; |
| 542 | return 1; |
| 543 | } |
| 544 | int exitCode = 0; |
| 545 | if (lua_isinteger(state.L, -1)) { |
| 546 | exitCode = s_cast<int>(lua_tointeger(state.L, -1)); |
| 547 | } else if (lua_isboolean(state.L, -1)) { |
| 548 | exitCode = lua_toboolean(state.L, -1) ? 0 : 1; |
| 549 | } |
| 550 | return exitCode; |
| 551 | } |
| 552 | |
| 553 | #else // DORA_CLI_SUPPORTED |
| 554 |
no test coverage detected