util/OptionParser: use std::span instead of ConstBuffer
This commit is contained in:
parent
9b427b3171
commit
e8667f99be
|
@ -22,6 +22,14 @@
|
|||
#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)
|
||||
{
|
||||
|
@ -31,7 +39,7 @@ OptionParser::CheckShiftValue(const char *s, const OptionDef &option)
|
|||
if (args.empty())
|
||||
throw FormatRuntimeError("Value expected after %s", s);
|
||||
|
||||
return args.shift();
|
||||
return Shift(args);
|
||||
}
|
||||
|
||||
inline OptionParser::Result
|
||||
|
@ -58,14 +66,14 @@ OptionParser::IdentifyOption(const char *s)
|
|||
else
|
||||
continue;
|
||||
|
||||
return {int(&i - options.data), value};
|
||||
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};
|
||||
return {int(&i - options.data()), value};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +85,7 @@ OptionParser::Result
|
|||
OptionParser::Next()
|
||||
{
|
||||
while (!args.empty()) {
|
||||
const char *arg = args.shift();
|
||||
const char *arg = Shift(args);
|
||||
if (arg[0] == '-')
|
||||
return IdentifyOption(arg);
|
||||
|
||||
|
|
|
@ -20,18 +20,18 @@
|
|||
#ifndef MPD_UTIL_OPTIONPARSER_HXX
|
||||
#define MPD_UTIL_OPTIONPARSER_HXX
|
||||
|
||||
#include "util/ConstBuffer.hxx"
|
||||
#include "OptionDef.hxx"
|
||||
|
||||
class OptionDef;
|
||||
#include <span>
|
||||
|
||||
/**
|
||||
* Command line option parser.
|
||||
*/
|
||||
class OptionParser
|
||||
{
|
||||
ConstBuffer<OptionDef> options;
|
||||
std::span<const OptionDef> options;
|
||||
|
||||
ConstBuffer<const char *> args;
|
||||
std::span<const char *const> args;
|
||||
|
||||
const char **const remaining_head, **remaining_tail;
|
||||
|
||||
|
@ -39,7 +39,7 @@ public:
|
|||
/**
|
||||
* Constructs #OptionParser.
|
||||
*/
|
||||
OptionParser(ConstBuffer<OptionDef> _options,
|
||||
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)),
|
||||
|
@ -66,7 +66,7 @@ public:
|
|||
/**
|
||||
* Returns the remaining non-option arguments.
|
||||
*/
|
||||
ConstBuffer<const char *> GetRemaining() const noexcept {
|
||||
std::span<const char *const> GetRemaining() const noexcept {
|
||||
return {remaining_head, remaining_tail};
|
||||
}
|
||||
|
||||
|
|
|
@ -79,7 +79,7 @@ ParseCommandLine(int argc, char **argv)
|
|||
}
|
||||
|
||||
auto args = option_parser.GetRemaining();
|
||||
if (args.size != 2)
|
||||
if (args.size() != 2)
|
||||
throw std::runtime_error("Usage: RunChromaprint [--verbose] [--config=FILE] DECODER URI");
|
||||
|
||||
c.decoder = args[0];
|
||||
|
|
|
@ -82,7 +82,7 @@ ParseCommandLine(int argc, char **argv)
|
|||
}
|
||||
|
||||
auto args = option_parser.GetRemaining();
|
||||
if (args.size != 2)
|
||||
if (args.size() != 2)
|
||||
throw std::runtime_error("Usage: run_convert IN_FORMAT OUT_FORMAT <IN >OUT");
|
||||
|
||||
c.in_audio_format = ParseAudioFormat(args[0], false);
|
||||
|
|
|
@ -87,7 +87,7 @@ ParseCommandLine(int argc, char **argv)
|
|||
}
|
||||
|
||||
auto args = option_parser.GetRemaining();
|
||||
if (args.size != 2)
|
||||
if (args.size() != 2)
|
||||
throw std::runtime_error("Usage: run_decoder [--verbose] [--config=FILE] DECODER URI");
|
||||
|
||||
c.decoder = args[0];
|
||||
|
|
|
@ -124,7 +124,7 @@ ParseCommandLine(int argc, char **argv)
|
|||
}
|
||||
|
||||
auto args = option_parser.GetRemaining();
|
||||
if (args.size != 1)
|
||||
if (args.size() != 1)
|
||||
throw std::runtime_error("Usage: run_input [--verbose] [--config=FILE] [--scan] [--chunk-size=BYTES] URI");
|
||||
|
||||
c.uri = args.front();
|
||||
|
|
|
@ -76,13 +76,13 @@ ParseCommandLine(int argc, char **argv)
|
|||
}
|
||||
|
||||
auto args = option_parser.GetRemaining();
|
||||
if (args.size < 2 || args.size > 3)
|
||||
if (args.size() < 2 || args.size() > 3)
|
||||
throw std::runtime_error("Usage: run_output CONFIG NAME [FORMAT] <IN");
|
||||
|
||||
c.config_path = args[0];
|
||||
c.output_name = args[1];
|
||||
|
||||
if (args.size > 2)
|
||||
if (args.size() > 2)
|
||||
c.audio_format = ParseAudioFormat(args[2], false);
|
||||
|
||||
return c;
|
||||
|
|
Loading…
Reference in New Issue