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) {
@ -187,4 +191,4 @@ int validate_build_options(const BuildOptions& options) {
} }
return 0; return 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] != '-') {
break;
}
// Store original argc/argv to restore later // Handle option with argument
int orig_argc = argc; if ((strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--module-path") == 0) &&
char** orig_argv = argv; i + 1 < argc) {
args.module_path = argv[i + 1];
// Find the first non-option argument which should be the module name i++; // Skip the argument value
int module_pos = 1; }
while (module_pos < argc && argv[module_pos][0] == '-') { else if ((strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--config-dir") == 0) &&
// Skip option and its value if it takes an argument i + 1 < argc) {
if (argv[module_pos][1] == 'm' || argv[module_pos][1] == 'c' || args.config_dir = argv[i + 1];
(strlen(argv[module_pos]) > 2 && i++; // Skip the argument value
(strcmp(&argv[module_pos][1], "-module-path") == 0 || }
strcmp(&argv[module_pos][1], "-config-dir") == 0))) { else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--list-modules") == 0) {
module_pos += 2; args.list_modules = true;
} else { }
module_pos++; else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
args.show_help = true;
} }
} }
// Temporarily set argc to only include global options up to the module name // If we found a module name
int temp_argc = module_pos; if (i < argc) {
// Set module name
args.module_name = argv[i];
int opt; // Build command string from remaining arguments
int option_index = 0; i++; // Move to first argument after module name
while (i < argc) {
// 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 += " ";
} }
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++;
} }
} }