util/OptionParser: pass array of OptionDefs to constructor
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2017 The Music Player Daemon Project
|
||||
* Copyright 2003-2018 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@@ -19,43 +19,41 @@
|
||||
|
||||
#include "OptionParser.hxx"
|
||||
#include "OptionDef.hxx"
|
||||
#include "util/RuntimeError.hxx"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
bool
|
||||
OptionParser::CheckOption(const OptionDef &opt) const noexcept
|
||||
inline unsigned
|
||||
OptionParser::IdentifyOption(const char *s) const
|
||||
{
|
||||
assert(option != nullptr);
|
||||
assert(s != nullptr);
|
||||
assert(*s == '-');
|
||||
|
||||
if (is_long)
|
||||
return opt.HasLongOption() &&
|
||||
strcmp(option, opt.GetLongOption()) == 0;
|
||||
if (s[1] == '-') {
|
||||
for (const auto &i : options)
|
||||
if (i.HasLongOption() &&
|
||||
strcmp(s + 2, i.GetLongOption()) == 0)
|
||||
return &i - options.data;
|
||||
} else if (s[1] != 0 && s[2] == 0) {
|
||||
const char ch = s[1];
|
||||
for (const auto &i : options)
|
||||
if (i.HasShortOption() && ch == i.GetShortOption())
|
||||
return &i - options.data;
|
||||
}
|
||||
|
||||
return opt.HasShortOption() &&
|
||||
option[0] == opt.GetShortOption() &&
|
||||
option[1] == '\0';
|
||||
throw FormatRuntimeError("Unknown option: %s", s);
|
||||
}
|
||||
|
||||
bool
|
||||
OptionParser::ParseNext() noexcept
|
||||
int
|
||||
OptionParser::Next()
|
||||
{
|
||||
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;
|
||||
}
|
||||
if (arg[0] == '-')
|
||||
return IdentifyOption(arg);
|
||||
|
||||
*remaining_tail++ = arg;
|
||||
}
|
||||
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2003-2017 The Music Player Daemon Project
|
||||
* Copyright 2003-2018 The Music Player Daemon Project
|
||||
* http://www.musicpd.org
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
@@ -29,10 +29,9 @@ class OptionDef;
|
||||
*/
|
||||
class OptionParser
|
||||
{
|
||||
ConstBuffer<OptionDef> options;
|
||||
|
||||
ConstBuffer<const char *> args;
|
||||
const char *option;
|
||||
const char *option_raw;
|
||||
bool is_long = false;
|
||||
|
||||
const char **const remaining_head, **remaining_tail;
|
||||
|
||||
@@ -40,41 +39,23 @@ public:
|
||||
/**
|
||||
* Constructs #OptionParser.
|
||||
*/
|
||||
OptionParser(int _argc, char **_argv) noexcept
|
||||
:args(_argv + 1, _argc - 1),
|
||||
OptionParser(ConstBuffer<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) {}
|
||||
|
||||
/**
|
||||
* Gets the last parsed option.
|
||||
*/
|
||||
const char *GetOption() noexcept {
|
||||
return option_raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if current option is a specified option.
|
||||
*/
|
||||
bool CheckOption(const OptionDef &opt) const noexcept;
|
||||
|
||||
/**
|
||||
* Checks if current option is a specified option
|
||||
* or specified alternative option.
|
||||
*/
|
||||
bool CheckOption(const OptionDef &opt,
|
||||
const OptionDef &alt_opt) const noexcept {
|
||||
return CheckOption(opt) || CheckOption(alt_opt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses current command line entry.
|
||||
* 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
|
||||
* Throws on error.
|
||||
*
|
||||
* @return the index if an option was found, -1 if there are
|
||||
* no more options
|
||||
*/
|
||||
bool ParseNext() noexcept;
|
||||
int Next();
|
||||
|
||||
/**
|
||||
* Returns the remaining non-option arguments.
|
||||
@@ -82,6 +63,9 @@ public:
|
||||
ConstBuffer<const char *> GetRemaining() const noexcept {
|
||||
return {remaining_head, remaining_tail};
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned IdentifyOption(const char *s) const;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user