-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.cpp
164 lines (136 loc) · 3.49 KB
/
arguments.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
/****************************************************************************
** Copyright (C) 2009-2012 Arzel Jérôme <myst6re@gmail.com>
**
** 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/>.
****************************************************************************/
#include "arguments.h"
#include <QCoreApplication>
#include <QDir>
#include <QTextStream>
Arguments::Arguments() :
_extract(false), _help(false), _quiet(false)
{
parse();
}
const QStringList &Arguments::paths() const
{
return _paths;
}
const QDir &Arguments::directory() const
{
return _directory;
}
bool Arguments::extract() const
{
return _extract;
}
bool Arguments::help() const
{
return _help;
}
bool Arguments::quiet() const
{
return _quiet;
}
void Arguments::parse()
{
QStringList args = qApp->arguments();
args.removeFirst(); // Application path
if (!args.isEmpty()) {
const QString &arg = args.takeFirst();
if (arg == "extract") {
_extract = true;
} else if (arg == "pack") {
_extract = false;
} else {
_help = true;
}
bool stopOptions = false;
while (!args.isEmpty()) {
const QString &arg = args.takeFirst();
if (!stopOptions) {
if (arg == "-h" || arg == "--help") {
_help = true;
} else if (arg == "-q" || arg == "--quiet") {
_quiet = true;
} else if (arg == "--") {
stopOptions = true;
} else {
_paths << arg;
}
} else {
_paths << arg;
}
}
wilcardParse();
} else {
_help = true;
}
}
QMap<QString, QString> Arguments::commands() const
{
QMap<QString, QString> options;
options["-h --help"] = "Show this help and quit.";
options["-q --quiet"] = "Suppress all outputs";
return options;
}
void Arguments::showHelp(int exitCode)
{
QTextStream out(stdout, QIODevice::WriteOnly);
out << "zzz extract ZZZ_FILE [ZZZ_FILE2...] OUTPUT_DIR\n";
out << "zzz pack ZZZ_FILE SOURCE_DIR\n";
out << "Options\n";
QMapIterator<QString, QString> it(commands());
while (it.hasNext()) {
it.next();
out << "\t" << qPrintable(it.key()) << "\n";
out << "\t" << "\t" << qPrintable(it.value()) << "\n";
}
out.flush();
::exit(exitCode);
}
QStringList Arguments::searchFiles(const QString &path)
{
int index = path.lastIndexOf('/');
QString dirname, filename;
if (index > 0) {
dirname = path.left(index);
filename = path.mid(index + 1);
} else {
filename = path;
}
QDir dir(dirname);
QStringList entryList = dir.entryList(QStringList(filename), QDir::Files);
int i=0;
foreach (const QString &entry, entryList) {
entryList.replace(i++, dir.filePath(entry));
}
return entryList;
}
void Arguments::wilcardParse()
{
QStringList paths;
foreach (const QString &path, _paths) {
if (path.contains('*') || path.contains('?')) {
paths << searchFiles(QDir::fromNativeSeparators(path));
} else {
paths << QDir::fromNativeSeparators(path);
}
}
if (!paths.isEmpty()) {
// Output directory
_directory = paths.takeLast();
_paths = paths;
}
}