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,31 +46,67 @@ CommandArgs parse_args(int argc, char* argv[])
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
int opt; // Reset getopt
int option_index = 0; optind = 1;
while ((opt = getopt_long(argc, argv, "m:c:lh", long_options, &option_index)) != -1) { opterr = 1; // Enable getopt error messages
switch (opt) {
case 'm': // Store original argc/argv to restore later
args.module_path = optarg; int orig_argc = argc;
break; char** orig_argv = argv;
case 'c':
args.config_dir = optarg; // Find the first non-option argument which should be the module name
break; int module_pos = 1;
case 'l': while (module_pos < argc && argv[module_pos][0] == '-') {
args.list_modules = true; // Skip option and its value if it takes an argument
break; if (argv[module_pos][1] == 'm' || argv[module_pos][1] == 'c' ||
case 'h': (strlen(argv[module_pos]) > 2 &&
args.show_help = true; (strcmp(&argv[module_pos][1], "-module-path") == 0 ||
break; strcmp(&argv[module_pos][1], "-config-dir") == 0))) {
case '?': module_pos += 2;
exit(1); } else {
module_pos++;
} }
} }
if (optind < argc) { // Temporarily set argc to only include global options up to the module name
args.module_name = argv[optind++]; int temp_argc = module_pos;
for (int i = optind; i < argc; i++) { int opt;
int option_index = 0;
// Parse only the global DPM options
while ((opt = getopt_long(temp_argc, argv, "m:c:lh", long_options, &option_index)) != -1) {
switch (opt) {
case 'm':
args.module_path = optarg;
break;
case 'c':
args.config_dir = optarg;
break;
case 'l':
args.list_modules = true;
break;
case 'h':
args.show_help = true;
break;
default:
break;
}
}
// Reset getopt for future calls
optind = 1;
// 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 += " ";
} }