From: Michael Wallner Date: Mon, 16 Nov 2020 22:46:40 +0000 (+0100) Subject: bin: consolidate clients X-Git-Tag: 1.1.0-beta1~109 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=2e9eb0803f1fc81bfe6c3e2dda0ed2cbe1aa6a76;p=awesomized%2Flibmemcached bin: consolidate clients --- diff --git a/src/bin/common/CMakeLists.txt b/src/bin/common/CMakeLists.txt index b068045f..7a65bedd 100644 --- a/src/bin/common/CMakeLists.txt +++ b/src/bin/common/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(libclient_common STATIC utilities.cc generator.cc) +add_library(libclient_common STATIC utilities.cc generator.cc options.cpp) add_library(client_common ALIAS libclient_common) target_link_libraries(libclient_common PUBLIC libmemcached) target_include_directories(libclient_common PUBLIC diff --git a/src/bin/common/options.cpp b/src/bin/common/options.cpp new file mode 100644 index 00000000..4583e7b7 --- /dev/null +++ b/src/bin/common/options.cpp @@ -0,0 +1,164 @@ +/* + +--------------------------------------------------------------------+ + | libmemcached - C/C++ Client Library for memcached | + +--------------------------------------------------------------------+ + | Redistribution and use in source and binary forms, with or without | + | modification, are permitted under the terms of the BSD license. | + | You should have received a copy of the license in a bundled file | + | named LICENSE; in case you did not receive a copy you can review | + | the terms online at: https://opensource.org/licenses/BSD-3-Clause | + +--------------------------------------------------------------------+ + | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ | + | Copyright (c) 2020 Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "options.hpp" +#include + +option client_options::null_opt{}; +const client_options::extended_option client_options::null_ext_opt{ + client_options::null_opt, + {}, {}, {},nullptr, false +}; + +void client_options::print_version() const { + std::cout << prog_name << " v" << prog_vers << " (libmemcached v" << LIBMEMCACHED_VERSION_STRING << ")" + << std::endl; +} + +void client_options::print_help() const { + print_version(); + std::cout << "\n\t" << prog_desc << "\n\n"; + std::cout << "Usage:\n\t" << prog_name << " -["; + for (const auto &opt : options) { + if (!opt.opt.has_arg && opt.opt.val != '-') { + std::cout << (char) opt.opt.val; + } + } + std::cout << "] [-"; + for (const auto &ext : options) { + if (ext.opt.has_arg) { + std::cout << (char) ext.opt.val; + if ((&ext) != &*options.rbegin()) + std::cout << '|'; + } + } + std::cout << " ] "; + + if (prog_argp) { + std::cout << prog_argp; + } + std::cout << "\n\nOptions:\n"; + for (const auto &ext : options) { + if (ext.opt.val == '-' || !(ext.opt.val || ext.opt.name)) { + continue; + } + std::cout << "\t"; + if (ext.opt.val) { + std::cout << "-" << (char) ext.opt.val; + if (ext.opt.name) { + std::cout << "|"; + } + } else { + std::cout << " "; + } + if (ext.opt.name) { + std::cout << "--" << ext.opt.name << " "; + } else { + std::cout << " "; + } + if (ext.opt.has_arg) { + if (ext.opt.has_arg == optional_argument) { + std::cout << "["; + } else { + std::cout << "<"; + } + std::cout << "arg"; + if (ext.opt.has_arg == optional_argument) { + std::cout << "]"; + } else { + std::cout << ">"; + } + } + std::cout << "\n\t\t" << ext.help << "\n"; + } + + const auto &servers = get("servers"); + if (&servers != &null_ext_opt) { + std::cout << "\nEnvironment:\n"; + std::cout << "\tMEMCACHED_SERVERS=\n"; + std::cout << "\t\tList of servers to use if `-s|--servers` was not provided.\n"; + } + std::cout << std::endl; +} + +bool client_options::parse(int argc, char **argv, char ***argp) { + /* extern */ optind = 1; + auto &debug = get("debug"); + std::string short_opts{}; + std::vector