From 5058545c22ad1f15659807105e9640260eaadba5 Mon Sep 17 00:00:00 2001 From: luca0N! Date: Thu, 9 Jun 2022 22:35:07 -0300 Subject: [PATCH] Add verbose cmdline option Added a verbose cmdline option and moved stdout debug messages there. --- src/Article.cxx | 1 - src/BlogBuilder.cxx | 3 ++- src/Common.cxx | 2 +- src/Common.hxx | 2 ++ src/WebsiteBuilder.cxx | 3 +-- src/main.cxx | 34 +++++++++++++++++++++++++++++++--- 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/Article.cxx b/src/Article.cxx index b48fccf..15ac2c8 100644 --- a/src/Article.cxx +++ b/src/Article.cxx @@ -116,7 +116,6 @@ namespace Article { bool reading_article_metadata = false; for (std::string const &line : cfg_lines) { if (line == "") continue; - std::cout << "[!] " << line << "\n"; // Check namespace if (reading_article_metadata) { std::string k_title = "Title=", diff --git a/src/BlogBuilder.cxx b/src/BlogBuilder.cxx index c63b9d3..c091f2b 100644 --- a/src/BlogBuilder.cxx +++ b/src/BlogBuilder.cxx @@ -61,7 +61,8 @@ void build_blog_structure(std::string const &path, std::string const &prefix, st // Go ahead and parse article metadata. Article::Metadata *articleMetadata = (Article::Metadata*) malloc(sizeof(Article::Metadata)); Article::get_metadata(a, articleMetadata); - std::cout << "Parsed metadata for article \"" << articleMetadata->title << "\"\n\tPublished on " + if (verbose) + std::cout << "Parsed metadata for article \"" << articleMetadata->title << "\"\n\tPublished on " << ctime(&(articleMetadata->publish_ts)) << "\n"; // TODO: This code could be optimized by removing directory diff --git a/src/Common.cxx b/src/Common.cxx index 170cd45..a54dbe5 100644 --- a/src/Common.cxx +++ b/src/Common.cxx @@ -91,7 +91,7 @@ std::string get_template(std::string const &path) { } fclose(swgTemplate); - std::cout << "Loaded HTML template into memory.\n"; + if (verbose) std::cout << "Loaded HTML template into memory.\n"; return htmlTemplate; } diff --git a/src/Common.hxx b/src/Common.hxx index 806f4a8..fde210c 100644 --- a/src/Common.hxx +++ b/src/Common.hxx @@ -51,3 +51,5 @@ std::string getFilename(std::string const &path, bool const ext = true); std::filesystem::path get_output_path(std::string const &path); bool is_special_file(std::string const &filename); std::string get_template(std::string const &path); + +extern bool verbose; diff --git a/src/WebsiteBuilder.cxx b/src/WebsiteBuilder.cxx index 15a1de3..13156a9 100644 --- a/src/WebsiteBuilder.cxx +++ b/src/WebsiteBuilder.cxx @@ -173,13 +173,12 @@ void build_website(SwgContext &ctx, std::string const &path) { // Skip all files inside the "output" directory. if (dir_entry.path().string().find(path + "output/") == 0) continue; // Directory item iteration - std::cout << "\t" << dir_entry << std::endl; + if (verbose) std::cout << "\t" << dir_entry << std::endl; // Check if the item is a Markdown file. std::string filename = getFilename(dir_entry.path()); if (dir_entry.is_regular_file() && filename.find(".md") == filename.length() - 3) { - std::cout << "\t\tIs file: " << getFilename(dir_entry.path()) << "\n"; // Markdown files should be insite a YYYY/MM directory. if (!is_valid_article(relativePath, dir_entry.path())) failedArticles.insert(failedArticles.end(), dir_entry.path()); diff --git a/src/main.cxx b/src/main.cxx index c74cb82..3efee17 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -33,8 +33,10 @@ #include "Common.hxx" #include "WebsiteBuilder.hxx" +bool verbose = false; + void printUsage(const char *programName) { - std::cerr << programName << ": usage: " << programName << " [path]" + std::cerr << programName << ": usage: " << programName << " [options] [path]" << std::endl; } @@ -53,13 +55,37 @@ int main(int argc, char *argv[]) { for (int argi = 1; argi < argc; argi++) { if (argv[argi][0] == '-') { // This is a cmdline option (starts with a dash). - + if (argv[argi][1] == '-') { + // Long cmdline option (--example). + if (strcmp(argv[argi], "--verbose") == 0) { + verbose = true; + std::cout << "Verbose output enabled.\n"; + } else { + std::cerr << "error: unknown command line option `" << + argv[argi] << "'." << std::endl; + exit(RETURN_FAILED_INVALID_SYNTAX); + } + } else { + for (int i = 1; i < strlen(argv[argi]); i++) { + switch (argv[argi][i]) { + case 'v': + verbose = true; + std::cout << "Verbose output enabled.\n"; + break; + default: + std::cerr << "error: unknown command line option `" << + argv[argi] << "'." << std::endl; + exit(RETURN_FAILED_INVALID_SYNTAX); + break; + } + } + } } else if (path == "") { path = argv[argi]; } else { std::cerr << "error: website directory was already " << "provided (`" << path << "'), refusing to continue with `" << - argv[argi] << "'.\n"; + argv[argi] << "'." << std::endl; printUsage(argv[0]); exit(RETURN_FAILED_INVALID_SYNTAX); } @@ -72,6 +98,8 @@ int main(int argc, char *argv[]) { build_website(ctx, path); + std::cout << "Website build succeeded.\n"; + // Website blog // Append blog to website