2013-11-24 12:19:51 +01:00
|
|
|
/*
|
2022-07-14 17:58:12 +02:00
|
|
|
* Copyright 2003-2022 The Music Player Daemon Project
|
2013-11-24 12:19:51 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "OptionParser.hxx"
|
|
|
|
#include "OptionDef.hxx"
|
2022-11-28 21:58:21 +01:00
|
|
|
#include "lib/fmt/RuntimeError.hxx"
|
2018-01-17 11:14:57 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2013-11-24 12:19:51 +01:00
|
|
|
|
2022-07-04 19:01:11 +02:00
|
|
|
static const char *
|
|
|
|
Shift(std::span<const char *const> &s) noexcept
|
|
|
|
{
|
|
|
|
const char *value = s.front();
|
|
|
|
s = s.subspan(1);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-01-17 11:14:57 +01:00
|
|
|
inline const char *
|
|
|
|
OptionParser::CheckShiftValue(const char *s, const OptionDef &option)
|
|
|
|
{
|
|
|
|
if (!option.HasValue())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (args.empty())
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("Value expected after {}", s);
|
2018-01-17 11:14:57 +01:00
|
|
|
|
2022-07-04 19:01:11 +02:00
|
|
|
return Shift(args);
|
2018-01-17 11:14:57 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 11:00:07 +01:00
|
|
|
inline OptionParser::Result
|
2018-01-17 11:14:57 +01:00
|
|
|
OptionParser::IdentifyOption(const char *s)
|
2013-11-24 12:19:51 +01:00
|
|
|
{
|
2018-01-17 10:24:06 +01:00
|
|
|
assert(s != nullptr);
|
|
|
|
assert(*s == '-');
|
|
|
|
|
|
|
|
if (s[1] == '-') {
|
2018-01-17 11:14:57 +01:00
|
|
|
for (const auto &i : options) {
|
|
|
|
if (!i.HasLongOption())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *t = StringAfterPrefix(s + 2, i.GetLongOption());
|
|
|
|
if (t == nullptr)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *value;
|
|
|
|
|
|
|
|
if (*t == 0)
|
|
|
|
value = CheckShiftValue(s, i);
|
|
|
|
else if (*t == '=')
|
|
|
|
value = t + 1;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
|
2022-07-04 19:01:11 +02:00
|
|
|
return {int(&i - options.data()), value};
|
2018-01-17 11:14:57 +01:00
|
|
|
}
|
2018-01-17 10:24:06 +01:00
|
|
|
} else if (s[1] != 0 && s[2] == 0) {
|
|
|
|
const char ch = s[1];
|
2018-01-17 11:14:57 +01:00
|
|
|
for (const auto &i : options) {
|
|
|
|
if (i.HasShortOption() && ch == i.GetShortOption()) {
|
|
|
|
const char *value = CheckShiftValue(s, i);
|
2022-07-04 19:01:11 +02:00
|
|
|
return {int(&i - options.data()), value};
|
2018-01-17 11:14:57 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-17 10:24:06 +01:00
|
|
|
}
|
2013-11-24 12:19:51 +01:00
|
|
|
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("Unknown option: {}", s);
|
2013-11-24 12:19:51 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 11:00:07 +01:00
|
|
|
OptionParser::Result
|
2018-01-17 10:24:06 +01:00
|
|
|
OptionParser::Next()
|
2013-11-24 12:19:51 +01:00
|
|
|
{
|
2018-01-16 11:25:18 +01:00
|
|
|
while (!args.empty()) {
|
2022-07-04 19:01:11 +02:00
|
|
|
const char *arg = Shift(args);
|
2018-01-17 10:24:06 +01:00
|
|
|
if (arg[0] == '-')
|
|
|
|
return IdentifyOption(arg);
|
2018-01-16 11:25:18 +01:00
|
|
|
|
|
|
|
*remaining_tail++ = arg;
|
2013-11-24 12:19:51 +01:00
|
|
|
}
|
2018-01-16 11:03:04 +01:00
|
|
|
|
2018-01-17 11:14:57 +01:00
|
|
|
return {-1, nullptr};
|
2013-11-24 12:19:51 +01:00
|
|
|
}
|