fixing me failing at passing module options to modules

master
Chris Punches 2025-03-11 01:42:24 -04:00
parent d47207baae
commit 576ffd5a76
2 changed files with 37 additions and 58 deletions

View File

@ -135,7 +135,11 @@ Command parse_command(const char* cmd_str) {
return CMD_HELP; return CMD_HELP;
} }
if (strcmp(cmd_str, "help") == 0) { // Check if cmd_str is a help option
if (strcmp(cmd_str, "-h") == 0 || strcmp(cmd_str, "--help") == 0) {
return CMD_HELP;
}
else if (strcmp(cmd_str, "help") == 0) {
return CMD_HELP; return CMD_HELP;
} }
else if (strcmp(cmd_str, "create") == 0) { else if (strcmp(cmd_str, "create") == 0) {

View File

@ -46,77 +46,52 @@ CommandArgs parse_args(int argc, char* argv[])
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
// Reset getopt // Find first non-option argument (module name)
optind = 1; int i;
opterr = 1; // Enable getopt error messages for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
// Store original argc/argv to restore later break;
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 // Handle option with argument
int temp_argc = module_pos; if ((strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--module-path") == 0) &&
i + 1 < argc) {
int opt; args.module_path = argv[i + 1];
int option_index = 0; i++; // Skip the argument value
}
// Parse only the global DPM options else if ((strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config-dir") == 0) &&
while ((opt = getopt_long(temp_argc, argv, "m:c:lh", long_options, &option_index)) != -1) { i + 1 < argc) {
switch (opt) { args.config_dir = argv[i + 1];
case 'm': i++; // Skip the argument value
args.module_path = optarg; }
break; else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list-modules") == 0) {
case 'c':
args.config_dir = optarg;
break;
case 'l':
args.list_modules = true; args.list_modules = true;
break; }
case 'h': else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
args.show_help = true; args.show_help = true;
break;
default:
break;
} }
} }
// Reset getopt for future calls // If we found a module name
optind = 1; if (i < argc) {
// Set module name
args.module_name = argv[i];
// Restore original argc/argv // Build command string from remaining arguments
argc = orig_argc; i++; // Move to first argument after module name
argv = orig_argv; while (i < argc) {
// 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 += " ";
} }
std::string arg = argv[i]; std::string arg = argv[i];
// Quote arguments with spaces
if (arg.find(' ') != std::string::npos) { if (arg.find(' ') != std::string::npos) {
args.command += "\"" + arg + "\""; args.command += "\"" + arg + "\"";
} else { } else {
args.command += arg; args.command += arg;
} }
i++;
} }
} }