diff options
author | Calvin Buckley <calvin@cmpct.info> | 2020-02-11 18:24:37 -0400 |
---|---|---|
committer | Calvin Buckley <calvin@cmpct.info> | 2020-02-11 18:32:26 -0400 |
commit | 4946a64db774a140d06c45f186f5bc5a4c0c6daa (patch) | |
tree | ebd08b88588ca8390842be86209129e58efd9f25 /tools | |
parent | 9fa9857a7514469d062335cd2dcb8c0d06eb624c (diff) | |
download | horizon-4946a64db774a140d06c45f186f5bc5a4c0c6daa.tar.gz horizon-4946a64db774a140d06c45f186f5bc5a4c0c6daa.tar.bz2 horizon-4946a64db774a140d06c45f186f5bc5a4c0c6daa.tar.xz horizon-4946a64db774a140d06c45f186f5bc5a4c0c6daa.zip |
Use Boost's program_options instead of vendoring clipp
This changes how options are parsed a bit, but tests are happy and
typical usage is unaffected; just the usage screen is different.
This was intended for post-1.0, but turns out I decided to do it.
Boost is already required for pre-C++11, so it doesn't seem like
a big deal to use. If you don't build the tools, it won't be
required.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tools/hscript-simulate/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tools/hscript-simulate/simulator.cc | 57 | ||||
-rw-r--r-- | tools/hscript-validate/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tools/hscript-validate/validator.cc | 71 |
5 files changed, 99 insertions, 36 deletions
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 5f37892..575403a 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,2 +1,5 @@ +find_package(Boost REQUIRED COMPONENTS program_options) +include_directories(${Boost_INCLUDE_DIR}) + add_subdirectory(hscript-validate) add_subdirectory(hscript-simulate) diff --git a/tools/hscript-simulate/CMakeLists.txt b/tools/hscript-simulate/CMakeLists.txt index ddd7eed..ded4e10 100644 --- a/tools/hscript-simulate/CMakeLists.txt +++ b/tools/hscript-simulate/CMakeLists.txt @@ -2,7 +2,7 @@ set(SIMULATOR_SRCS simulator.cc ) add_executable(hscript-simulate ${SIMULATOR_SRCS}) -target_link_libraries(hscript-simulate hscript) +target_link_libraries(hscript-simulate hscript ${Boost_LIBRARIES}) install(TARGETS hscript-simulate DESTINATION bin) diff --git a/tools/hscript-simulate/simulator.cc b/tools/hscript-simulate/simulator.cc index 3dd6175..7162c4b 100644 --- a/tools/hscript-simulate/simulator.cc +++ b/tools/hscript-simulate/simulator.cc @@ -9,13 +9,18 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +#include <boost/program_options.hpp> #include <unistd.h> #include "hscript/script.hh" #include "util/output.hh" -#include "3rdparty/clipp.h" bool pretty = false; +static int cli_failure(boost::program_options::options_description cli) { + std::cout << cli << std::endl; + return EXIT_FAILURE; +} + int main(int argc, char *argv[]) { const Horizon::Script *my_script; Horizon::ScriptOptions opts; @@ -30,19 +35,43 @@ int main(int argc, char *argv[]) { opts.set(ScriptOptionFlags::Simulate); - auto cli = ( - clipp::value("installfile", installfile), - clipp::option("-n", "--no-colour").doc("Do not 'prettify' output")( - [] { pretty = false; } - ), - clipp::option("-s", "--strict").doc("Strict parsing")( - [&opts] { opts.set(ScriptOptionFlags::StrictMode); } - ) - ); - if(!clipp::parse(argc, argv, cli)) { - std::cout << "usage:" << std::endl; - std::cout << clipp::usage_lines(cli, "hscript-simulate") << std::endl; - return EXIT_FAILURE; + boost::program_options::options_description cli_hidden; + cli_hidden.add_options() + ("installfile", "Installfile to load"); + boost::program_options::options_description cli_visible("Allowed options"); + cli_visible.add_options() + ("no-colour,n", "Do not 'prettify' output") + ("strict,s", "Strict parsing"); + boost::program_options::options_description cli; + cli.add(cli_visible).add(cli_hidden); + boost::program_options::positional_options_description cli_pos; + cli_pos.add("installfile", -1); + boost::program_options::variables_map args; + try { + boost::program_options::store( + boost::program_options::command_line_parser(argc, argv) + .options(cli) + .positional(cli_pos) + .run(), + args); + boost::program_options::notify(args); + } catch(const boost::program_options::error& cli_err) { + std::cout << "An invalid option was entered." << std::endl; + return cli_failure(cli_visible); + } + + if (args.count("installfile")) { + installfile = args["installfile"].as<std::string>(); + } else { + std::cout << "You must provide an installfile." << std::endl; + return cli_failure(cli_visible); + } + + if (args.count("no-colour")) { + pretty = false; + } + if (args.count("strict")) { + opts.set(ScriptOptionFlags::StrictMode); } if(!isatty(1)) { diff --git a/tools/hscript-validate/CMakeLists.txt b/tools/hscript-validate/CMakeLists.txt index 2f0b5a4..6eae7cc 100644 --- a/tools/hscript-validate/CMakeLists.txt +++ b/tools/hscript-validate/CMakeLists.txt @@ -2,7 +2,7 @@ set(VALIDATE_SRCS validator.cc ) add_executable(hscript-validate ${VALIDATE_SRCS}) -target_link_libraries(hscript-validate hscript) +target_link_libraries(hscript-validate hscript ${Boost_LIBRARIES}) install(TARGETS hscript-validate DESTINATION bin) diff --git a/tools/hscript-validate/validator.cc b/tools/hscript-validate/validator.cc index 521e8ee..fe5a1f6 100644 --- a/tools/hscript-validate/validator.cc +++ b/tools/hscript-validate/validator.cc @@ -9,13 +9,18 @@ * SPDX-License-Identifier: AGPL-3.0-only */ +#include <boost/program_options.hpp> #include <unistd.h> #include "hscript/script.hh" #include "util/output.hh" -#include "3rdparty/clipp.h" bool pretty = false; +static int cli_failure(boost::program_options::options_description cli) { + std::cout << cli << std::endl; + return EXIT_FAILURE; +} + int main(int argc, char *argv[]) { const Horizon::Script *my_script; Horizon::ScriptOptions opts; @@ -28,25 +33,51 @@ int main(int argc, char *argv[]) { pretty = true; /* LCOV_EXCL_LINE */ } - auto cli = ( - clipp::value("installfile", installfile), - clipp::option("-i", "--install").doc("Set Installation Environment flag (DANGEROUS)")( - [&opts] { opts.set(ScriptOptionFlags::InstallEnvironment); } - ), - clipp::option("-k", "--keep-going").doc("Continue parsing after errors")( - [&opts] { opts.set(ScriptOptionFlags::KeepGoing); } - ), - clipp::option("-n", "--no-colour").doc("Do not 'prettify' output")( - [] { pretty = false; } - ), - clipp::option("-s", "--strict").doc("Strict parsing")( - [&opts] { opts.set(ScriptOptionFlags::StrictMode); } - ) - ); - if(!clipp::parse(argc, argv, cli)) { - std::cout << "usage:" << std::endl; - std::cout << clipp::usage_lines(cli, "hscript-validate") << std::endl; - return EXIT_FAILURE; + boost::program_options::options_description cli_hidden; + cli_hidden.add_options() + ("installfile", "Installfile to load"); + boost::program_options::options_description cli_visible("Allowed options"); + cli_visible.add_options() + ("install,i", "Set Installation Environment flag (DANGEROUS)") + ("keep-going,k", "Continue parsing after errors") + ("no-colour,n", "Do not 'prettify' output") + ("strict,s", "Strict parsing"); + boost::program_options::options_description cli; + cli.add(cli_visible).add(cli_hidden); + boost::program_options::positional_options_description cli_pos; + cli_pos.add("installfile", -1); + boost::program_options::variables_map args; + try { + boost::program_options::store( + boost::program_options::command_line_parser(argc, argv) + .options(cli) + .positional(cli_pos) + .run(), + args); + boost::program_options::notify(args); + } catch(const boost::program_options::error& cli_err) { + std::cout << "An invalid option was entered." << std::endl; + return cli_failure(cli_visible); + } + + if (args.count("installfile")) { + installfile = args["installfile"].as<std::string>(); + } else { + std::cout << "You must provide an installfile." << std::endl; + return cli_failure(cli_visible); + } + + if (args.count("install")) { + opts.set(ScriptOptionFlags::InstallEnvironment); + } + if (args.count("keep-going")) { + opts.set(ScriptOptionFlags::KeepGoing); + } + if (args.count("no-colour")) { + pretty = false; + } + if (args.count("strict")) { + opts.set(ScriptOptionFlags::StrictMode); } bold_if_pretty(std::cout); |