2013-11-24 12:19:51 +01:00
|
|
|
/*
|
2018-01-17 10:24:06 +01:00
|
|
|
* Copyright 2003-2018 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"
|
2018-01-17 10:24:06 +01:00
|
|
|
#include "util/RuntimeError.hxx"
|
2013-11-24 12:19:51 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-01-17 11:00:07 +01:00
|
|
|
inline OptionParser::Result
|
2018-01-17 10:24:06 +01:00
|
|
|
OptionParser::IdentifyOption(const char *s) const
|
2013-11-24 12:19:51 +01:00
|
|
|
{
|
2018-01-17 10:24:06 +01:00
|
|
|
assert(s != nullptr);
|
|
|
|
assert(*s == '-');
|
|
|
|
|
|
|
|
if (s[1] == '-') {
|
|
|
|
for (const auto &i : options)
|
|
|
|
if (i.HasLongOption() &&
|
|
|
|
strcmp(s + 2, i.GetLongOption()) == 0)
|
2018-01-17 11:00:07 +01:00
|
|
|
return {int(&i - options.data)};
|
2018-01-17 10:24:06 +01:00
|
|
|
} else if (s[1] != 0 && s[2] == 0) {
|
|
|
|
const char ch = s[1];
|
|
|
|
for (const auto &i : options)
|
|
|
|
if (i.HasShortOption() && ch == i.GetShortOption())
|
2018-01-17 11:00:07 +01:00
|
|
|
return {int(&i - options.data)};
|
2018-01-17 10:24:06 +01:00
|
|
|
}
|
2013-11-24 12:19:51 +01:00
|
|
|
|
2018-01-17 10:24:06 +01:00
|
|
|
throw FormatRuntimeError("Unknown option: %s", 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()) {
|
|
|
|
const char *arg = args.shift();
|
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:00:07 +01:00
|
|
|
return {-1};
|
2013-11-24 12:19:51 +01:00
|
|
|
}
|