Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
config.cpp
Go to the documentation of this file.
1/**
2 * @file config.cpp
3 * @brief Contains logic for parsing command-line arguments into an AppConfig
4 * object.
5 *
6 * This file provides the implementation of a utility function that translates
7 * raw command-line input into structured configuration for the application.
8 */
9
10#include "config.hpp"
11
12#include <stdexcept>
13
14/**
15 * @brief Parses command-line arguments into an AppConfig structure.
16 *
17 * This function iterates over the provided command-line arguments and looks
18 * for:
19 * - `--user <username>` or `--user=<username>`: Sets the GitHub username to
20 * fetch.
21 * - `--server`: Indicates that the HTTP server should be started.
22 * - `--port=<number>`: Sets the port number for the HTTP server.
23 *
24 * Invalid port values will cause an exception to be thrown.
25 *
26 * @param args A list of command-line arguments, excluding the program name.
27 * @return AppConfig A structure populated with the extracted configuration.
28 *
29 * @throws std::invalid_argument If the port value is not a valid integer.
30 */
31AppConfig parse_arguments(const std::vector<std::string> &args) {
32 AppConfig config;
33
34 for (size_t i = 0; i < args.size(); ++i) {
35 const std::string &arg = args[i];
36
37 if (arg == "--user" && i + 1 < args.size()) {
38 config.username = args[++i];
39 } else if (arg.starts_with("--user=")) {
40 config.username = arg.substr(7);
41 } else if (arg == "--server") {
42 config.run_server = true;
43 } else if (arg.starts_with("--port=")) {
44 try {
45 config.port = std::stoi(arg.substr(7));
46 } catch (const std::exception &) {
47 throw std::invalid_argument("Invalid port number: " + arg.substr(7));
48 }
49 }
50 }
51
52 return config;
53}
AppConfig parse_arguments(const std::vector< std::string > &args)
Parses command-line arguments into an AppConfig structure.
Definition config.cpp:31
CURL_EXTERN int void * arg
Definition curl.h:2622