OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
OdbDesignArgs.cpp
Go to the documentation of this file.
1#include "OdbDesignArgs.h"
2#include <iostream>
3#include <sstream>
4
5namespace Odb::Lib::App
6{
7 OdbDesignArgs::OdbDesignArgs(int argc, char* argv[])
8 : CommandLineArgs(argc, argv)
9 {
10 }
11
13 {
14 return intArg("port", DEFAULT_PORT);
15 }
16
17 std::string OdbDesignArgs::bindAddress() const
18 {
19 return stringArg("bind", DEFAULT_BIND_ADDRESS);
20 }
21
23 {
24 return intArg("grpc-port", DEFAULT_GRPC_PORT);
25 }
26
27 std::string OdbDesignArgs::designsDir() const
28 {
29 return stringArg("designs-dir", DEFAULT_DESIGNS_DIR);
30 }
31
32 std::string OdbDesignArgs::templatesDir() const
33 {
34 return stringArg("templates-dir", DEFAULT_TEMPLATES_DIR);
35 }
36
38 {
39 return boolArg("help", DEFAULT_HELP);
40 }
41
43 {
44 return boolArg("http-trace", DEFAULT_HTTP_TRACE);
45 }
46
47 std::string OdbDesignArgs::loadDesign() const
48 {
49 return stringArg("load-design", DEFAULT_LOAD_DESIGN);
50 }
51
53 {
54 return boolArg("load-all", DEFAULT_LOAD_ALL);
55 }
56
58 {
59 return boolArg("disable-authentication", DEFAULT_DISABLE_AUTH);
60 }
61
63 {
64 // intArg -> std::stoi throws when CommandLineArgs stored the literal
65 // boolean `true` (missing value, or a value starting with '-' or '/');
66 // this runs unconditionally at the top of OdbAppBase::Run(), so a
67 // malformed --cache-max-mb must not std::terminate the process — fall
68 // back to the default with a warning. Negative values clamp to 0
69 // (eviction disabled), matching setCacheMaxBytes semantics.
70 try
71 {
72 const int mb = intArg("cache-max-mb", DEFAULT_CACHE_MAX_MB);
73 return mb < 0 ? 0 : mb;
74 }
75 catch (const std::exception&)
76 {
77 std::cerr << "WARNING: invalid --cache-max-mb value; using default "
78 << DEFAULT_CACHE_MAX_MB << std::endl;
79 return DEFAULT_CACHE_MAX_MB;
80 }
81 }
82
84 {
85 // Same stoi-hardening as cacheMaxMb(): a malformed --max-background-loads
86 // must not std::terminate the process — fall back to the default with a
87 // warning. Negative values clamp to 0 (unbounded), matching
88 // DesignCache::setMaxBackgroundLoads semantics.
89 try
90 {
91 const int loads = intArg("max-background-loads", DEFAULT_MAX_BACKGROUND_LOADS);
92 return loads < 0 ? 0 : loads;
93 }
94 catch (const std::exception&)
95 {
96 std::cerr << "WARNING: invalid --max-background-loads value; using default "
97 << DEFAULT_MAX_BACKGROUND_LOADS << std::endl;
98 return DEFAULT_MAX_BACKGROUND_LOADS;
99 }
100 }
101
103 {
104 std::stringstream ss;
105 ss << "Usage: " << executableName() << " [options]\n";
106 ss << "Options:\n";
107 ss << " --bind <ip|host> Bind address for REST server (default: " << DEFAULT_BIND_ADDRESS << ")\n";
108 ss << " --port <port> Port to listen on (default: " << DEFAULT_PORT << ")\n";
109 ss << " --grpc-port <port> gRPC port to listen on (default: " << DEFAULT_GRPC_PORT << ")\n";
110 ss << " --designs-dir <dir> Directory containing design files (default: " << DEFAULT_DESIGNS_DIR << ")\n";
111 ss << " --templates-dir <dir> Directory containing template files (default: " << DEFAULT_TEMPLATES_DIR << ")\n";
112 ss << " --http-trace Enable per-request HTTP tracing (default: " << (DEFAULT_HTTP_TRACE ? "true" : "false") << ")\n";
113 ss << " --load-design <design> Design to load on startup (default: " << DEFAULT_LOAD_DESIGN << ")\n";
114 ss << " --load-all Load all designs on startup (default: " << (DEFAULT_LOAD_ALL ? "true" : "false") << ")\n";
115 ss << " --disable-authentication Disable authentication (default: " << (DEFAULT_DISABLE_AUTH ? "true" : "false") << ")\n";
116 ss << " --cache-max-mb <MB> Max design cache size in MB before LRU eviction, 0 disables (default: " << DEFAULT_CACHE_MAX_MB << ")\n";
117 ss << " --max-background-loads <N> Max concurrent background design loads, 0 = unbounded (default: " << DEFAULT_MAX_BACKGROUND_LOADS << ")\n";
118 ss << " --help Print this help message\n";
119 return ss.str();
120 }
121}
std::string templatesDir() const
OdbDesignArgs(int argc, char *argv[])
std::string bindAddress() const
std::string getUsageString() const override
std::string designsDir() const
std::string loadDesign() const
std::string executableName() const
bool boolArg(const std::string &name, bool defaultValue) const
int intArg(const std::string &name, int defaultValue) const
std::string stringArg(const std::string &name, const std::string &defaultValue) const