-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmyprogram.cpp
196 lines (180 loc) · 5.16 KB
/
myprogram.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
/** \file myprogram.cpp
*
* `myprogram' does this and that.
* ----------
* choose between:
* Not copyrighted -- provided to the public domain
* Author: Timothée Flutre
* or:
* Copyright (C) 2011-2014 Timothée Flutre
*
* This program 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, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Timothée Flutre
* ----------
*
* Versioning: https://github.com/timflutre/...
*
* Compile with: g++ -Wall -g utils_io.cpp myprogram.cpp -lgsl -lgslcblas -lz -o myprogram
* "-lgsl -lgslcblas" are just provided as example
*/
#include <cmath>
#include <ctime>
#include <cstring>
#include <getopt.h>
#include <libgen.h>
#include <iostream>
#include <string>
using namespace std;
#include "utils_io.hpp"
using namespace utils;
#ifndef VERSION
#define VERSION "1.0.0" // http://semver.org/
#endif
/** \brief Display the help on stdout.
* \note The format complies with help2man (http://www.gnu.org/s/help2man)
*/
void help(char ** argv)
{
cout << "`" << argv[0] << "'"
<< " does this and that." << endl
<< endl
<< "Usage: " << argv[0] << " [OPTIONS] ..." << endl
<< endl
<< "Options:" << endl
<< " -h, --help\tdisplay the help and exit" << endl
<< " -V, --version\toutput version information and exit" << endl
<< " -v, --verbose\tverbosity level (0/default=1/2/3)" << endl
<< " -i, --input\tpath to the input file" << endl
<< endl
<< "Examples:" << endl
<< " " << argv[0] << " -i <input>" << endl
<< endl
<< "Report bugs to <>." << endl
;
}
/** \brief Display version and license information on stdout.
*/
void version(char ** argv)
{
cout << argv[0] << " " << VERSION << endl
<< endl
<< "Copyright (C) 2011-2014 Timothée Flutre." << endl
<< "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>" << endl
<< "This is free software; see the source for copying conditions. There is NO" << endl
<< "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." << endl
<< endl
<< "Written by Timothée Flutre." << endl
;
// or choose "Not copyrighted -- provided to the public domain"
}
/** \brief Parse the command-line arguments and check the values of the
* compulsory ones.
*/
void
parseCmdLine(
int argc,
char ** argv,
string & input,
int & verbose)
{
int c = 0;
while(true)
{
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"verbose", required_argument, 0, 'v'},
{"input", required_argument, 0, 0},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "hVv:i:",
long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 0:
if(long_options[option_index].flag != 0)
break;
if(strcmp(long_options[option_index].name, "input") == 0)
{
input = optarg;
break;
}
case 'h':
help (argv);
exit(0);
case 'V':
version(argv);
exit(0);
case 'v':
verbose = atoi(optarg);
break;
case 'i':
input = optarg;
break;
case '?':
printf("\n"); help(argv);
abort();
default:
printf("\n"); help(argv);
abort();
}
}
if(input.empty()){
cerr << "cmd-line: " << getCmdLine(argc, argv) << endl << endl
<< "ERROR: missing compulsory option --input" << endl << endl;
help(argv);
exit(1);
}
if(! doesFileExist(input)){
cerr << "cmd-line: " << getCmdLine(argc, argv) << endl << endl
<< "ERROR: can't find file " << input << endl << endl;
help(argv);
exit(1);
}
}
void run(const string & input, const int & verbose)
{
// specific code ...
}
int main(int argc, char ** argv)
{
string input;
int verbose = 1;
parseCmdLine(argc, argv, input, verbose);
time_t startRawTime, endRawTime;
if(verbose > 0){
time(&startRawTime);
cout << "START " << basename(argv[0])
<< " " << getDateTime(startRawTime) << endl
<< "version " << VERSION << " compiled " << __DATE__
<< " " << __TIME__ << endl
<< "cmd-line: " << getCmdLine(argc, argv) << endl
<< "cwd: " << getCurrentDirectory() << endl;
cout << flush;
}
run(input, verbose);
if(verbose > 0){
time(&endRawTime);
cout << "END " << basename(argv[0])
<< " " << getDateTime(endRawTime) << endl
<< "elapsed -> " << getElapsedTime(startRawTime, endRawTime) << endl
<< "max.mem -> " << getMaxMemUsedByProcess2Str() << endl;
}
return EXIT_SUCCESS;
}