util/OptionParser: use ConstBuffer<const char*> to store argv/argc

This commit is contained in:
Max Kellermann 2018-01-16 10:11:06 +01:00
parent 4a304bf34e
commit 23dff4f209
2 changed files with 6 additions and 7 deletions

View File

@ -40,9 +40,7 @@ bool
OptionParser::ParseNext() noexcept OptionParser::ParseNext() noexcept
{ {
assert(HasEntries()); assert(HasEntries());
const char *arg = *argv; const char *arg = args.shift();
++argv;
--argc;
if (arg[0] == '-') { if (arg[0] == '-') {
if (arg[1] == '-') { if (arg[1] == '-') {
option = arg + 2; option = arg + 2;

View File

@ -20,6 +20,8 @@
#ifndef MPD_UTIL_OPTIONPARSER_HXX #ifndef MPD_UTIL_OPTIONPARSER_HXX
#define MPD_UTIL_OPTIONPARSER_HXX #define MPD_UTIL_OPTIONPARSER_HXX
#include "util/ConstBuffer.hxx"
#include <assert.h> #include <assert.h>
class OptionDef; class OptionDef;
@ -29,8 +31,7 @@ class OptionDef;
*/ */
class OptionParser class OptionParser
{ {
int argc; ConstBuffer<const char *> args;
char *const*argv;
const char *option = nullptr; const char *option = nullptr;
const char *option_raw = nullptr; const char *option_raw = nullptr;
bool is_long = false; bool is_long = false;
@ -40,13 +41,13 @@ public:
* Constructs #OptionParser. * Constructs #OptionParser.
*/ */
constexpr OptionParser(int _argc, char *const*_argv) noexcept constexpr OptionParser(int _argc, char *const*_argv) noexcept
:argc(_argc - 1), argv(_argv + 1) {} :args(_argv + 1, _argc - 1) {}
/** /**
* Checks if there are command line entries to process. * Checks if there are command line entries to process.
*/ */
constexpr bool HasEntries() const noexcept { constexpr bool HasEntries() const noexcept {
return argc > 0; return !args.empty();
} }
/** /**