From 153b2e4843fe13c309cefa45672fb2bf3e73178e Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 17 May 2025 09:49:08 +1200 Subject: [PATCH] Tidy --- README.md | 20 ++++++++++++-------- include/argparse.hpp | 1 + src/argparse.cpp | 33 ++++++++++++++++++++++++++++++--- src/main.cpp | 6 ++++++ 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 693dcdc..719f304 100644 --- a/README.md +++ b/README.md @@ -13,17 +13,21 @@ curl -fsSL -o dehydrate https://gitea.jde.nz/public/dehydrate/releases/download/ ## How it Works +Dehydrate converts existing files to C++ source code which can be used to recreate the original files. +Works on individual files or entire directory trees. -Given a source file or folder, creates c++ code to recreate that file/folder. -If it's a folder, it recreates the entire tree (all subfolders and files within). - -Use: ``` -Usage: - dehydrate [-s] SOURCEFILE DESTFOLDER Creates _SOURCEFILE.CPP and _SOURCEFILE.HPP in DESTFOLDER - dehydrate [-s] SOURCEFOLDER DESTFOLDER Creates _SOURCEFOLDER.CPP and _SOURCEFOLDER.HPP in DESTFOLDER - -s = silent (no output) +Usage: dehydrate [OPTIONS] SOURCE DEST + +Options: + -s Silent mode (no output) + -u Update dehydrate to the latest version + +Examples: + dehydrate file.txt output/ Creates _file.txt.cpp and _file.txt.hpp in output/ + dehydrate src/ output/ Creates _src.cpp and _src.hpp in output/ + dehydrate -u Updates dehydrate to the latest version ``` All c++ code produced is in namespace `recreate_{SOURCEFILE|SOURCEFOLDER}` diff --git a/include/argparse.hpp b/include/argparse.hpp index be42dfa..4a3d383 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -3,6 +3,7 @@ struct Args { bool silent = false; + bool update = false; std::string source; std::string dest; }; diff --git a/src/argparse.cpp b/src/argparse.cpp index 8ac1375..34372ef 100644 --- a/src/argparse.cpp +++ b/src/argparse.cpp @@ -1,16 +1,43 @@ #include "argparse.hpp" #include +static const std::string HELP_TEXT = R"( +Converts existing files to C++ source code which can be used to recreate the original files. + +Usage: dehydrate [OPTIONS] SOURCE DEST + +Options: + -s Silent mode (no output) + -u Update dehydrate to the latest version + +Examples: + dehydrate file.txt output/ Creates _file.txt.cpp and _file.txt.hpp in output/ + dehydrate src/ output/ Creates _src.cpp and _src.hpp in output/ + dehydrate -u Updates dehydrate to the latest version +)"; + Args parse_args(int argc, char* argv[]) { Args args; int idx = 1; - if (argc > 1 && std::string(argv[1]) == "-s") { + + // Check for silent flag + if (idx < argc && std::string(argv[idx]) == "-s") { args.silent = true; idx++; } - if (argc - idx != 2) { - throw std::runtime_error("Usage: dehydrate [-s] SOURCE DESTFOLDER"); + + // Check for update flag + if (idx < argc && std::string(argv[idx]) == "-u") { + args.update = true; + idx++; + return args; // No need for source and dest parameters when updating } + + // Require source and dest parameters for normal operation + if (argc - idx != 2) { + throw std::runtime_error(HELP_TEXT); + } + args.source = argv[idx]; args.dest = argv[idx + 1]; return args; diff --git a/src/main.cpp b/src/main.cpp index 1fd330e..588138b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -58,6 +58,12 @@ int update() int main(int argc, char* argv[]) { try { Args args = parse_args(argc, argv); + + // Handle update request + if (args.update) { + return update(); + } + std::filesystem::path src(args.source); if (!std::filesystem::exists(src)) { std::cerr << "Source does not exist: " << args.source << std::endl;