stamen

Stamen - static menu generator
git clone git://git.dimitrijedobrota.com/stamen.git
Log | Files | Refs | README | LICENSE

commit 12c5ce88ce059481256dc028f9c672c1861655ca
parent e665a6f2bca782f688527dcf8232eeff412652a8
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Fri, 14 Jun 2024 01:00:28 +0200

Use args library in generator for better usability

Diffstat:
MCMakeLists.txt | 2++
Mdemo/CMakeLists.txt | 4++--
Msrc/CMakeLists.txt | 2+-
Msrc/generate.cpp | 67+++++++++++++++++++++++++++++++++++++++++++++++++++----------------
4 files changed, 56 insertions(+), 19 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -44,6 +44,8 @@ foreach(p LIB BIN INCLUDE CMAKE) unset(_path) endforeach() +find_package(args 1 CONFIG REQUIRED) + add_subdirectory(src) add_subdirectory(demo) diff --git a/demo/CMakeLists.txt b/demo/CMakeLists.txt @@ -4,7 +4,7 @@ configure_file(demo_menu.conf ${GENERATE_OUT}/demo_menu.conf COPYONLY) add_custom_command( OUTPUT ${GENERATE_OUT}/demo_menu.hpp ${GENERATE_OUT}/demo_menu.cpp - COMMAND ${GENERATE_OUT}/stamen-generate ${GENERATE_OUT}/demo_menu.conf + COMMAND ${GENERATE_OUT}/stamen-generate --cpp ${GENERATE_OUT}/demo_menu.conf DEPENDS demo_menu.conf stamen-generate COMMENT "Generating menu files" ) @@ -23,7 +23,7 @@ set_target_properties(demo PROPERTIES add_custom_command( OUTPUT ${GENERATE_OUT}/demo_menu.h ${GENERATE_OUT}/demo_menu.c - COMMAND ${GENERATE_OUT}/stamen-generate ${GENERATE_OUT}/demo_menu.conf c + COMMAND ${GENERATE_OUT}/stamen-generate --c ${GENERATE_OUT}/demo_menu.conf DEPENDS demo_menu.conf stamen-generate COMMENT "Generating cmenu files" ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt @@ -13,7 +13,7 @@ set_target_properties(stamen PROPERTIES ) add_executable(stamen-generate generate.cpp) -target_link_libraries(stamen-generate PRIVATE stamen) +target_link_libraries(stamen-generate PRIVATE args stamen) target_include_directories(stamen-generate PRIVATE ../include) set_target_properties(stamen-generate PROPERTIES diff --git a/src/generate.cpp b/src/generate.cpp @@ -1,3 +1,5 @@ +#include <args/args.hpp> + #include "menu.h" #include "stamen.h" @@ -53,31 +55,64 @@ class Generator { } } }; - } // namespace stamen -using namespace stamen; - -int main(const int argc, const char *argv[]) { - const auto args = std::span(argv, size_t(argc)); +struct arguments_t { + std::string config; + bool cpp = false; +}; - if (argc != 2 && argc != 3) { - std::cout << argv[0] << " config_file [c/cpp]" << std::endl; - return 1; +int parse_opt(int key, const char *arg, args::Parser *parser) { + auto arguments = (arguments_t *)parser->input(); + switch (key) { + case 666: arguments->cpp = false; break; + case 777: arguments->cpp = true; break; + case args::ARG: + if (!arguments->config.empty()) { + args::failure(parser, 1, 0, "Too many arguments"); + } + arguments->config = arg; + break; + case args::NO_ARGS: + args::failure(parser, 0, 0, "Missing an argument"); + args::help(parser, stderr, args::STD_USAGE); } + return 0; +} + +static const args::option_t options[]{ + {0, 0, 0, 0, "Output mode", 1}, + {"c", 666, 0, 0, "Generate files for C"}, + {"cpp", 777, 0, 0, "Generate files for C++"}, + {0, 0, 0, 0, "Informational Options", -1}, + {0}, +}; - const bool cpp = argc == 2 || std::string(args[2]) == "cpp"; +static const args::argp_t argp{ + options, + parse_opt, + "config_file", + "", +}; + +int main(int argc, char *argv[]) { + arguments_t arguments; + if (args::parse(&argp, argc, argv, 0, &arguments)) { + std::cerr << "There was an error while parsing arguments"; + return 0; + } - std::string path = args[1]; - Menu::read(path); + const auto &config = arguments.config; + stamen::Menu::read(config); - std::string::size_type pos = path.rfind('.'); - std::string base = pos != std::string::npos ? path.substr(0, pos) : path; - std::string ext = cpp ? "pp" : ""; + std::string::size_type pos = arguments.config.rfind('.'); + std::string base = + pos != std::string::npos ? config.substr(0, pos) : config; + std::string ext = arguments.cpp ? "pp" : ""; std::ofstream source(base + ".c" + ext), include(base + ".h" + ext); - Generator::generateSource(source, cpp); - Generator::generateInclude(include, cpp); + stamen::Generator::generateSource(source, arguments.cpp); + stamen::Generator::generateInclude(include, arguments.cpp); return 0; }