Remove compiler warnings
[vng.git] / VngCommandLine.cpp
blob0b84779ca0f2105020c85c14203160e71cb6aa07
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
4 * Copyright (C) 2002-2005 David Roundy
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "VngCommandLine.h"
20 #include "CommandLineParser.h"
21 #include "Logger.h"
23 // commands
24 #include "commands/Add.h"
25 #include "commands/Branches.h"
26 #include "commands/Changes.h"
27 #include "commands/Dist.h"
28 #include "commands/Initialize.h"
29 #include "commands/Move.h"
30 #include "commands/Push.h"
31 #include "commands/Record.h"
32 #include "commands/Remove.h"
33 #include "commands/Revert.h"
34 #include "commands/UnRecord.h"
35 #include "commands/UnRevert.h"
36 #include "commands/WhatsNew.h"
38 #include <QTextStream>
40 #define VERSION "vng version 0.26 (>2008-05-16)"
42 VngCommandLine::VngCommandLine(int argc, char **argv)
43 : QCoreApplication(argc, argv)
45 if (argc > 1)
46 m_command = QString(argv[1]);
47 if (m_command == "help" && argc > 2) { // 'help foo' -> 'foo -h'
48 argv[1] = argv[2];
49 argv[2] = new char[3];// this is a memory leak, do we care?
50 argv[2][0] = '-';
51 argv[2][1] = 'h';
52 argv[2][2] = 0;
53 m_command = QString(argv[1]);
56 CommandLineParser::init(argc, argv);
59 VngCommandLine::~VngCommandLine()
61 delete CommandLineParser::instance();
64 int VngCommandLine::run()
66 if (m_command == "--extended-help") {
67 printExtendedHelp();
68 return 0;
70 if (m_command == "--version" || m_command == "-v"
71 #if defined(Q_OS_WIN)
72 || m_command == "/V"
73 #endif
74 ) {
75 QTextStream out(stdout);
76 out << VERSION << endl;
77 return 0;
79 if (m_command.isEmpty()) {
80 printHelp();
81 return 0;
84 if (m_command == "--commands") { // for commandline completion.
85 Configuration config("");
86 QTextStream out(stdout);
87 out << "help" << endl;
88 out << "initialize" << endl;
89 out << "--help" << endl;
90 out << "--version" << endl;
91 out << "--extended--help" << endl;
92 if (QDir(config.repository().absolutePath() + "/.git").exists()) {
93 out << "add" << endl;
94 out << "branches" << endl;
95 out << "changes" << endl;
96 out << "dist" << endl;
97 out << "log" << endl;
98 out << "move" << endl;
99 out << "mv" << endl;
100 out << "push" << endl;
101 out << "record" << endl;
102 out << "remove" << endl;
103 out << "revert" << endl;
104 out << "rm" << endl;
105 out << "unrecord" << endl;
106 out << "unrevert" << endl;
107 out << "whatsnew" << endl;
109 return 0;
112 // autoComplete command
113 QStringList commands;
114 // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort
115 commands << "add";
116 commands << "branches";
117 commands << "changes";
118 commands << "dist";
119 commands << "help";
120 commands << "initialize";
121 commands << "log";
122 commands << "move";
123 commands << "mv";
124 commands << "push";
125 commands << "record";
126 commands << "remove";
127 commands << "revert";
128 commands << "rm";
129 commands << "unrecord";
130 commands << "unrevert";
131 commands << "whatsnew";
133 QString command;
134 foreach(QString s, commands) {
135 if (m_command == s) { // perfect match
136 command = m_command;
137 break;
139 if (! command.isEmpty()) {
140 if (s.startsWith(m_command)) {
141 QTextStream out(stdout);
142 out << "vng failed: " << "Ambiguous command..." << endl;
143 return -1;
145 else
146 break; // found best match.
148 if (s.startsWith(m_command)) {
149 command = s;
150 continue;
153 if (command.isEmpty()) {
154 if (! m_command.isEmpty()) {
155 QTextStream out(stdout);
156 out << "vng failed: " << "Invalid command `" << m_command << "'\n\n";
158 printHelp();
159 return 0;
161 m_command = command;
163 AbstractCommand *ac = 0;
164 if (m_command == "whatsnew")
165 ac = new WhatsNew();
166 else if (m_command == "add")
167 ac = new Add();
168 else if (m_command == "branches")
169 ac = new Branches();
170 else if (m_command == "changes" || m_command == "log")
171 ac = new Changes();
172 else if (m_command == "initialize")
173 ac = new Initialize();
174 else if (m_command == "move" || m_command == "mv")
175 ac = new Move();
176 else if (m_command == "push")
177 ac = new Push();
178 else if (m_command == "remove" || m_command == "rm")
179 ac = new Remove();
180 else if (m_command == "revert")
181 ac = new Revert();
182 else if (m_command == "unrecord")
183 ac = new UnRecord();
184 else if (m_command == "unrevert")
185 ac = new UnRevert();
186 else if (m_command == "dist")
187 ac = new Dist();
188 else if (command == "help") {
189 printHelp();
190 return 0;
192 else if (m_command == "record")
193 ac = new Record();
195 Q_ASSERT(ac);
196 return ac->start();
199 void VngCommandLine::printHelp()
201 QTextStream out(stdout);
202 out << "Usage: vng COMMAND ...\n\n" << VERSION << endl;
203 out << "Commands:" << endl;
204 out << " help Display help for vng or a single commands." << endl;
205 out << "Changing and querying the working copy:" << endl;
206 Configuration config("");
207 out << " add Add one or more new files or directories." << endl;
208 out << " remove Remove one or more files or directories from the repository." << endl;
209 out << " mv Move/rename one or more files or directories." << endl;
210 out << " revert Revert to the recorded version (safe the first time only)." << endl;
211 out << " unrevert Undo the last revert (may fail if changes after the revert)." << endl;
212 out << " whatsnew Display unrecorded changes in the working copy." << endl;
213 out << "Copying changes between the working copy and the repository:" << endl;
214 out << " record Save changes in the working copy to the repository as a patch." << endl;
215 out << " unrecord Remove recorded patches without changing the working copy." << endl;
216 out << "Copying patches between repositories with working copy update:" << endl;
217 out << " push Copy and apply patches from this repository to another one." << endl;
218 out << "Querying the repository:" << endl;
219 // out << " diff Create a diff between two versions of the repository." << endl;
220 // out << " branches " << endl;
221 out << " dist Create a distribution tarball." << endl;
222 out << " changes Gives a changelog-style summary of the repository history." << endl;
223 out << "Administrating repositories:" << endl;
224 out << " initialize Initialize a new source tree as a vng repository." << endl;
225 out << endl;
226 out << "Use 'vng --extended-help' for more detailed help." << endl;
227 out << "Use 'vng COMMAND --help' for help on a single command." << endl;
228 out << "Use 'vng --version' to see the vng version number." << endl;
231 void VngCommandLine::printExtendedHelp()
233 QTextStream out(stdout);
234 out << VERSION << endl;
235 out << "Usage: vng COMMAND ...\n\n";
236 out << "Extended Help:\n\n";
237 out << "A vng repository consists of:\n\n";
238 out << " - a set of PATCHES\n";
239 out << " - a WORKING directory\n\n";
240 out << "Here is a description of which of these components is altered by each\n";
241 out << "command, and how it is used or altered:\n\n";
242 out << " whatsnew Show the differences between WORKING and the \"recorded\"\n";
243 out << " version, that is, the result of applying all PATCHES in the\n";
244 out << " repository. This difference, we will call LOCAL CHANGES.\n\n";
245 out << " record Add a patch to PATCHES representing the LOCAL CHANGES.\n\n";
246 out << " unrecord Delete a patch from PATCHES, but *do not* alter WORKING.\n\n";
247 out << " revert Remove LOCAL CHANGES. Note that this command is interactive,\n";
248 out << " so you can use it to revert only some of these changes.\n\n";
249 out << " unrevert Undo the last revert operation.\n\n";
250 // out << " unpull Delete a patch from PATCHES and unapply it from WORKING.\n";
251 // out << " Note that this command works for any patch, not just one that\n";
252 // out << " was previously \"pull\"ed. If there are no LOCAL CHANGES,\n";
253 // out << " this command is equivalent to \"vng unrecord; vng revert\"\n\n";
254 // out << " rollback Create the inverse of a particular patch and add it to PATCHES,\n";
255 // out << " but DO NOT apply it to WORKING. Note that this command is the\n";
256 // out << " only way to wind up with a patch in PATCHES which has not been\n";
257 // out << " applied to WORKING.\n\n";
261 // a C main function
262 int main(int argc, char **argv)
264 VngCommandLine vng(argc, argv);
265 return vng.run();