util/OptionParser: move to cmdline/

This library will depend on libfmt, and libutil shouldn't depend on
any library.
This commit is contained in:
Max Kellermann
2022-11-29 11:27:59 +01:00
parent cf3f3a7750
commit 45b13fc2a6
13 changed files with 28 additions and 15 deletions

79
src/cmdline/OptionDef.hxx Normal file
View File

@@ -0,0 +1,79 @@
/*
* Copyright 2003-2022 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_UTIL_OPTIONDEF_HXX
#define MPD_UTIL_OPTIONDEF_HXX
#include <cassert>
/**
* Command line option definition.
*/
class OptionDef
{
const char *long_option;
char short_option;
bool has_value = false;
const char *desc;
public:
constexpr OptionDef(const char *_long_option, const char *_desc)
: long_option(_long_option),
short_option(0),
desc(_desc) { }
constexpr OptionDef(const char *_long_option,
char _short_option, const char *_desc)
: long_option(_long_option),
short_option(_short_option),
desc(_desc) { }
constexpr OptionDef(const char *_long_option,
char _short_option, bool _has_value,
const char *_desc) noexcept
:long_option(_long_option),
short_option(_short_option),
has_value(_has_value),
desc(_desc) {}
constexpr bool HasLongOption() const { return long_option != nullptr; }
constexpr bool HasShortOption() const { return short_option != 0; }
constexpr bool HasValue() const noexcept {
return has_value;
}
constexpr bool HasDescription() const { return desc != nullptr; }
const char *GetLongOption() const {
assert(HasLongOption());
return long_option;
}
char GetShortOption() const {
assert(HasShortOption());
return short_option;
}
const char *GetDescription() const {
assert(HasDescription());
return desc;
}
};
#endif

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2003-2022 The Music Player Daemon Project
* 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"
#include "util/RuntimeError.hxx"
#include "util/StringCompare.hxx"
static const char *
Shift(std::span<const char *const> &s) noexcept
{
const char *value = s.front();
s = s.subspan(1);
return value;
}
inline const char *
OptionParser::CheckShiftValue(const char *s, const OptionDef &option)
{
if (!option.HasValue())
return nullptr;
if (args.empty())
throw FormatRuntimeError("Value expected after %s", s);
return Shift(args);
}
inline OptionParser::Result
OptionParser::IdentifyOption(const char *s)
{
assert(s != nullptr);
assert(*s == '-');
if (s[1] == '-') {
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;
return {int(&i - options.data()), value};
}
} else if (s[1] != 0 && s[2] == 0) {
const char ch = s[1];
for (const auto &i : options) {
if (i.HasShortOption() && ch == i.GetShortOption()) {
const char *value = CheckShiftValue(s, i);
return {int(&i - options.data()), value};
}
}
}
throw FormatRuntimeError("Unknown option: %s", s);
}
OptionParser::Result
OptionParser::Next()
{
while (!args.empty()) {
const char *arg = Shift(args);
if (arg[0] == '-')
return IdentifyOption(arg);
*remaining_tail++ = arg;
}
return {-1, nullptr};
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2003-2022 The Music Player Daemon Project
* 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.
*/
#ifndef MPD_UTIL_OPTIONPARSER_HXX
#define MPD_UTIL_OPTIONPARSER_HXX
#include "OptionDef.hxx"
#include <span>
/**
* Command line option parser.
*/
class OptionParser
{
std::span<const OptionDef> options;
std::span<const char *const> args;
const char **const remaining_head, **remaining_tail;
public:
/**
* Constructs #OptionParser.
*/
OptionParser(std::span<const OptionDef> _options,
int _argc, char **_argv) noexcept
:options(_options), args(_argv + 1, _argc - 1),
remaining_head(const_cast<const char **>(_argv + 1)),
remaining_tail(remaining_head) {}
struct Result {
int index;
const char *value;
constexpr operator bool() const noexcept {
return index >= 0;
}
};
/**
* Parses current command line entry.
* Regardless of result, advances current position to the next
* command line entry.
*
* Throws on error.
*/
Result Next();
/**
* Returns the remaining non-option arguments.
*/
std::span<const char *const> GetRemaining() const noexcept {
return {remaining_head, remaining_tail};
}
private:
const char *CheckShiftValue(const char *s, const OptionDef &option);
Result IdentifyOption(const char *s);
};
#endif

9
src/cmdline/meson.build Normal file
View File

@@ -0,0 +1,9 @@
cmdline = static_library(
'cmdline',
'OptionParser.cxx',
include_directories: inc,
)
cmdline_dep = declare_dependency(
link_with: cmdline,
)