OpenTTD Source: src/misc/getoptdata.h Source File

OpenTTD Source 12.2

getoptdata.h
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #ifndef GETOPTDATA_H
11 #define GETOPTDATA_H
12
14 enum OptionDataFlags {
15 ODF_NO_VALUE,
16 ODF_HAS_VALUE,
17 ODF_OPTIONAL_VALUE,
18 ODF_END,
19 };
20
22 struct OptionData {
23 byte id;
24 char shortname;
25 uint16 flags;
26 const char *longname;
27 };
28
30 struct GetOptData {
31 char *opt;
32 int numleft;
33 char **argv;
34 const OptionData *options;
35 char *cont;
36
43 GetOptData(int argc, char **argv, const OptionData *options) :
44 opt(nullptr),
45 numleft(argc),
46 argv(argv),
47 options(options),
48 cont(nullptr)
49 {
50 }
51
52 int GetOpt();
53 };
54
62 #define GETOPT_GENERAL(id, shortname, longname, flags) { id, shortname, flags, longname }
63
69 #define GETOPT_NOVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_NO_VALUE)
70
76 #define GETOPT_VALUE(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_HAS_VALUE)
77
84 #define GETOPT_OPTVAL(shortname, longname) GETOPT_GENERAL(shortname, shortname, longname, ODF_OPTIONAL_VALUE)
85
86
91 #define GETOPT_SHORT_NOVAL(shortname) GETOPT_NOVAL(shortname, nullptr)
92
97 #define GETOPT_SHORT_VALUE(shortname) GETOPT_VALUE(shortname, nullptr)
98
104 #define GETOPT_SHORT_OPTVAL(shortname) GETOPT_OPTVAL(shortname, nullptr)
105
107 #define GETOPT_END() { '\0', '\0', ODF_END, nullptr}
108
109
110 #endif /* GETOPTDATA_H */
ODF_END
@ ODF_END
Terminator (data is not parsed further).
Definition: getoptdata.h:18
ODF_OPTIONAL_VALUE
@ ODF_OPTIONAL_VALUE
An option with an optional value.
Definition: getoptdata.h:17
OptionData::longname
const char * longname
Long option name including '-'/'–' prefix, use nullptr if not available.
Definition: getoptdata.h:26
GetOptData::options
const OptionData * options
Command line option descriptions.
Definition: getoptdata.h:34
GetOptData::argv
char ** argv
Remaining command line arguments.
Definition: getoptdata.h:33
OptionData::flags
uint16 flags
Option data flags.
Definition: getoptdata.h:25
GetOptData::opt
char * opt
Option value, if available (else nullptr).
Definition: getoptdata.h:31
OptionData
Data of an option.
Definition: getoptdata.h:22
GetOptData
Data storage for parsing command line options.
Definition: getoptdata.h:30
GetOptData::GetOptData
GetOptData(int argc, char **argv, const OptionData *options)
Constructor of the data store.
Definition: getoptdata.h:43
OptionData::id
byte id
Unique identification of this option data, often the same as shortname.
Definition: getoptdata.h:23
OptionData::shortname
char shortname
Short option letter if available, else use '\0'.
Definition: getoptdata.h:24
GetOptData::cont
char * cont
Next call to GetOpt should start here (in the middle of an argument).
Definition: getoptdata.h:35
OptionDataFlags
OptionDataFlags
Flags of an option.
Definition: getoptdata.h:14
GetOptData::GetOpt
int GetOpt()
Find the next option.
Definition: getoptdata.cpp:22
ODF_HAS_VALUE
@ ODF_HAS_VALUE
An option with a value.
Definition: getoptdata.h:16
ODF_NO_VALUE
@ ODF_NO_VALUE
A plain option (no value attached to it).
Definition: getoptdata.h:15
GetOptData::numleft
int numleft
Number of arguments left in argv.
Definition: getoptdata.h:32