2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2008-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <cstdlib> /* exit() */
|
|
|
|
#include <cstring>
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <string>
|
2021-10-01 17:47:23 +00:00
|
|
|
#include <string_view>
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2023-04-16 20:34:19 +00:00
|
|
|
#include <fmt/core.h>
|
2022-07-30 17:54:22 +00:00
|
|
|
|
2023-04-14 19:33:23 +00:00
|
|
|
#include "libtransmission/tr-getopt.h"
|
|
|
|
#include "libtransmission/utils.h"
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2022-07-30 17:54:22 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
int tr_optind = 1;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
[[nodiscard]] constexpr std::string_view getArgName(tr_option const* opt)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!opt->has_arg)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-30 17:54:22 +00:00
|
|
|
return ""sv;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-21 17:40:36 +00:00
|
|
|
|
|
|
|
if (opt->argName != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 17:40:36 +00:00
|
|
|
return opt->argName;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-09 15:31:35 +00:00
|
|
|
|
2022-07-30 17:54:22 +00:00
|
|
|
return "<args>"sv;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
[[nodiscard]] constexpr size_t get_next_line_len(std::string_view description, size_t maxlen)
|
2010-06-16 14:05:18 +00:00
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
auto len = std::size(description);
|
|
|
|
if (len > maxlen)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-01 17:47:23 +00:00
|
|
|
description.remove_suffix(len - maxlen);
|
|
|
|
auto const pos = description.rfind(' ');
|
|
|
|
len = pos != std::string_view::npos ? pos : maxlen;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-01 17:47:23 +00:00
|
|
|
return len;
|
2010-06-16 14:05:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
void getopts_usage_line(tr_option const* const opt, size_t long_width, size_t short_width, size_t arg_width)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2022-07-30 17:54:22 +00:00
|
|
|
auto const long_name = std::string_view{ opt->longName != nullptr ? opt->longName : "" };
|
|
|
|
auto const short_name = std::string_view{ opt->shortName != nullptr ? opt->shortName : "" };
|
|
|
|
auto const arg = getArgName(opt);
|
|
|
|
|
|
|
|
fmt::print(
|
|
|
|
FMT_STRING(" {:s}{:<{}s} {:s}{:<{}s} {:<{}s}"),
|
|
|
|
std::empty(short_name) ? " "sv : "-"sv,
|
|
|
|
short_name,
|
|
|
|
short_width,
|
|
|
|
std::empty(long_name) ? " "sv : "--"sv,
|
|
|
|
long_name,
|
|
|
|
long_width,
|
|
|
|
arg,
|
|
|
|
arg_width);
|
|
|
|
|
|
|
|
auto const d_indent = short_width + long_width + arg_width + 6U;
|
|
|
|
auto const d_width = 80U - d_indent;
|
|
|
|
|
|
|
|
auto description = std::string_view{ opt->description };
|
|
|
|
auto len = get_next_line_len(description, d_width);
|
|
|
|
fmt::print(FMT_STRING("{:s}\n"), description.substr(0, len));
|
|
|
|
description.remove_prefix(len);
|
2023-06-30 14:49:58 +00:00
|
|
|
description = tr_strv_strip(description);
|
2022-07-30 17:54:22 +00:00
|
|
|
|
|
|
|
auto const indent = std::string(d_indent, ' ');
|
|
|
|
while ((len = get_next_line_len(description, d_width)) != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-30 17:54:22 +00:00
|
|
|
fmt::print(FMT_STRING("{:s}{:s}\n"), indent, description.substr(0, len));
|
|
|
|
description.remove_prefix(len);
|
2023-06-30 14:49:58 +00:00
|
|
|
description = tr_strv_strip(description);
|
2010-06-16 14:05:18 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
void maxWidth(struct tr_option const* o, size_t& long_width, size_t& short_width, size_t& arg_width)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (o->longName != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-30 17:54:22 +00:00
|
|
|
long_width = std::max(long_width, strlen(o->longName));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (o->shortName != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-30 17:54:22 +00:00
|
|
|
short_width = std::max(short_width, strlen(o->shortName));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2022-07-30 17:54:22 +00:00
|
|
|
if (auto const arg = getArgName(o); !std::empty(arg))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-07-30 17:54:22 +00:00
|
|
|
arg_width = std::max(arg_width, std::size(arg));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-09 15:31:35 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
tr_option const* findOption(tr_option const* opts, char const* str, char const** setme_arg)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t matchlen = 0;
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* arg = nullptr;
|
|
|
|
tr_option const* match = nullptr;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
/* find the longest matching option */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_option const* o = opts; o->val != 0; ++o)
|
2008-07-09 19:39:40 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
size_t len = o->longName != nullptr ? strlen(o->longName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (matchlen < len && str[0] == '-' && str[1] == '-' && strncmp(str + 2, o->longName, len) == 0 &&
|
2017-04-19 12:04:45 +00:00
|
|
|
(str[len + 2] == '\0' || (o->has_arg && str[len + 2] == '=')))
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2021-09-15 00:18:09 +00:00
|
|
|
arg = str[len + 2] == '=' ? str + len + 3 : nullptr;
|
2008-07-21 19:24:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
len = o->shortName != nullptr ? strlen(o->shortName) : 0;
|
2008-07-21 19:24:35 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (matchlen < len && str[0] == '-' && strncmp(str + 1, o->shortName, len) == 0 && (str[len + 1] == '\0' || o->has_arg))
|
2008-07-21 19:24:35 +00:00
|
|
|
{
|
|
|
|
matchlen = len;
|
|
|
|
match = o;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
switch (str[len + 1])
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case '\0':
|
2021-09-15 00:18:09 +00:00
|
|
|
arg = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case '=':
|
|
|
|
arg = str + len + 2;
|
|
|
|
break;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
arg = str + len + 1;
|
|
|
|
break;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (setme_arg != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-21 19:24:35 +00:00
|
|
|
*setme_arg = arg;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-21 19:24:35 +00:00
|
|
|
|
|
|
|
return match;
|
2008-07-08 14:29:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 04:16:22 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void tr_getopt_usage(char const* app_name, char const* description, struct tr_option const* opts)
|
|
|
|
{
|
|
|
|
auto long_width = size_t{ 0 };
|
|
|
|
auto short_width = size_t{ 0 };
|
|
|
|
auto arg_width = size_t{ 0 };
|
|
|
|
|
|
|
|
for (tr_option const* o = opts; o->val != 0; ++o)
|
|
|
|
{
|
|
|
|
maxWidth(o, long_width, short_width, arg_width);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto const help = tr_option{ -1, "help", "Display this help page and exit", "h", false, nullptr };
|
|
|
|
maxWidth(&help, long_width, short_width, arg_width);
|
|
|
|
|
|
|
|
if (description == nullptr)
|
|
|
|
{
|
|
|
|
description = "Usage: %s [options]";
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(description, app_name);
|
|
|
|
printf("\n\nOptions:\n");
|
|
|
|
getopts_usage_line(&help, long_width, short_width, arg_width);
|
|
|
|
|
|
|
|
for (tr_option const* o = opts; o->val != 0; ++o)
|
|
|
|
{
|
|
|
|
getopts_usage_line(o, long_width, short_width, arg_width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_getopt(char const* usage, int argc, char const* const* argv, tr_option const* opts, char const** setme_optarg)
|
2008-07-08 14:29:05 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char const* arg = nullptr;
|
|
|
|
tr_option const* o = nullptr;
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
*setme_optarg = nullptr;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2008-07-08 14:29:05 +00:00
|
|
|
/* handle the builtin 'help' option */
|
2017-05-13 22:38:31 +00:00
|
|
|
for (int i = 1; i < argc; ++i)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_getopt_usage(argv[0], usage, opts);
|
|
|
|
exit(0);
|
2008-07-09 15:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
/* out of options? */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (argc == 1 || tr_optind >= argc)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_DONE;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o = findOption(opts, argv[tr_optind], &arg);
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (o == nullptr)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
/* let the user know we got an unknown option... */
|
2008-07-09 15:31:35 +00:00
|
|
|
*setme_optarg = argv[tr_optind++];
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_UNK;
|
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!o->has_arg)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
/* no argument needed for this option, so we're done */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (arg != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
*setme_optarg = nullptr;
|
2008-07-21 19:24:35 +00:00
|
|
|
++tr_optind;
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|
|
|
|
|
2008-07-21 19:24:35 +00:00
|
|
|
/* option needed an argument, and it was embedded in this string */
|
2021-09-15 00:18:09 +00:00
|
|
|
if (arg != nullptr)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2008-07-21 19:24:35 +00:00
|
|
|
*setme_optarg = arg;
|
|
|
|
++tr_optind;
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* throw an error if the option needed an argument but didn't get one */
|
2012-12-05 17:29:46 +00:00
|
|
|
if (++tr_optind >= argc)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (findOption(opts, argv[tr_optind], nullptr) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2008-07-08 14:29:05 +00:00
|
|
|
return TR_OPT_ERR;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2008-07-08 14:29:05 +00:00
|
|
|
|
2008-07-09 15:31:35 +00:00
|
|
|
*setme_optarg = argv[tr_optind++];
|
2008-07-08 14:29:05 +00:00
|
|
|
return o->val;
|
|
|
|
}
|