-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmdline.cpp
204 lines (174 loc) · 5.04 KB
/
cmdline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// vim:ts=4:sw=4
//
// File autogenerated by gengetopt version 2.4
// generated with the following command:
// gengetopt -l -imdate++.ggo
//
// The developers of gengetopt consider the fixed text that goes in all
// gengetopt output files to be in the public domain:
// we make no copyright claims on it.
//
// cmdline.cc - Copyright(c) under GPL 2003-2013 Sean Dwyer <ewe2@users.sourceforge.net>
// hacked around a bit from the original generated cmdline.c
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
// If we use autoconf
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "cmdline.h"
#define STATIC_HELP
#include "mdate.h"
static char rcsid[] __attribute__ ((unused)) = "@(#) $Id$";
using namespace std;
static struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "version", 0, NULL, 'v' },
{ "lang", 1, NULL, 'L' },
{ "correlation", 1, NULL, 'c' },
{ "dmy", 1, NULL, 'd' },
{ "julian", 1, NULL, 'j' },
{ "longcount", 1, NULL, 'l' },
{ NULL, 0, NULL, 0 }
};
void
cmdline_parser_print_version (void)
{
cout << PACKAGE << " " << VERSION << endl;
cout << "Copyright(c) 1998-2013 Sean Dwyer <ewe2@users.sourceforge.net>" << endl;
}
void
cmdline_parser_print_help (void)
{
cmdline_parser_print_version ();
std::cout << helptext;
}
int
cmdline_parser (int argc, char *const *argv, struct gengetopt_args_info *args_info)
{
int c; // Character of the parsed option.
int missing_required_options = 0; // hrmm..
int n_args;
args_info->help_given = 0 ;
args_info->version_given = 0 ;
args_info->correlation_given = 0 ;
args_info->dmy_given = 0 ;
args_info->julian_given = 0 ;
args_info->longcount_given = 0 ;
args_info->format_given = 0;
#define clear_args() { \
args_info->dmy_arg1 = 0; \
args_info->dmy_arg2 = 0;\
args_info->dmy_arg3 = 0;\
args_info->longcount_arg1 = 0;\
args_info->longcount_arg2 = 0;\
args_info->longcount_arg3 = 0;\
args_info->longcount_arg4 = 0;\
args_info->longcount_arg5 = 0;\
args_info->format_arg = 0;\
}
clear_args();
optarg = 0;
optind = 1;
opterr = 1;
optopt = '?';
// FIXME: why does mingw32 cross-compiler die here? is there a upstream patch
// that fixes it??
while ((c = getopt_long (argc, argv, "hVvpgL:c:d:j:l:", long_options, NULL)) != -1)
switch (c)
{
case 0:
break;
case 'h': // Print help and exit.
clear_args ();
cmdline_parser_print_help ();
::exit (0);
case 'V': // Print version and exit.
case 'v':
clear_args ();
cmdline_parser_print_version ();
::exit (0);
case 'L': // language selection.
set_lang(optarg);
break;
case 'c': // optional correlation (default=no).
if (args_info->correlation_given)
{
std::cerr << MSG_CERR;
clear_args ();
::exit (1);
}
args_info->correlation_given = 1;
args_info->correlation_arg = strtod (optarg, NULL);
break;
case 'd': // day month year (Gregorian).
if (args_info->dmy_given)
{
std::cerr << MSG_DERR;
clear_args ();
::exit (1);
}
args_info->dmy_given = 1;
args_info->dmy_arg1 = atoi(optarg);
args_info->dmy_arg2 = atoi(optarg + 2);
args_info->dmy_arg3 = atoi(optarg + 6);
break;
case 'j': // julian day number.
if (args_info->julian_given)
{
std::cerr << MSG_JERR;
clear_args ();
::exit (1);
}
args_info->julian_given = 1;
args_info->julian_arg = strtod (optarg, NULL);
break;
case 'l': // long count.
if (args_info->longcount_given)
{
std::cerr << MSG_LERR;
clear_args ();
::exit (1);
}
args_info->longcount_given = 1;
args_info->longcount_arg1 = atoi(optarg);
args_info->longcount_arg2 = atoi(optarg + 2);
args_info->longcount_arg3 = atoi(optarg + 6);
args_info->longcount_arg4 = atoi(optarg + 9);
args_info->longcount_arg5 = atoi(optarg + 12);
break;
case '?': // Invalid option.
// `getopt_long' already printed an error message.
::exit (1);
default: // bug: option not considered.
cerr << MSG_OPTERR << c << endl;
abort ();
} // switch
// while
// now deal with formatting
n_args = argc - optind;
if (n_args > 1) {
std::cerr << MSG_NONOPTERR;
::exit(1);
}
if ((n_args == 1) && argv[optind][0] == '+') {
if (args_info->format_given) {
clear_args();
std::cerr << MSG_NONOPTERR;
::exit(1);
}
args_info->format_given = 1;
args_info->format_arg = (n_args == 1 ? argv[optind] + 1: NULL);
}
if ((n_args == 1) && argv[optind][0] != '+') {
clear_args();
std::cerr << MSG_OPTERR << c << "\n";
::exit(1);
}
if ( missing_required_options )
::exit(0);
return 0;
}