From 1dbb579fc4fd46bd2ac8a724420caa0739952d61 Mon Sep 17 00:00:00 2001 From: Waldemar Kozaczuk Date: Thu, 3 Oct 2019 00:24:36 -0400 Subject: [PATCH] cpiod: reduce cpiod size by replacing boost program options with the custom light alternative Fixes #980 Signed-off-by: Waldemar Kozaczuk Message-Id: <20191003042437.27978-4-jwkozaczuk@gmail.com> --- tools/cpiod/cpiod.cc | 69 +++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/tools/cpiod/cpiod.cc b/tools/cpiod/cpiod.cc index 5305b94cae..5959362512 100644 --- a/tools/cpiod/cpiod.cc +++ b/tools/cpiod/cpiod.cc @@ -11,17 +11,16 @@ #include #include #include -#include #include "cpio.hh" #include #include #include #include +#include using namespace osv; using namespace std; using namespace boost::asio::ip; -namespace po = boost::program_options; // Want to use boost::filesystem, but too much effort to integrate extern "C" { int mkdirp(const char *d, mode_t mode); } @@ -127,31 +126,55 @@ class cpio_in_expand : public cpio_in { } }; +static void usage() +{ + std::cout << "Allowed options:\n"; + std::cout << " --help produce help message\n"; + std::cout << " --port arg (=10000) set listening port\n"; + std::cout << " --verbose arg (=1) disable verbose output\n"; + std::cout << " --prefix arg (=/) set prefix\n\n"; +} + +static void handle_parse_error(const std::string &message) +{ + std::cout << message << std::endl; + usage(); + exit(1); +} + int main(int ac, char** av) { - int port; - std::string prefix; - bool verbose; - // Declare the supported options. - po::options_description desc("Allowed options"); - desc.add_options() - ("help", "produce help message") - ("port", po::value()->default_value(10000), "set listening port") - ("verbose", po::value()->default_value(true), "disable verbose output") - ("prefix", po::value()->default_value(std::string("/")), "set prefix"); - - po::variables_map vm; - po::store(po::parse_command_line(ac, av, desc), vm); - po::notify(vm); - - if (vm.count("help")) { - cout << desc << "\n"; - return 1; + int port = 10000; + std::string prefix("/"); + bool verbose = true; + + auto options_values = options::parse_options_values(ac - 1, av + 1, handle_parse_error); + + if (options::extract_option_flag(options_values, "help", handle_parse_error)) { + usage(); + return 1; } - port = vm["port"].as(); - prefix = vm["prefix"].as(); - verbose = vm["verbose"].as(); + if (options::option_value_exists(options_values, "port")) { + port = options::extract_option_int_value(options_values, "port", handle_parse_error); + } + + if (options::option_value_exists(options_values, "prefix")) { + prefix = options::extract_option_value(options_values, "prefix"); + } + + if (options::option_value_exists(options_values, "verbose")) { + verbose = options::extract_option_int_value(options_values, "verbose", handle_parse_error) != 0; + } + + if (!options_values.empty()) { + for (auto other_option : options_values) { + std::cout << "Unrecognized option: " << other_option.first << std::endl; + } + + usage(); + return 1; + } boost::asio::io_service io_service; tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port));