1 /* $Id: getoptdata.h 23245 2011-11-17 21:18:24Z rubidium $ */
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/>.
10 /** @file getoptdata.h Library for parsing command-line options. */
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. */
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 nullptr if not available.
31 /** Data storage for parsing command line options. */
33 char *opt
; ///< Option value, if available (else \c nullptr).
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).
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
) :
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 nullptr if not used.
62 * @param flags Flags of the option.
64 #define GETOPT_GENERAL(id, shortname, longname, flags) { id, shortname, flags, longname }
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 nullptr if not used.
71 #define GETOPT_NOVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_NO_VALUE)
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 nullptr if not used.
78 #define GETOPT_VALUE(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_HAS_VALUE)
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 nullptr 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)
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, nullptr)
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, nullptr)
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, nullptr)
108 /** Option terminator. */
109 #define GETOPT_END() { '\0', '\0', ODF_END, nullptr}
112 #endif /* GETOPTDATA_H */