(svn r22098) -Codechange: Have an array of option data, use it in the option parsing routine. Adapt openttd option processing too.
This commit is contained in:
@@ -12,13 +12,29 @@
|
||||
#ifndef GETOPTDATA_H
|
||||
#define GETOPTDATA_H
|
||||
|
||||
/** Flags of an option. */
|
||||
enum OptionDataFlags {
|
||||
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 {
|
||||
byte id; ///< Unique identification of this option data, often the same as #shortname.
|
||||
char shortname; ///< Short option letter if available, else use \c '\0'.
|
||||
uint16 flags; ///< Option data flags. @see OptionDataFlags
|
||||
const char *longname; ///< Long option name including '-'/'--' prefix, use \c NULL if not available.
|
||||
};
|
||||
|
||||
/** Data storage for parsing command line options. */
|
||||
struct GetOptData {
|
||||
char *opt; ///< Option value, if available (else \c NULL).
|
||||
int numleft; ///< Number of arguments left in #argv.
|
||||
char **argv; ///< Remaining command line arguments.
|
||||
const char *options; ///< Command line option descriptions.
|
||||
char *cont; ///< Next call to #MyGetOpt should start here (in the middle of an argument).
|
||||
char *opt; ///< Option value, if available (else \c NULL).
|
||||
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 #MyGetOpt should start here (in the middle of an argument).
|
||||
|
||||
/**
|
||||
* Constructor of the data store.
|
||||
@@ -26,7 +42,7 @@ struct GetOptData {
|
||||
* @param argv Command line arguments, excluding the program name.
|
||||
* @param options Command line option descriptions.
|
||||
*/
|
||||
GetOptData(int argc, char **argv, const char *options) :
|
||||
GetOptData(int argc, char **argv, const OptionData *options) :
|
||||
opt(NULL),
|
||||
numleft(argc),
|
||||
argv(argv),
|
||||
@@ -38,4 +54,59 @@ struct GetOptData {
|
||||
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 NULL 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 NULL 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 NULL 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 NULL 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, NULL)
|
||||
|
||||
/**
|
||||
* Short option with value.
|
||||
* @param shortname Short option name. Use \c '\0' if not used.
|
||||
*/
|
||||
#define GETOPT_SHORT_VALUE(shortname) GETOPT_VALUE(shortname, NULL)
|
||||
|
||||
/**
|
||||
* 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, NULL)
|
||||
|
||||
/** Option terminator. */
|
||||
#define GETOPT_END() { '\0', '\0', ODF_END, NULL}
|
||||
|
||||
|
||||
#endif /* GETOPTDATA_H */
|
||||
|
||||
Reference in New Issue
Block a user