util/OptionParser: loop in ParseNext() until a new option is found
This commit is contained in:
parent
68f660dbcc
commit
5ab086e337
|
@ -313,9 +313,7 @@ ParseCommandLine(int argc, char **argv, struct options *options)
|
|||
|
||||
// First pass: handle command line options
|
||||
OptionParser parser(argc, argv);
|
||||
while (parser.HasEntries()) {
|
||||
if (!parser.ParseNext())
|
||||
continue;
|
||||
while (parser.ParseNext()) {
|
||||
if (parser.CheckOption(opt_kill)) {
|
||||
options->kill = true;
|
||||
continue;
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
#ifndef MPD_UTIL_OPTIONDEF_HXX
|
||||
#define MPD_UTIL_OPTIONDEF_HXX
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
/**
|
||||
* Command line option definition.
|
||||
*/
|
||||
|
|
|
@ -39,23 +39,23 @@ OptionParser::CheckOption(const OptionDef &opt) const noexcept
|
|||
bool
|
||||
OptionParser::ParseNext() noexcept
|
||||
{
|
||||
assert(HasEntries());
|
||||
const char *arg = args.shift();
|
||||
if (arg[0] == '-') {
|
||||
if (arg[1] == '-') {
|
||||
option = arg + 2;
|
||||
is_long = true;
|
||||
while (!args.empty()) {
|
||||
const char *arg = args.shift();
|
||||
if (arg[0] == '-') {
|
||||
if (arg[1] == '-') {
|
||||
option = arg + 2;
|
||||
is_long = true;
|
||||
}
|
||||
else {
|
||||
option = arg + 1;
|
||||
is_long = false;
|
||||
}
|
||||
option_raw = arg;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
option = arg + 1;
|
||||
is_long = false;
|
||||
}
|
||||
option_raw = arg;
|
||||
return true;
|
||||
|
||||
*remaining_tail++ = arg;
|
||||
}
|
||||
|
||||
option = nullptr;
|
||||
option_raw = nullptr;
|
||||
*remaining_tail++ = arg;
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -22,8 +22,6 @@
|
|||
|
||||
#include "util/ConstBuffer.hxx"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
class OptionDef;
|
||||
|
||||
/**
|
||||
|
@ -32,8 +30,8 @@ class OptionDef;
|
|||
class OptionParser
|
||||
{
|
||||
ConstBuffer<const char *> args;
|
||||
const char *option = nullptr;
|
||||
const char *option_raw = nullptr;
|
||||
const char *option;
|
||||
const char *option_raw;
|
||||
bool is_long = false;
|
||||
|
||||
const char **const remaining_head, **remaining_tail;
|
||||
|
@ -42,23 +40,15 @@ public:
|
|||
/**
|
||||
* Constructs #OptionParser.
|
||||
*/
|
||||
constexpr OptionParser(int _argc, char **_argv) noexcept
|
||||
OptionParser(int _argc, char **_argv) noexcept
|
||||
:args(_argv + 1, _argc - 1),
|
||||
remaining_head(const_cast<const char **>(_argv + 1)),
|
||||
remaining_tail(remaining_head) {}
|
||||
|
||||
/**
|
||||
* Checks if there are command line entries to process.
|
||||
*/
|
||||
constexpr bool HasEntries() const noexcept {
|
||||
return !args.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last parsed option.
|
||||
*/
|
||||
const char *GetOption() noexcept {
|
||||
assert(option_raw != nullptr);
|
||||
return option_raw;
|
||||
}
|
||||
|
||||
|
@ -78,9 +68,11 @@ public:
|
|||
|
||||
/**
|
||||
* Parses current command line entry.
|
||||
* Returns true on success, false otherwise.
|
||||
* Regardless of result, advances current position to the next
|
||||
* command line entry.
|
||||
*
|
||||
* @return true if an option was found, false if there are no
|
||||
* more options
|
||||
*/
|
||||
bool ParseNext() noexcept;
|
||||
|
||||
|
|
Loading…
Reference in New Issue