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