-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.cpp
223 lines (176 loc) · 4.92 KB
/
cli.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// -----------------------------------------------------------------------------
// cli.cpp cli.cpp
// -----------------------------------------------------------------------------
/**
* @file
* @brief This file holds the implementation of the @ref cli class.
* @author Col. Walter E. Kurtz
* @version 2019-06-23
* @copyright GNU General Public License - Version 3.0
*/
// -----------------------------------------------------------------------------
// Includes Includes
// -----------------------------------------------------------------------------
#include <unistd.h>
#include <sstream>
#include <iostream>
#include "message.h"
#include "cli.h"
// -----------------------------------------------------------------------------
// Used namespaces Used namespaces
// -----------------------------------------------------------------------------
using namespace std;
// -----------------------------------------------------------------------------
// Construction Construction
// -----------------------------------------------------------------------------
// ---
// cli
// ---
/*
*
*/
cli::cli()
{
// run default operation
operation = DEFAULT;
// get filename from command-line
source = PARAM;
}
// -----------------------------------------------------------------------------
// Handling Handling
// -----------------------------------------------------------------------------
// ----
// help
// ----
/*
*
*/
void cli::help(const std::string& xname) const
{
// filename without leading directories
string nodir;
for(string::size_type i = 0; i < xname.size(); i++)
{
// check character
if (xname[i] == '/')
{
// reset
nodir = "";
}
else
{
// append character
nodir += xname[i];
}
}
// show syntax
cout << endl;
cout << nodir << " [options] [<filename>]" << endl;
cout << endl;
cout << " -h show help and exit" << endl;
cout << " -v show version and exit" << endl;
cout << " -d show database lines" << endl;
cout << " -k show the list of keys" << endl;
cout << " -L show the list of commands" << endl;
cout << " -o show a brief overview" << endl;
cout << " -O show a verbose overview" << endl;
cout << " -s create auxiliary bash scripts and exit" << endl;
cout << " -z read NUL terminated filenames from stdin" << endl;
cout << endl;
}
// -------
// version
// -------
/*
*
*/
void cli::version() const
{
cout << endl;
cout << "version 2019-06-23.1" << endl;
cout << endl;
}
// -----
// parse
// -----
/*
*
*/
bool cli::parse(int argc, char** argv)
{
// don't print error messages
opterr = 0;
// ASCII code of the detected option
int optchar;
// parse all given options
while ((optchar = getopt(argc, argv, ":hvdkLoOsz")) != -1)
{
// use this object to convert arguments
stringstream argstream((optarg == 0) ? "" : optarg);
// the ID of the script to generate
string scriptid;
// analyze (short) options
switch (optchar)
{
case 'h': operation = SHOW_HELP;
// stop parsing
return true;
case 'v': operation = SHOW_VERSION;
// stop parsing
return true;
case 'd': operation = SHOW_DBASE_LINES;
// next option
break;
case 'k': operation = SHOW_KEYS;
// stop parsing
return true;
case 'L': operation = SHOW_COMMANDS;
// stop parsing
return true;
case 'o': operation = SHOW_OVERVIEW_BRIEF;
// next option
break;
case 'O': operation = SHOW_OVERVIEW_VERBOSE;
// next option
break;
case 's': operation = CREATE_SCRIPTS;
// stop parsing
return true;
case 'z': source = STDIN;
// next option
break;
case ':': msg::err("missing argument");
// signalize trouble
return false;
case '?': msg::err("unknown option");
// signalize trouble
return false;
}
}
// check source
if (source == PARAM)
{
// get number of positional arguments
unsigned pcount = (argc - optind);
// check number of positional arguments
if (pcount != 1)
{
if (pcount < 1)
{
// notify user
msg::err("filename missing");
}
else
{
// notify user
msg::err("too many filenames given");
}
// signalize trouble
return false;
}
// get filename of the file to parse
filename = argv[optind];
}
// signalize success
return true;
}