summaryrefslogtreecommitdiff
path: root/src/misc/getoptdata.h
diff options
context:
space:
mode:
authoralberth <alberth@openttd.org>2011-02-18 20:28:25 +0000
committeralberth <alberth@openttd.org>2011-02-18 20:28:25 +0000
commit8c2fc4515355b4bf89d8427694361851b54708da (patch)
tree526e911fddcd557a241d9bd5fa064b3278fb08cf /src/misc/getoptdata.h
parentb0b1c0dea9f1f27b969741b3715c1fe9a76349d7 (diff)
downloadopenttd-8c2fc4515355b4bf89d8427694361851b54708da.tar.xz
(svn r22098) -Codechange: Have an array of option data, use it in the option parsing routine. Adapt openttd option processing too.
Diffstat (limited to 'src/misc/getoptdata.h')
-rw-r--r--src/misc/getoptdata.h83
1 files changed, 77 insertions, 6 deletions
diff --git a/src/misc/getoptdata.h b/src/misc/getoptdata.h
index c3a204494..5982f01fb 100644
--- a/src/misc/getoptdata.h
+++ b/src/misc/getoptdata.h
@@ -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 */