Files
yaze/src/cli/cli_main.cc
scawful 6990e565b8 feat: Enhance chat command with multiple output formats and improve help documentation
- Updated the chat command to support new output formats: text, markdown, json, and compact.
- Modified the agent configuration to include output format settings.
- Enhanced the command line interface to handle new format options and provide detailed usage instructions.
- Improved the message printing logic in SimpleChatSession to format output based on the selected format.
- Added JSON and Markdown formatting for session metrics and messages.
- Updated help documentation to reflect changes in command usage and available options.
2025-10-04 13:33:19 -04:00

267 lines
7.0 KiB
C++

#include <cstdlib>
#include <iostream>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "absl/flags/declare.h"
#include "absl/flags/flag.h"
#include "absl/strings/str_format.h"
#include "absl/strings/match.h"
#include "cli/modern_cli.h"
#include "cli/tui.h"
#include "yaze_config.h"
ABSL_FLAG(bool, tui, false, "Launch Text User Interface");
ABSL_DECLARE_FLAG(std::string, rom);
ABSL_DECLARE_FLAG(std::string, ai_provider);
ABSL_DECLARE_FLAG(std::string, ai_model);
ABSL_DECLARE_FLAG(std::string, gemini_api_key);
ABSL_DECLARE_FLAG(std::string, ollama_host);
ABSL_DECLARE_FLAG(std::string, prompt_version);
ABSL_DECLARE_FLAG(bool, use_function_calling);
ABSL_FLAG(bool, quiet, false, "Enable quiet mode for simple-chat.");
namespace {
struct ParsedGlobals {
std::vector<char*> positional;
bool show_help = false;
bool show_version = false;
bool list_commands = false;
std::optional<std::string> help_category;
std::optional<std::string> error;
};
ParsedGlobals ParseGlobalFlags(int argc, char* argv[]) {
ParsedGlobals result;
if (argc <= 0 || argv == nullptr) {
result.error = "Invalid argv provided";
return result;
}
result.positional.reserve(argc);
result.positional.push_back(argv[0]);
bool passthrough = false;
for (int i = 1; i < argc; ++i) {
char* current = argv[i];
std::string_view token(current);
if (!passthrough) {
if (token == "--") {
passthrough = true;
continue;
}
if (absl::StartsWith(token, "--help=")) {
std::string category(token.substr(7));
if (!category.empty()) {
result.help_category = category;
} else {
result.show_help = true;
}
continue;
}
if (token == "--help" || token == "-h") {
if (i + 1 < argc && argv[i + 1][0] != '-') {
result.help_category = std::string(argv[++i]);
} else {
result.show_help = true;
}
continue;
}
if (token == "--version") {
result.show_version = true;
continue;
}
if (token == "--tui") {
absl::SetFlag(&FLAGS_tui, true);
continue;
}
if (token == "--list-commands" || token == "--list") {
result.list_commands = true;
continue;
}
if (absl::StartsWith(token, "--quiet=")) {
std::string value(token.substr(8));
bool enable = value.empty() || value == "1" || value == "true";
absl::SetFlag(&FLAGS_quiet, enable);
continue;
}
if (token == "--quiet" || token == "-q") {
absl::SetFlag(&FLAGS_quiet, true);
continue;
}
if (absl::StartsWith(token, "--rom=")) {
absl::SetFlag(&FLAGS_rom, std::string(token.substr(6)));
continue;
}
if (token == "--rom") {
if (i + 1 >= argc) {
result.error = "--rom flag requires a value";
return result;
}
absl::SetFlag(&FLAGS_rom, std::string(argv[++i]));
continue;
}
// AI provider flags
if (absl::StartsWith(token, "--ai_provider=")) {
absl::SetFlag(&FLAGS_ai_provider, std::string(token.substr(14)));
continue;
}
if (token == "--ai_provider") {
if (i + 1 >= argc) {
result.error = "--ai_provider flag requires a value";
return result;
}
absl::SetFlag(&FLAGS_ai_provider, std::string(argv[++i]));
continue;
}
if (absl::StartsWith(token, "--ai_model=")) {
absl::SetFlag(&FLAGS_ai_model, std::string(token.substr(11)));
continue;
}
if (token == "--ai_model") {
if (i + 1 >= argc) {
result.error = "--ai_model flag requires a value";
return result;
}
absl::SetFlag(&FLAGS_ai_model, std::string(argv[++i]));
continue;
}
if (absl::StartsWith(token, "--gemini_api_key=")) {
absl::SetFlag(&FLAGS_gemini_api_key, std::string(token.substr(17)));
continue;
}
if (token == "--gemini_api_key") {
if (i + 1 >= argc) {
result.error = "--gemini_api_key flag requires a value";
return result;
}
absl::SetFlag(&FLAGS_gemini_api_key, std::string(argv[++i]));
continue;
}
if (absl::StartsWith(token, "--ollama_host=")) {
absl::SetFlag(&FLAGS_ollama_host, std::string(token.substr(14)));
continue;
}
if (token == "--ollama_host") {
if (i + 1 >= argc) {
result.error = "--ollama_host flag requires a value";
return result;
}
absl::SetFlag(&FLAGS_ollama_host, std::string(argv[++i]));
continue;
}
if (absl::StartsWith(token, "--prompt_version=")) {
absl::SetFlag(&FLAGS_prompt_version, std::string(token.substr(17)));
continue;
}
if (token == "--prompt_version") {
if (i + 1 >= argc) {
result.error = "--prompt_version flag requires a value";
return result;
}
absl::SetFlag(&FLAGS_prompt_version, std::string(argv[++i]));
continue;
}
if (absl::StartsWith(token, "--use_function_calling=")) {
std::string value(token.substr(23));
absl::SetFlag(&FLAGS_use_function_calling, value == "true" || value == "1");
continue;
}
if (token == "--use_function_calling") {
if (i + 1 >= argc) {
result.error = "--use_function_calling flag requires a value";
return result;
}
std::string value(argv[++i]);
absl::SetFlag(&FLAGS_use_function_calling, value == "true" || value == "1");
continue;
}
}
result.positional.push_back(current);
}
return result;
}
void PrintVersion() {
std::cout << absl::StrFormat("yaze %d.%d.%d", YAZE_VERSION_MAJOR,
YAZE_VERSION_MINOR, YAZE_VERSION_PATCH)
<< std::endl;
}
} // namespace
int main(int argc, char* argv[]) {
ParsedGlobals globals = ParseGlobalFlags(argc, argv);
if (globals.error.has_value()) {
std::cerr << "Error: " << *globals.error << std::endl;
return EXIT_FAILURE;
}
if (globals.show_version) {
PrintVersion();
return EXIT_SUCCESS;
}
// Check if TUI mode is requested
if (absl::GetFlag(FLAGS_tui)) {
yaze::cli::ShowMain();
return EXIT_SUCCESS;
}
yaze::cli::ModernCLI cli;
if (globals.help_category.has_value()) {
cli.PrintCategoryHelp(*globals.help_category);
return EXIT_SUCCESS;
}
if (globals.list_commands) {
cli.PrintCommandSummary();
return EXIT_SUCCESS;
}
if (globals.show_help) {
cli.PrintTopLevelHelp();
return EXIT_SUCCESS;
}
if (globals.positional.size() <= 1) {
cli.PrintTopLevelHelp();
return EXIT_SUCCESS;
}
// Run CLI commands
auto status = cli.Run(static_cast<int>(globals.positional.size()),
globals.positional.data());
if (!status.ok()) {
std::cerr << "Error: " << status.message() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}