moved optstring and long options to the constructor

This commit is contained in:
2025-07-09 15:02:12 +02:00
parent e1f8c4012f
commit d8ee0c2279
2 changed files with 9 additions and 5 deletions

View File

@ -11,6 +11,10 @@
#include <cstring>
#include <unistd.h>
CommandLineOptions::CommandLineOptions(AppType app)
: appType_(app), optString_(buildOptString()),
longOptions_(buildOptionList()) {}
/** for testing */
ParsedOptions CommandLineOptions::parse(const std::vector<std::string> &args) {
std::vector<char *> argv;
@ -28,13 +32,11 @@ ParsedOptions CommandLineOptions::parse(int argc, char *argv[]) {
FrameSyncOptions frame;
base.port = DEFAULT_TCP_RX_PORTNO;
auto optString = buildOptString();
auto longOptions = buildOptionList();
optind = 0; // reset getopt
int opt, option_index = 0;
while ((opt = getopt_long(argc, argv, optString.c_str(), longOptions.data(),
&option_index)) != -1) {
while ((opt = getopt_long(argc, argv, optString_.c_str(),
longOptions_.data(), &option_index)) != -1) {
switch (opt) {
case 'v':
case 'h':

View File

@ -33,7 +33,7 @@ using ParsedOptions =
class CommandLineOptions {
public:
constexpr explicit CommandLineOptions(AppType app) : appType_(app) {}
explicit CommandLineOptions(AppType app);
ParsedOptions parse(const std::vector<std::string> &args); // for testing
ParsedOptions parse(int argc, char *argv[]);
std::string getTypeString() const;
@ -46,6 +46,8 @@ class CommandLineOptions {
private:
AppType appType_;
std::string optString_;
std::vector<option> longOptions_;
std::vector<option> buildOptionList() const;
std::string buildOptString() const;