(svn r28004) -Update from Eints:
[openttd.git] / src / misc / getoptdata.h
blob5982f01fb85e970b5995e532d32fc14c653b0ac2
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file getoptdata.h Library for parsing command-line options. */
12 #ifndef GETOPTDATA_H
13 #define GETOPTDATA_H
15 /** Flags of an option. */
16 enum OptionDataFlags {
17 ODF_NO_VALUE, ///< A plain option (no value attached to it).
18 ODF_HAS_VALUE, ///< An option with a value.
19 ODF_OPTIONAL_VALUE, ///< An option with an optional value.
20 ODF_END, ///< Terminator (data is not parsed further).
23 /** Data of an option. */
24 struct OptionData {
25 byte id; ///< Unique identification of this option data, often the same as #shortname.
26 char shortname; ///< Short option letter if available, else use \c '\0'.
27 uint16 flags; ///< Option data flags. @see OptionDataFlags
28 const char *longname; ///< Long option name including '-'/'--' prefix, use \c NULL if not available.
31 /** Data storage for parsing command line options. */
32 struct GetOptData {
33 char *opt; ///< Option value, if available (else \c NULL).
34 int numleft; ///< Number of arguments left in #argv.
35 char **argv; ///< Remaining command line arguments.
36 const OptionData *options; ///< Command line option descriptions.
37 char *cont; ///< Next call to #MyGetOpt should start here (in the middle of an argument).
39 /**
40 * Constructor of the data store.
41 * @param argc Number of command line arguments, excluding the program name.
42 * @param argv Command line arguments, excluding the program name.
43 * @param options Command line option descriptions.
45 GetOptData(int argc, char **argv, const OptionData *options) :
46 opt(NULL),
47 numleft(argc),
48 argv(argv),
49 options(options),
50 cont(NULL)
54 int GetOpt();
57 /**
58 * General macro for creating an option.
59 * @param id Identification of the option.
60 * @param shortname Short option name. Use \c '\0' if not used.
61 * @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
62 * @param flags Flags of the option.
64 #define GETOPT_GENERAL(id, shortname, longname, flags) { id, shortname, flags, longname }
66 /**
67 * Short option without value.
68 * @param shortname Short option name. Use \c '\0' if not used.
69 * @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
71 #define GETOPT_NOVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_NO_VALUE)
73 /**
74 * Short option with value.
75 * @param shortname Short option name. Use \c '\0' if not used.
76 * @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
78 #define GETOPT_VALUE(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_HAS_VALUE)
80 /**
81 * Short option with optional value.
82 * @param shortname Short option name. Use \c '\0' if not used.
83 * @param longname Long option name including leading '-' or '--'. Use \c NULL if not used.
84 * @note Options with optional values are hopelessly ambiguous, eg "-opt -value", avoid them.
86 #define GETOPT_OPTVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_OPTIONAL_VALUE)
89 /**
90 * Short option without value.
91 * @param shortname Short option name. Use \c '\0' if not used.
93 #define GETOPT_SHORT_NOVAL(shortname) GETOPT_NOVAL(shortname, NULL)
95 /**
96 * Short option with value.
97 * @param shortname Short option name. Use \c '\0' if not used.
99 #define GETOPT_SHORT_VALUE(shortname) GETOPT_VALUE(shortname, NULL)
102 * Short option with optional value.
103 * @param shortname Short option name. Use \c '\0' if not used.
104 * @note Options with optional values are hopelessly ambiguous, eg "-opt -value", avoid them.
106 #define GETOPT_SHORT_OPTVAL(shortname) GETOPT_OPTVAL(shortname, NULL)
108 /** Option terminator. */
109 #define GETOPT_END() { '\0', '\0', ODF_END, NULL}
112 #endif /* GETOPTDATA_H */