cleaned up dpm-core argument routing

master
Chris Punches 2025-03-11 01:24:49 -04:00
parent e2a7390064
commit d47207baae
2 changed files with 58 additions and 21 deletions

View File

@ -33,6 +33,7 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <getopt.h> #include <getopt.h>
#include <cstring>
#include "Logger.hpp" #include "Logger.hpp"
#include "LoggingLevels.hpp" #include "LoggingLevels.hpp"

View File

@ -46,9 +46,36 @@ CommandArgs parse_args(int argc, char* argv[])
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
// Reset getopt
optind = 1;
opterr = 1; // Enable getopt error messages
// Store original argc/argv to restore later
int orig_argc = argc;
char** orig_argv = argv;
// Find the first non-option argument which should be the module name
int module_pos = 1;
while (module_pos < argc && argv[module_pos][0] == '-') {
// Skip option and its value if it takes an argument
if (argv[module_pos][1] == 'm' || argv[module_pos][1] == 'c' ||
(strlen(argv[module_pos]) > 2 &&
(strcmp(&argv[module_pos][1], "-module-path") == 0 ||
strcmp(&argv[module_pos][1], "-config-dir") == 0))) {
module_pos += 2;
} else {
module_pos++;
}
}
// Temporarily set argc to only include global options up to the module name
int temp_argc = module_pos;
int opt; int opt;
int option_index = 0; int option_index = 0;
while ((opt = getopt_long(argc, argv, "m:c:lh", long_options, &option_index)) != -1) {
// Parse only the global DPM options
while ((opt = getopt_long(temp_argc, argv, "m:c:lh", long_options, &option_index)) != -1) {
switch (opt) { switch (opt) {
case 'm': case 'm':
args.module_path = optarg; args.module_path = optarg;
@ -62,15 +89,24 @@ CommandArgs parse_args(int argc, char* argv[])
case 'h': case 'h':
args.show_help = true; args.show_help = true;
break; break;
case '?': default:
exit(1); break;
} }
} }
if (optind < argc) { // Reset getopt for future calls
args.module_name = argv[optind++]; optind = 1;
for (int i = optind; i < argc; i++) { // Restore original argc/argv
argc = orig_argc;
argv = orig_argv;
// If we have a module name
if (module_pos < argc) {
args.module_name = argv[module_pos];
// All arguments after module name go into the command string
for (int i = module_pos + 1; i < argc; i++) {
if (!args.command.empty()) { if (!args.command.empty()) {
args.command += " "; args.command += " ";
} }