utils/args_utils.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "args_utils.hpp" | ||
| 2 | #include "utils/exit_request.hpp" | ||
| 3 | |||
| 4 | /** | ||
| 5 | * Initializes the ArgsUtils instance with command-line argument handlers. | ||
| 6 | * Sets up a map of argument keys to handler functions that parse and assign values to ParsedArgs. | ||
| 7 | */ | ||
| 8 | 20 | ArgsUtils::ArgsUtils() { | |
| 9 | 200 | this->handlers = { | |
| 10 | 4 | {"--model", [&](const std::string& val, struct ParsedArgs* args) { args->model = val; }}, | |
| 11 | 1 | {"--embedding_model", [&](const std::string& val, struct ParsedArgs* args) { args->embedding_model = val; }}, | |
| 12 | 2 | {"--prompt", [&](const std::string& val, struct ParsedArgs* args) { args->prompt = val; }}, | |
| 13 | 5 | {"--stream", [&](const std::string& val, struct ParsedArgs* args) { args->stream = val == "true"; }}, | |
| 14 | 5 | {"--rag", [&](const std::string& val, struct ParsedArgs* args) { args->rag = val == "true"; }}, | |
| 15 | 5 | {"--interactive", [&](const std::string& val, struct ParsedArgs* args) { args->interactive = val == "true"; }}, | |
| 16 | 5 | {"--dark_theme", [&](const std::string& val, struct ParsedArgs* args) { args->dark_theme = val == "true"; }}, | |
| 17 | {"--skip_embedding", | ||
| 18 | 5 | [&](const std::string& val, struct ParsedArgs* args) { args->skip_embedding = val == "true"; }}, | |
| 19 | 5 | {"--help", [&](const std::string& val, struct ParsedArgs* args) { args->help = val == "true"; }}, | |
| 20 | 200 | }; | |
| 21 | 40 | } | |
| 22 | |||
| 23 | /** | ||
| 24 | * Parses command-line arguments and returns a ParsedArgs struct. | ||
| 25 | * Initializes all arguments with default values, then overrides them with values from the command line. | ||
| 26 | * Expects arguments in the format --key=value. | ||
| 27 | * | ||
| 28 | * @param argc Pointer to argument count from main() | ||
| 29 | * @param argv Array of argument strings from main() | ||
| 30 | * @return ParsedArgs struct containing all parsed argument values | ||
| 31 | */ | ||
| 32 | 37 | struct ParsedArgs ArgsUtils::parse_args(const int argc, const char* const argv[]) const { | |
| 33 | 37 | struct ParsedArgs parsed_args {}; | |
| 34 | 37 | parsed_args.model = "gemma3n:latest"; | |
| 35 | 37 | parsed_args.embedding_model = "embeddinggemma:latest"; | |
| 36 | 37 | parsed_args.prompt = "Hey! What's up!"; | |
| 37 | 37 | parsed_args.stream = false; | |
| 38 | 37 | parsed_args.rag = false; | |
| 39 | 37 | parsed_args.interactive = false; | |
| 40 | 37 | parsed_args.dark_theme = false; | |
| 41 | 37 | parsed_args.skip_embedding = false; | |
| 42 | 37 | parsed_args.help = false; | |
| 43 | |||
| 44 | 37 | std::string arg{}; | |
| 45 | 77 | for (int i = 1; i < argc; i += 1) { | |
| 46 | 40 | arg = argv[i]; | |
| 47 | 40 | size_t eq_pos = arg.find('='); | |
| 48 | |||
| 49 | 40 | if (eq_pos == std::string::npos) { | |
| 50 | 2 | continue; | |
| 51 | } | ||
| 52 | |||
| 53 | 38 | auto it = handlers.find(arg.substr(0, eq_pos)); | |
| 54 | 38 | if (it != handlers.end()) { | |
| 55 | 37 | it->second(arg.substr(eq_pos + 1), &parsed_args); | |
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | 74 | return parsed_args; | |
| 60 | 37 | } | |
| 61 | |||
| 62 | /** | ||
| 63 | * Returns the help text containing information about all supported runtime arguments. | ||
| 64 | */ | ||
| 65 | 1 | const char* ArgsUtils::help_text() { | |
| 66 | return "Usage: app [OPTIONS]\n\n" | ||
| 67 | "Options:\n" | ||
| 68 | " --model=<name> The LLM model to use for inference.\n" | ||
| 69 | " (default: gemma3n:latest)\n\n" | ||
| 70 | " --embedding_model=<name> The model to use for generating embeddings/\n" | ||
| 71 | " (default: embeddinggemma:latest)\n\n" | ||
| 72 | " --prompt=<text> Initial prompt or query to send to the model.\n" | ||
| 73 | " (default: Hey! What's up!)\n\n" | ||
| 74 | " --stream=<true|false> Enable streaming responses.\n" | ||
| 75 | " (default: false)\n\n" | ||
| 76 | " --rag=<true|false> Enable Retrieval-Augmented Generation (RAG) mode.\n" | ||
| 77 | " (default: false)\n\n" | ||
| 78 | " --interactive=<true|false> Enable interactive mode for multi-turn conversations.\n" | ||
| 79 | " (default: false)\n\n" | ||
| 80 | " --dark_theme=<true|false> Use dark theme for the ncurses interface.\n" | ||
| 81 | " (default: false)\n\n" | ||
| 82 | " --skip_embedding=<true|false> Skip embedding creation.\n" | ||
| 83 | " (default: false)\n\n" | ||
| 84 | " --help=<true|false> Print help.\n" | ||
| 85 | 1 | " (default: false)\n"; | |
| 86 | } | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Prints usage information for all supported runtime arguments and exits. | ||
| 90 | * Displays each argument name, type, description, and default value. | ||
| 91 | */ | ||
| 92 | ✗ | void ArgsUtils::print_help() { | |
| 93 | ✗ | printf("%s", this->help_text()); | |
| 94 | |||
| 95 | ✗ | std::exit(EXIT_SUCCESS); | |
| 96 | } | ||
| 97 |