2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "options.hpp"
19 option
client_options::null_opt
{};
20 const client_options::extended_option
client_options::null_ext_opt
{
21 client_options::null_opt
,
22 {}, {}, {},nullptr, false
25 void client_options::print_version() const {
26 std::cout
<< prog_name
<< " v" << prog_vers
<< " (libmemcached v" << LIBMEMCACHED_VERSION_STRING
<< ")"
30 void client_options::print_help() const {
32 std::cout
<< "\n\t" << prog_desc
<< "\n\n";
33 std::cout
<< "Usage:\n\t" << prog_name
<< " -[";
34 for (const auto &opt
: options
) {
35 if (!opt
.opt
.has_arg
&& opt
.opt
.val
!= '-') {
36 std::cout
<< (char) opt
.opt
.val
;
40 for (const auto &ext
: options
) {
41 if (ext
.opt
.has_arg
) {
42 std::cout
<< (char) ext
.opt
.val
;
43 if ((&ext
) != &*options
.rbegin())
47 std::cout
<< " <arg>] ";
50 std::cout
<< prog_argp
;
52 std::cout
<< "\n\nOptions:\n";
53 for (const auto &ext
: options
) {
54 if (ext
.opt
.val
== '-' || !(ext
.opt
.val
|| ext
.opt
.name
)) {
59 std::cout
<< "-" << (char) ext
.opt
.val
;
67 std::cout
<< "--" << ext
.opt
.name
<< " ";
71 if (ext
.opt
.has_arg
) {
72 if (ext
.opt
.has_arg
== optional_argument
) {
78 if (ext
.opt
.has_arg
== optional_argument
) {
84 std::cout
<< "\n\t\t" << ext
.help
<< "\n";
87 const auto &servers
= get("servers");
88 if (&servers
!= &null_ext_opt
) {
89 std::cout
<< "\nEnvironment:\n";
90 std::cout
<< "\tMEMCACHED_SERVERS=\n";
91 std::cout
<< "\t\tList of servers to use if `-s|--servers` was not provided.\n";
93 std::cout
<< std::endl
;
96 bool client_options::parse(int argc
, char **argv
, char ***argp
) {
97 /* extern */ optind
= 1;
98 auto &debug
= get("debug");
99 std::string short_opts
{};
100 std::vector
<option
> long_opts
{};
102 short_opts
.reserve(options
.size() * 3);
103 long_opts
.reserve(options
.size() + 1);
105 for (const auto &ext
: options
) {
107 short_opts
.push_back(ext
.opt
.val
);
108 for (int i
= 0; i
< ext
.opt
.has_arg
; ++i
) {
109 short_opts
.push_back(':');
113 long_opts
.push_back(ext
.opt
);
116 long_opts
.push_back({});
119 auto opt
= getopt_long(argc
, argv
, short_opts
.c_str(), long_opts
.data(), nullptr);
121 if (debug
.set
&& opt
> 0) {
122 std::cerr
<< "Processing option '" << (char) opt
<< "' (" << opt
<< ")\n";
129 *argp
= &argv
[optind
];
134 auto &ext_opt
= get(opt
);
137 ext_opt
.arg
= optarg
;
140 if (!ext_opt
.parse(*this, ext_opt
)) {
147 bool client_options::apply(memcached_st
*memc
) {
150 if (WSAStartup(MAKEWORD(2, 0), &wsaData
)) {
151 std::cerr
<< "Socket Initialization Error.\n";
156 for (auto &opt
: options
) {
158 if (!opt
.apply(*this, opt
, memc
)) {