Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
config.cpp File Reference

Contains logic for parsing command-line arguments into an AppConfig object. More...

#include "config.hpp"
#include <stdexcept>

Go to the source code of this file.

Functions

AppConfig parse_arguments (const std::vector< std::string > &args)
 Parses command-line arguments into an AppConfig structure.
 

Detailed Description

Contains logic for parsing command-line arguments into an AppConfig object.

This file provides the implementation of a utility function that translates raw command-line input into structured configuration for the application.

Definition in file config.cpp.

Function Documentation

◆ parse_arguments()

AppConfig parse_arguments ( const std::vector< std::string > & args)

Parses command-line arguments into an AppConfig structure.

This function iterates over the provided command-line arguments and looks for:

  • --user <username> or --user=<username>: Sets the GitHub username to fetch.
  • --server: Indicates that the HTTP server should be started.
  • --port=<number>: Sets the port number for the HTTP server.

Invalid port values will cause an exception to be thrown.

Parameters
argsA list of command-line arguments, excluding the program name.
Returns
AppConfig A structure populated with the extracted configuration.
Exceptions
std::invalid_argumentIf the port value is not a valid integer.

Definition at line 31 of file config.cpp.

31 {
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}
CURL_EXTERN int void * arg
Definition curl.h:2622

References arg.

Referenced by main(), TEST_CASE(), TEST_CASE(), TEST_CASE(), and TEST_CASE().