Merge branch 'master' into jgrpp
# Conflicts: # src/cheat_gui.cpp # src/industry_gui.cpp # src/linkgraph/linkgraphschedule.cpp # src/misc/getoptdata.h # src/music/dmusic.cpp # src/network/core/os_abstraction.cpp # src/newgrf_engine.cpp # src/openttd.cpp # src/order_gui.cpp # src/os/windows/win32.cpp # src/pathfinder/npf/queue.cpp # src/smallmap_gui.cpp # src/strgen/strgen.cpp
This commit is contained in:
@@ -21,23 +21,20 @@
|
||||
*/
|
||||
int GetOptData::GetOpt()
|
||||
{
|
||||
const OptionData *odata;
|
||||
|
||||
char *s = this->cont;
|
||||
const char *s = this->cont;
|
||||
if (s == nullptr) {
|
||||
if (this->numleft == 0) return -1; // No arguments left -> finished.
|
||||
if (this->arguments.empty()) return -1; // No arguments left -> finished.
|
||||
|
||||
s = this->argv[0];
|
||||
s = this->arguments[0];
|
||||
if (*s != '-') return -1; // No leading '-' -> not an option -> finished.
|
||||
|
||||
this->argv++;
|
||||
this->numleft--;
|
||||
this->arguments = this->arguments.subspan(1);
|
||||
|
||||
/* Is it a long option? */
|
||||
for (odata = this->options; odata->flags != ODF_END; odata++) {
|
||||
if (odata->longname != nullptr && !strcmp(odata->longname, s)) { // Long options always use the entire argument.
|
||||
for (auto &option : this->options) {
|
||||
if (option.longname != nullptr && !strcmp(option.longname, s)) { // Long options always use the entire argument.
|
||||
this->cont = nullptr;
|
||||
goto set_optval;
|
||||
return this->GetOpt(option);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,39 +42,41 @@ int GetOptData::GetOpt()
|
||||
}
|
||||
|
||||
/* Is it a short option? */
|
||||
for (odata = this->options; odata->flags != ODF_END; odata++) {
|
||||
if (odata->shortname != '\0' && *s == odata->shortname) {
|
||||
for (auto &option : this->options) {
|
||||
if (option.shortname != '\0' && *s == option.shortname) {
|
||||
this->cont = (s[1] != '\0') ? s + 1 : nullptr;
|
||||
|
||||
set_optval: // Handle option value of *odata .
|
||||
this->opt = nullptr;
|
||||
switch (odata->flags) {
|
||||
case ODF_NO_VALUE:
|
||||
return odata->id;
|
||||
|
||||
case ODF_HAS_VALUE:
|
||||
case ODF_OPTIONAL_VALUE:
|
||||
if (this->cont != nullptr) { // Remainder of the argument is the option value.
|
||||
this->opt = this->cont;
|
||||
this->cont = nullptr;
|
||||
return odata->id;
|
||||
}
|
||||
/* No more arguments, either return an error or a value-less option. */
|
||||
if (this->numleft == 0) return (odata->flags == ODF_HAS_VALUE) ? -2 : odata->id;
|
||||
|
||||
/* Next argument looks like another option, let's not return it as option value. */
|
||||
if (odata->flags == ODF_OPTIONAL_VALUE && this->argv[0][0] == '-') return odata->id;
|
||||
|
||||
this->opt = this->argv[0]; // Next argument is the option value.
|
||||
this->argv++;
|
||||
this->numleft--;
|
||||
return odata->id;
|
||||
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
return this->GetOpt(option);
|
||||
}
|
||||
}
|
||||
|
||||
return -2; // No other ways to interpret the text -> error.
|
||||
}
|
||||
|
||||
int GetOptData::GetOpt(const OptionData &option)
|
||||
{
|
||||
this->opt = nullptr;
|
||||
switch (option.type) {
|
||||
case ODF_NO_VALUE:
|
||||
return option.id;
|
||||
|
||||
case ODF_HAS_VALUE:
|
||||
case ODF_OPTIONAL_VALUE:
|
||||
if (this->cont != nullptr) { // Remainder of the argument is the option value.
|
||||
this->opt = this->cont;
|
||||
this->cont = nullptr;
|
||||
return option.id;
|
||||
}
|
||||
/* No more arguments, either return an error or a value-less option. */
|
||||
if (this->arguments.empty()) return (option.type == ODF_HAS_VALUE) ? -2 : option.id;
|
||||
|
||||
/* Next argument looks like another option, let's not return it as option value. */
|
||||
if (option.type == ODF_OPTIONAL_VALUE && this->arguments[0][0] == '-') return option.id;
|
||||
|
||||
this->opt = this->arguments[0]; // Next argument is the option value.
|
||||
this->arguments = this->arguments.subspan(1);
|
||||
return option.id;
|
||||
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,100 +11,39 @@
|
||||
#define GETOPTDATA_H
|
||||
|
||||
/** Flags of an option. */
|
||||
enum OptionDataFlags {
|
||||
enum OptionDataType : uint8_t {
|
||||
ODF_NO_VALUE, ///< A plain option (no value attached to it).
|
||||
ODF_HAS_VALUE, ///< An option with a value.
|
||||
ODF_OPTIONAL_VALUE, ///< An option with an optional value.
|
||||
ODF_END, ///< Terminator (data is not parsed further).
|
||||
};
|
||||
|
||||
/** Data of an option. */
|
||||
struct OptionData {
|
||||
uint8_t id; ///< Unique identification of this option data, often the same as #shortname.
|
||||
char shortname; ///< Short option letter if available, else use \c '\0'.
|
||||
uint16_t flags; ///< Option data flags. @see OptionDataFlags
|
||||
const char *longname; ///< Long option name including '-'/'--' prefix, use \c nullptr if not available.
|
||||
OptionDataType type; ///< The type of option.
|
||||
char id; ///< Unique identification of this option data, often the same as #shortname.
|
||||
char shortname = '\0'; ///< Short option letter if available, else use \c '\0'.
|
||||
const char *longname = nullptr; ///< Long option name including '-'/'--' prefix, use \c nullptr if not available.
|
||||
};
|
||||
|
||||
/** Data storage for parsing command line options. */
|
||||
struct GetOptData {
|
||||
char *opt; ///< Option value, if available (else \c nullptr).
|
||||
int numleft; ///< Number of arguments left in #argv.
|
||||
char **argv; ///< Remaining command line arguments.
|
||||
const OptionData *options; ///< Command line option descriptions.
|
||||
char *cont; ///< Next call to #GetOpt should start here (in the middle of an argument).
|
||||
using OptionSpan = std::span<const OptionData>;
|
||||
using ArgumentSpan = std::span<char * const>;
|
||||
|
||||
ArgumentSpan arguments; ///< Remaining command line arguments.
|
||||
const OptionSpan options; ///< Command line option descriptions.
|
||||
const char *opt = nullptr; ///< Option value, if available (else \c nullptr).
|
||||
const char *cont = nullptr; ///< Next call to #GetOpt should start here (in the middle of an argument).
|
||||
|
||||
/**
|
||||
* Constructor of the data store.
|
||||
* @param argc Number of command line arguments, excluding the program name.
|
||||
* @param argv Command line arguments, excluding the program name.
|
||||
* @param argument The command line arguments, excluding the program name.
|
||||
* @param options Command line option descriptions.
|
||||
*/
|
||||
GetOptData(int argc, char **argv, const OptionData *options) :
|
||||
opt(nullptr),
|
||||
numleft(argc),
|
||||
argv(argv),
|
||||
options(options),
|
||||
cont(nullptr)
|
||||
{
|
||||
}
|
||||
GetOptData(ArgumentSpan arguments, OptionSpan options) : arguments(arguments), options(options) {}
|
||||
|
||||
int GetOpt();
|
||||
int GetOpt(const OptionData &option);
|
||||
};
|
||||
|
||||
/**
|
||||
* General macro for creating an option.
|
||||
* @param id Identification of the option.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
* @param longname Long option name including leading '-' or '--'. Use \c nullptr if not used.
|
||||
* @param flags Flags of the option.
|
||||
*/
|
||||
#define GETOPT_GENERAL(id, shortname, longname, flags) { id, shortname, flags, longname }
|
||||
|
||||
/**
|
||||
* Short option without value.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
* @param longname Long option name including leading '-' or '--'. Use \c nullptr if not used.
|
||||
*/
|
||||
#define GETOPT_NOVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_NO_VALUE)
|
||||
|
||||
/**
|
||||
* Short option with value.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
* @param longname Long option name including leading '-' or '--'. Use \c nullptr if not used.
|
||||
*/
|
||||
#define GETOPT_VALUE(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_HAS_VALUE)
|
||||
|
||||
/**
|
||||
* Short option with optional value.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
* @param longname Long option name including leading '-' or '--'. Use \c nullptr if not used.
|
||||
* @note Options with optional values are hopelessly ambiguous, eg "-opt -value", avoid them.
|
||||
*/
|
||||
#define GETOPT_OPTVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_OPTIONAL_VALUE)
|
||||
|
||||
|
||||
/**
|
||||
* Short option without value.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
*/
|
||||
#define GETOPT_SHORT_NOVAL(shortname) GETOPT_NOVAL(shortname, nullptr)
|
||||
|
||||
/**
|
||||
* Short option with value.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
*/
|
||||
#define GETOPT_SHORT_VALUE(shortname) GETOPT_VALUE(shortname, nullptr)
|
||||
|
||||
/**
|
||||
* Short option with optional value.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
* @note Options with optional values are hopelessly ambiguous, eg "-opt -value", avoid them.
|
||||
*/
|
||||
#define GETOPT_SHORT_OPTVAL(shortname) GETOPT_OPTVAL(shortname, nullptr)
|
||||
|
||||
/** Option terminator. */
|
||||
#define GETOPT_END() { '\0', '\0', ODF_END, nullptr}
|
||||
|
||||
|
||||
#endif /* GETOPTDATA_H */
|
||||
|
||||
Reference in New Issue
Block a user