Codechange: use designated initializers for OptionData and pass as span

This commit is contained in:
Rubidium 2024-04-10 21:49:39 +02:00 committed by rubidium42
parent 4f2412a272
commit e8a56db21d
5 changed files with 49 additions and 114 deletions

View File

@ -21,7 +21,7 @@
*/ */
int GetOptData::GetOpt() int GetOptData::GetOpt()
{ {
const OptionData *odata; OptionSpan::iterator odata;
char *s = this->cont; char *s = this->cont;
if (s == nullptr) { if (s == nullptr) {
@ -34,7 +34,7 @@ int GetOptData::GetOpt()
this->numleft--; this->numleft--;
/* Is it a long option? */ /* Is it a long option? */
for (odata = this->options; odata->flags != ODF_END; odata++) { for (odata = std::begin(this->options); odata != std::end(this->options); odata++) {
if (odata->longname != nullptr && !strcmp(odata->longname, s)) { // Long options always use the entire argument. if (odata->longname != nullptr && !strcmp(odata->longname, s)) { // Long options always use the entire argument.
this->cont = nullptr; this->cont = nullptr;
goto set_optval; goto set_optval;
@ -45,13 +45,13 @@ int GetOptData::GetOpt()
} }
/* Is it a short option? */ /* Is it a short option? */
for (odata = this->options; odata->flags != ODF_END; odata++) { for (odata = std::begin(this->options); odata != std::end(this->options); odata++) {
if (odata->shortname != '\0' && *s == odata->shortname) { if (odata->shortname != '\0' && *s == odata->shortname) {
this->cont = (s[1] != '\0') ? s + 1 : nullptr; this->cont = (s[1] != '\0') ? s + 1 : nullptr;
set_optval: // Handle option value of *odata . set_optval: // Handle option value of *odata .
this->opt = nullptr; this->opt = nullptr;
switch (odata->flags) { switch (odata->type) {
case ODF_NO_VALUE: case ODF_NO_VALUE:
return odata->id; return odata->id;
@ -63,10 +63,10 @@ set_optval: // Handle option value of *odata .
return odata->id; return odata->id;
} }
/* No more arguments, either return an error or a value-less option. */ /* 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; if (this->numleft == 0) return (odata->type == ODF_HAS_VALUE) ? -2 : odata->id;
/* Next argument looks like another option, let's not return it as option value. */ /* 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; if (odata->type == ODF_OPTIONAL_VALUE && this->argv[0][0] == '-') return odata->id;
this->opt = this->argv[0]; // Next argument is the option value. this->opt = this->argv[0]; // Next argument is the option value.
this->argv++; this->argv++;

View File

@ -11,27 +11,27 @@
#define GETOPTDATA_H #define GETOPTDATA_H
/** Flags of an option. */ /** Flags of an option. */
enum OptionDataFlags { enum OptionDataType : uint8_t {
ODF_NO_VALUE, ///< A plain option (no value attached to it). ODF_NO_VALUE, ///< A plain option (no value attached to it).
ODF_HAS_VALUE, ///< An option with a value. ODF_HAS_VALUE, ///< An option with a value.
ODF_OPTIONAL_VALUE, ///< An option with an optional value. ODF_OPTIONAL_VALUE, ///< An option with an optional value.
ODF_END, ///< Terminator (data is not parsed further).
}; };
/** Data of an option. */ /** Data of an option. */
struct OptionData { struct OptionData {
uint8_t id; ///< Unique identification of this option data, often the same as #shortname. OptionDataType type; ///< The type of option.
char shortname; ///< Short option letter if available, else use \c '\0'. char id; ///< Unique identification of this option data, often the same as #shortname.
uint16_t flags; ///< Option data flags. @see OptionDataFlags char shortname = '\0'; ///< Short option letter if available, else use \c '\0'.
const char *longname; ///< Long option name including '-'/'--' prefix, use \c nullptr if not available. const char *longname = nullptr; ///< Long option name including '-'/'--' prefix, use \c nullptr if not available.
}; };
/** Data storage for parsing command line options. */ /** Data storage for parsing command line options. */
struct GetOptData { struct GetOptData {
using OptionSpan = std::span<const OptionData>;
char *opt; ///< Option value, if available (else \c nullptr). char *opt; ///< Option value, if available (else \c nullptr).
int numleft; ///< Number of arguments left in #argv. int numleft; ///< Number of arguments left in #argv.
char **argv; ///< Remaining command line arguments. char **argv; ///< Remaining command line arguments.
const OptionData *options; ///< Command line option descriptions. OptionSpan options; ///< Command line option descriptions.
char *cont; ///< Next call to #GetOpt should start here (in the middle of an argument). char *cont; ///< Next call to #GetOpt should start here (in the middle of an argument).
/** /**
@ -40,7 +40,7 @@ struct GetOptData {
* @param argv Command line arguments, excluding the program name. * @param argv Command line arguments, excluding the program name.
* @param options Command line option descriptions. * @param options Command line option descriptions.
*/ */
GetOptData(int argc, char **argv, const OptionData *options) : GetOptData(int argc, char **argv, OptionSpan options) :
opt(nullptr), opt(nullptr),
numleft(argc), numleft(argc),
argv(argv), argv(argv),
@ -52,59 +52,4 @@ struct GetOptData {
int GetOpt(); int GetOpt();
}; };
/**
* 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 */ #endif /* GETOPTDATA_H */

View File

@ -478,36 +478,27 @@ void PostMainLoop()
extern void DedicatedFork(); extern void DedicatedFork();
#endif #endif
/** Options of OpenTTD. */ /**
static const OptionData _options[] = { * Create all the options that OpenTTD supports. Each option is
GETOPT_SHORT_VALUE('I'), * always a single character with no, an optional or a required value.
GETOPT_SHORT_VALUE('S'), * @return The available options.
GETOPT_SHORT_VALUE('M'), */
GETOPT_SHORT_VALUE('m'), static std::vector<OptionData> CreateOptions()
GETOPT_SHORT_VALUE('s'), {
GETOPT_SHORT_VALUE('v'), std::vector<OptionData> options;
GETOPT_SHORT_VALUE('b'), /* Options that require a parameter. */
GETOPT_SHORT_OPTVAL('D'), for (char c : "GIMPSbcmnpqrstv") options.push_back({ .type = ODF_HAS_VALUE, .id = c, .shortname = c });
GETOPT_SHORT_VALUE('n'),
GETOPT_SHORT_VALUE('p'),
GETOPT_SHORT_VALUE('P'),
#if !defined(_WIN32) #if !defined(_WIN32)
GETOPT_SHORT_NOVAL('f'), options.push_back({ .type = ODF_HAS_VALUE, .id = 'f', .shortname = 'f' });
#endif #endif
GETOPT_SHORT_VALUE('r'),
GETOPT_SHORT_VALUE('t'), /* Options with an optional parameter. */
GETOPT_SHORT_OPTVAL('d'), for (char c : "Ddg") options.push_back({ .type = ODF_OPTIONAL_VALUE, .id = c, .shortname = c });
GETOPT_SHORT_NOVAL('e'),
GETOPT_SHORT_OPTVAL('g'), /* Options without a parameter. */
GETOPT_SHORT_VALUE('G'), for (char c : "QXehx") options.push_back({ .type = ODF_NO_VALUE, .id = c, .shortname = c });
GETOPT_SHORT_VALUE('c'), return options;
GETOPT_SHORT_NOVAL('x'), }
GETOPT_SHORT_NOVAL('X'),
GETOPT_SHORT_VALUE('q'),
GETOPT_SHORT_NOVAL('h'),
GETOPT_SHORT_NOVAL('Q'),
GETOPT_END()
};
/** /**
* Main entry point for this lovely game. * Main entry point for this lovely game.
@ -538,7 +529,8 @@ int openttd_main(int argc, char *argv[])
_game_mode = GM_MENU; _game_mode = GM_MENU;
_switch_mode = SM_MENU; _switch_mode = SM_MENU;
GetOptData mgo(argc - 1, argv + 1, _options); auto options = CreateOptions();
GetOptData mgo(argc - 1, argv + 1, options);
int ret = 0; int ret = 0;
int i; int i;

View File

@ -385,12 +385,11 @@ static bool CompareFiles(const char *n1, const char *n2)
/** Options of settingsgen. */ /** Options of settingsgen. */
static const OptionData _opts[] = { static const OptionData _opts[] = {
GETOPT_NOVAL( 'h', "--help"), { .type = ODF_NO_VALUE, .id = 'h', .shortname = 'h', .longname = "--help" },
GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE), { .type = ODF_NO_VALUE, .id = 'h', .shortname = '?' },
GETOPT_VALUE( 'o', "--output"), { .type = ODF_HAS_VALUE, .id = 'o', .shortname = 'o', .longname = "--output" },
GETOPT_VALUE( 'b', "--before"), { .type = ODF_HAS_VALUE, .id = 'b', .shortname = 'b', .longname = "--before" },
GETOPT_VALUE( 'a', "--after"), { .type = ODF_HAS_VALUE, .id = 'a', .shortname = 'a', .longname = "--after" },
GETOPT_END(),
}; };
/** /**

View File

@ -312,16 +312,15 @@ struct LanguageFileWriter : LanguageWriter, FileWriter {
/** Options of strgen. */ /** Options of strgen. */
static const OptionData _opts[] = { static const OptionData _opts[] = {
GETOPT_GENERAL('C', '\0', "-export-commands", ODF_NO_VALUE), { .type = ODF_NO_VALUE, .id = 'C', .longname = "-export-commands" },
GETOPT_GENERAL('L', '\0', "-export-plurals", ODF_NO_VALUE), { .type = ODF_NO_VALUE, .id = 'L', .longname = "-export-plurals" },
GETOPT_GENERAL('P', '\0', "-export-pragmas", ODF_NO_VALUE), { .type = ODF_NO_VALUE, .id = 'P', .longname = "-export-pragmas" },
GETOPT_NOVAL( 't', "--todo"), { .type = ODF_NO_VALUE, .id = 't', .shortname = 't', .longname = "--todo" },
GETOPT_NOVAL( 'w', "--warning"), { .type = ODF_NO_VALUE, .id = 'w', .shortname = 'w', .longname = "--warning" },
GETOPT_NOVAL( 'h', "--help"), { .type = ODF_NO_VALUE, .id = 'h', .shortname = 'h', .longname = "--help" },
GETOPT_GENERAL('h', '?', nullptr, ODF_NO_VALUE), { .type = ODF_NO_VALUE, .id = 'h', .shortname = '?' },
GETOPT_VALUE( 's', "--source_dir"), { .type = ODF_HAS_VALUE, .id = 's', .shortname = 's', .longname = "--source_dir" },
GETOPT_VALUE( 'd', "--dest_dir"), { .type = ODF_HAS_VALUE, .id = 'd', .shortname = 'd', .longname = "--dest_dir" },
GETOPT_END(),
}; };
int CDECL main(int argc, char *argv[]) int CDECL main(int argc, char *argv[])