2025-03-02 05:29:26 +00:00
|
|
|
/**
|
|
|
|
* @file dpm_interface_helpers.cpp
|
|
|
|
* @brief Implementation of helper functions for DPM command-line interface
|
|
|
|
*
|
|
|
|
* Implements utility functions for command-line argument parsing and provides
|
|
|
|
* data structures for representing command arguments in a structured format.
|
|
|
|
* These helpers are used by the main DPM interface to process user input.
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2025 SILO GROUP LLC
|
|
|
|
* @author Chris Punches <chris.punches@silogroup.org>
|
|
|
|
*
|
|
|
|
* Part of the Dark Horse Linux Package Manager (DPM)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* For bug reports or contributions, please contact the dhlp-contributors
|
|
|
|
* mailing list at: https://lists.darkhorselinux.org/mailman/listinfo/dhlp-contributors
|
|
|
|
*/
|
|
|
|
|
2025-02-27 06:57:21 +00:00
|
|
|
#include "dpm_interface_helpers.hpp"
|
|
|
|
|
|
|
|
CommandArgs parse_args(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
CommandArgs args;
|
2025-03-02 08:03:00 +00:00
|
|
|
args.module_path = "";
|
2025-03-03 08:56:51 +00:00
|
|
|
args.config_dir = "";
|
2025-03-08 22:44:23 +00:00
|
|
|
args.list_modules = false;
|
|
|
|
args.show_help = false;
|
2025-02-27 06:57:21 +00:00
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"module-path", required_argument, 0, 'm'},
|
2025-03-03 08:56:51 +00:00
|
|
|
{"config-dir", required_argument, 0, 'c'},
|
2025-03-08 22:44:23 +00:00
|
|
|
{"list-modules", no_argument, 0, 'l'},
|
2025-02-27 06:57:21 +00:00
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2025-03-11 05:24:49 +00:00
|
|
|
// 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;
|
|
|
|
|
2025-02-27 06:57:21 +00:00
|
|
|
int opt;
|
|
|
|
int option_index = 0;
|
2025-03-11 05:24:49 +00:00
|
|
|
|
|
|
|
// Parse only the global DPM options
|
|
|
|
while ((opt = getopt_long(temp_argc, argv, "m:c:lh", long_options, &option_index)) != -1) {
|
2025-02-27 06:57:21 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'm':
|
|
|
|
args.module_path = optarg;
|
2025-03-11 05:24:49 +00:00
|
|
|
break;
|
2025-03-03 08:56:51 +00:00
|
|
|
case 'c':
|
|
|
|
args.config_dir = optarg;
|
2025-03-11 05:24:49 +00:00
|
|
|
break;
|
2025-03-08 22:44:23 +00:00
|
|
|
case 'l':
|
|
|
|
args.list_modules = true;
|
2025-03-11 05:24:49 +00:00
|
|
|
break;
|
2025-02-27 06:57:21 +00:00
|
|
|
case 'h':
|
2025-03-08 22:44:23 +00:00
|
|
|
args.show_help = true;
|
2025-03-11 05:24:49 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2025-02-27 06:57:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-11 05:24:49 +00:00
|
|
|
// 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];
|
2025-02-27 06:57:21 +00:00
|
|
|
|
2025-03-11 05:24:49 +00:00
|
|
|
// All arguments after module name go into the command string
|
|
|
|
for (int i = module_pos + 1; i < argc; i++) {
|
2025-02-27 06:57:21 +00:00
|
|
|
if (!args.command.empty()) {
|
|
|
|
args.command += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string arg = argv[i];
|
|
|
|
if (arg.find(' ') != std::string::npos) {
|
|
|
|
args.command += "\"" + arg + "\"";
|
|
|
|
} else {
|
|
|
|
args.command += arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
2025-03-08 22:44:23 +00:00
|
|
|
}
|