some cleaning up of commandline argument parsing

master
Chris Punches 2025-03-13 19:15:42 -04:00
parent 80de44b1b6
commit 02c6c5c66a
5 changed files with 131 additions and 19 deletions

View File

@ -130,6 +130,16 @@ extern "C" {
* @param message The message to log * @param message The message to log
*/ */
void dpm_log(int level, const char* message); void dpm_log(int level, const char* message);
/**
* @brief Sets the logging level
*
* Allows modules to set the logging level used by the DPM logging system.
* This is useful for implementing verbose modes in modules.
*
* @param level The log level (LOG_FATAL, LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DEBUG)
*/
void dpm_set_logging_level(int level);
} }
/** /**
@ -167,6 +177,9 @@ extern "C" void dpm_log(int level, const char* message) { \
extern "C" const char* dpm_get_config(const char* section, const char* key) { \ extern "C" const char* dpm_get_config(const char* section, const char* key) { \
return nullptr; \ return nullptr; \
} \ } \
extern "C" void dpm_set_logging_level(int level) { \
std::cout << "[INFO] Verbosity level ignored, as all standalone executions have maximum verbosity" << std::endl; \
} \
int main(int argc, char** argv) { \ int main(int argc, char** argv) { \
std::cout << "Module version: " << dpm_module_get_version() << std::endl; \ std::cout << "Module version: " << dpm_module_get_version() << std::endl; \
std::cout << "Description: " << dpm_get_description() << std::endl; \ std::cout << "Description: " << dpm_get_description() << std::endl; \

View File

@ -120,5 +120,15 @@ extern "C" {
* @param message The message to log * @param message The message to log
*/ */
void dpm_log(int level, const char* message); void dpm_log(int level, const char* message);
/**
* @brief Sets the logging level
*
* Allows modules to set the logging level used by the DPM logging system.
* This is useful for implementing verbose modes in modules.
*
* @param level The log level as an integer (0=FATAL, 1=ERROR, 2=WARN, 3=INFO, 4=DEBUG)
*/
void dpm_set_logging_level(int level);
} }
/** @} */ /** @} */

View File

@ -1,6 +1,18 @@
#include "cli_parsers.hpp" #include "cli_parsers.hpp"
int parse_create_options(int argc, char** argv, BuildOptions& options) { int parse_create_options(int argc, char** argv, BuildOptions& options) {
// Extend BuildOptions to track which options were provided on command line
struct {
bool output_dir = false;
bool contents_dir = false;
bool hooks_dir = false;
bool package_name = false;
bool force = false;
bool verbose = false;
bool help = false;
} provided;
// For debugging // For debugging
dpm_log(LOG_DEBUG, "Parsing command-line arguments"); dpm_log(LOG_DEBUG, "Parsing command-line arguments");
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
@ -22,18 +34,25 @@ int parse_create_options(int argc, char** argv, BuildOptions& options) {
if (option == "--output-dir") { if (option == "--output-dir") {
options.output_dir = value; options.output_dir = value;
provided.output_dir = true;
} else if (option == "--contents") { } else if (option == "--contents") {
options.contents_dir = value; options.contents_dir = value;
provided.contents_dir = true;
} else if (option == "--hooks") { } else if (option == "--hooks") {
options.hooks_dir = value; options.hooks_dir = value;
provided.hooks_dir = true;
} else if (option == "--name") { } else if (option == "--name") {
options.package_name = value; options.package_name = value;
provided.package_name = true;
} else if (option == "--force") { } else if (option == "--force") {
options.force = true; options.force = true;
provided.force = true;
} else if (option == "--verbose") { } else if (option == "--verbose") {
options.verbose = true; options.verbose = true;
provided.verbose = true;
} else if (option == "--help") { } else if (option == "--help") {
options.show_help = true; options.show_help = true;
provided.help = true;
} }
// Convert this argument to a dummy to prevent getopt from processing it // Convert this argument to a dummy to prevent getopt from processing it
@ -64,24 +83,31 @@ int parse_create_options(int argc, char** argv, BuildOptions& options) {
switch (opt) { switch (opt) {
case 'o': case 'o':
options.output_dir = optarg; options.output_dir = optarg;
provided.output_dir = true;
break; break;
case 'c': case 'c':
options.contents_dir = optarg; options.contents_dir = optarg;
provided.contents_dir = true;
break; break;
case 'H': case 'H':
options.hooks_dir = optarg; options.hooks_dir = optarg;
provided.hooks_dir = true;
break; break;
case 'n': case 'n':
options.package_name = optarg; options.package_name = optarg;
provided.package_name = true;
break; break;
case 'f': case 'f':
options.force = true; options.force = true;
provided.force = true;
break; break;
case 'v': case 'v':
options.verbose = true; options.verbose = true;
provided.verbose = true;
break; break;
case 'h': case 'h':
options.show_help = true; options.show_help = true;
provided.help = true;
break; break;
case '?': case '?':
// Ignore errors as we handle equals-format options separately // Ignore errors as we handle equals-format options separately
@ -105,7 +131,40 @@ int parse_create_options(int argc, char** argv, BuildOptions& options) {
} }
// Log the parsed options for debugging // Log the parsed options for debugging
dpm_log(LOG_DEBUG, ("Parsed options: contents_dir=" + options.contents_dir).c_str()); dpm_log(LOG_DEBUG, "Parsed options:");
if (provided.output_dir) {
dpm_log(LOG_DEBUG, (" output_dir=" + options.output_dir).c_str());
}
if (provided.contents_dir) {
dpm_log(LOG_DEBUG, (" contents_dir=" + options.contents_dir).c_str());
}
if (provided.hooks_dir) {
dpm_log(LOG_DEBUG, (" hooks_dir=" + options.hooks_dir).c_str());
}
if (provided.package_name) {
dpm_log(LOG_DEBUG, (" package_name=" + options.package_name).c_str());
}
if (provided.force) {
dpm_log(LOG_DEBUG, " force=true");
}
if (provided.verbose) {
dpm_log(LOG_DEBUG, " verbose=true");
}
if (provided.help) {
dpm_log(LOG_DEBUG, " help=true");
}
if (!provided.output_dir && !provided.contents_dir && !provided.hooks_dir &&
!provided.package_name && !provided.force && !provided.verbose && !provided.help) {
dpm_log(LOG_DEBUG, " No options were provided");
}
return 0; return 0;
} }

View File

@ -1,5 +1,6 @@
#include "commands.hpp" #include "commands.hpp"
int cmd_stage(int argc, char** argv) { int cmd_stage(int argc, char** argv) {
// create a container for commandline options // create a container for commandline options
BuildOptions options; BuildOptions options;
@ -15,39 +16,41 @@ int cmd_stage(int argc, char** argv) {
return cmd_help(argc, argv); return cmd_help(argc, argv);
} }
// Set logging level to DEBUG when verbose is enabled
if (options.verbose) {
dpm_set_logging_level(LOG_DEBUG);
}
// Validate options // Validate options
int validate_result = validate_build_options(options); int validate_result = validate_build_options(options);
if (validate_result != 0) { if (validate_result != 0) {
return validate_result; return validate_result;
} }
// Log the operation // Log detailed options (only visible in verbose mode)
if (options.verbose) { dpm_log(LOG_DEBUG, "Staging DPM package with the following options:");
dpm_log(LOG_INFO, "Staging DPM package with the following options:"); dpm_log(LOG_DEBUG, (" Output directory: " + options.output_dir).c_str());
dpm_log(LOG_INFO, (" Output directory: " + options.output_dir).c_str()); dpm_log(LOG_DEBUG, (" Contents directory: " + options.contents_dir).c_str());
dpm_log(LOG_INFO, (" Contents directory: " + options.contents_dir).c_str());
if (!options.hooks_dir.empty()) { if (!options.hooks_dir.empty()) {
dpm_log(LOG_INFO, (" Hooks directory: " + options.hooks_dir).c_str()); dpm_log(LOG_DEBUG, (" Hooks directory: " + options.hooks_dir).c_str());
}
if (!options.package_name.empty()) {
dpm_log(LOG_INFO, (" Package name: " + options.package_name).c_str());
}
if (options.force) {
dpm_log(LOG_INFO, " Force: Yes");
}
} }
// For now, just log that we would stage the package if (!options.package_name.empty()) {
dpm_log(LOG_DEBUG, (" Package name: " + options.package_name).c_str());
}
if (options.force) {
dpm_log(LOG_DEBUG, " Force: Yes");
}
// Standard info logs that are always visible
dpm_log(LOG_INFO, "Package staging functionality not yet implemented"); dpm_log(LOG_INFO, "Package staging functionality not yet implemented");
dpm_log(LOG_INFO, "Would stage package directory using the provided options"); dpm_log(LOG_INFO, "Would stage package directory using the provided options");
return 0; return 0;
} }
int cmd_help(int argc, char** argv) { int cmd_help(int argc, char** argv) {
dpm_log(LOG_INFO, "DPM Build Module - Creates DPM packages according to specification"); dpm_log(LOG_INFO, "DPM Build Module - Creates DPM packages according to specification");
dpm_log(LOG_INFO, "Available commands:"); dpm_log(LOG_INFO, "Available commands:");

View File

@ -63,3 +63,30 @@ extern "C" void dpm_log(int level, const char* message) {
g_logger.log(log_level, message); g_logger.log(log_level, message);
} }
extern "C" void dpm_set_logging_level(int level) {
// Convert integer level to LoggingLevels enum
LoggingLevels log_level;
switch (level) {
case 0:
log_level = LoggingLevels::FATAL;
break;
case 1:
log_level = LoggingLevels::ERROR;
break;
case 2:
log_level = LoggingLevels::WARN;
break;
case 3:
log_level = LoggingLevels::INFO;
break;
case 4:
log_level = LoggingLevels::DEBUG;
break;
default:
log_level = LoggingLevels::INFO;
break;
}
g_logger.setLogLevel(log_level);
}