API docs fixes
[vng.git] / VngCommandLine.cpp
blob09ebd2e5f16eac9f6c0425627572d848cbaf6e8a
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 "Add.h"
25 #include "Changes.h"
26 #include "Initialize.h"
27 #include "Push.h"
28 #include "Revert.h"
29 #include "Record.h"
30 #include "UnRevert.h"
31 #include "UnRecord.h"
32 #include "WhatsNew.h"
34 #include <QTextStream>
36 #define VERSION "vng version 0.23 (>2008-04-06)"
38 VngCommandLine::VngCommandLine(int argc, char **argv)
39 : QCoreApplication(argc, argv)
41 CommandLineParser::init(argc, argv);
42 if (argc > 1)
43 m_command = QString(argv[1]);
46 VngCommandLine::~VngCommandLine()
48 delete CommandLineParser::instance();
51 int VngCommandLine::run()
53 if (m_command == "--extended-help") {
54 printExtendedHelp();
55 return 0;
57 if (m_command == "--version" || m_command == "-v"
58 #if defined(Q_OS_WIN)
59 || m_command == "/V"
60 #endif
61 ) {
62 QTextStream out(stdout);
63 out << VERSION << endl;
64 return 0;
66 if (m_command.isEmpty()) {
67 printHelp();
68 return 0;
71 // autoComplete command
72 QStringList commands;
73 // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort
74 commands << "add";
75 commands << "changes";
76 commands << "help";
77 commands << "initialize";
78 commands << "push";
79 commands << "record";
80 commands << "revert";
81 commands << "unrecord";
82 commands << "unrevert";
83 commands << "whatsnew";
85 if (m_command == "--commands") { // for commandline completion.
86 Configuration config("");
87 QTextStream out(stdout);
88 out << "help" << 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 << "changes" << endl;
95 out << "push" << endl;
96 out << "record" << endl;
97 out << "revert" << endl;
98 out << "unrecord" << endl;
99 out << "unrevert" << endl;
100 out << "whatsnew" << endl;
102 return 0;
106 QString command;
107 foreach(QString s, commands) {
108 if (m_command == s) { // perfect match
109 command = m_command;
110 break;
112 if (! command.isEmpty()) {
113 if (s.startsWith(m_command)) {
114 QTextStream out(stdout);
115 out << "vng failed: " << "Ambiguous command..." << endl;
116 return -1;
118 else
119 break; // found best match.
121 if (s.startsWith(m_command)) {
122 command = s;
123 continue;
126 if (command.isEmpty()) {
127 if (! m_command.isEmpty()) {
128 QTextStream out(stdout);
129 out << "vng failed: " << "Invalid command `" << m_command << "'\n\n";
131 printHelp();
132 return 0;
134 m_command = command;
136 AbstractCommand *ac = 0;
137 if (m_command == "whatsnew")
138 ac = new WhatsNew();
139 else if (m_command == "add")
140 ac = new Add();
141 else if (m_command == "changes")
142 ac = new Changes();
143 else if (m_command == "initialize")
144 ac = new Initialize();
145 else if (m_command == "push")
146 ac = new Push();
147 else if (m_command == "revert")
148 ac = new Revert();
149 else if (m_command == "unrecord")
150 ac = new UnRecord();
151 else if (m_command == "unrevert")
152 ac = new UnRevert();
153 else if (command == "help") {
154 printHelp();
155 return 0;
157 else if (m_command == "record")
158 ac = new Record();
160 Q_ASSERT(ac);
161 return ac->start();
164 void VngCommandLine::printHelp()
166 QTextStream out(stdout);
167 out << "Usage: vng COMMAND ...\n\n" << VERSION << endl;
168 out << "Commands:" << endl;
169 out << " help Display help for vng or a single commands." << endl;
170 out << "Changing and querying the working copy:" << endl;
171 out << " add Add one or more new files or directories." << endl;
172 out << " revert Revert to the recorded version (safe the first time only)." << endl;
173 out << " unrevert Undo the last revert (may fail if changes after the revert)." << endl;
174 out << " whatsnew Display unrecorded changes in the working copy." << endl;
175 out << "Copying changes between the working copy and the repository:" << endl;
176 out << " record Save changes in the working copy to the repository as a patch." << endl;
177 out << " unrecord Remove recorded patches without changing the working copy." << endl;
178 out << "Copying patches between repositories with working copy update:" << endl;
179 out << " push Copy and apply patches from this repository to another one." << endl;
180 out << "Querying the repository:" << endl;
181 // out << " diff Create a diff between two versions of the repository." << endl;
182 out << " changes Gives a changelog-style summary of the repository history." << endl;
183 out << "Administrating repositories:" << endl;
184 out << " initialize Initialize a new source tree as a vng repository." << endl;
185 out << endl;
186 out << "Use 'vng --extended-help' for more detailed help." << endl;
187 out << "Use 'vng COMMAND --help' for help on a single command." << endl;
188 out << "Use 'vng --version' to see the vng version number." << endl;
191 void VngCommandLine::printExtendedHelp()
193 QTextStream out(stdout);
194 out << VERSION << endl;
195 out << "Usage: vng COMMAND ...\n\n";
196 out << "Extended Help:\n\n";
197 out << "A vng repository consists of:\n\n";
198 out << " - a set of PATCHES\n";
199 out << " - a WORKING directory\n\n";
200 out << "Here is a description of which of these components is altered by each\n";
201 out << "command, and how it is used or altered:\n\n";
202 out << " whatsnew Show the differences between WORKING and the \"recorded\"\n";
203 out << " version, that is, the result of applying all PATCHES in the\n";
204 out << " repository. This difference, we will call LOCAL CHANGES.\n\n";
205 out << " record Add a patch to PATCHES representing the LOCAL CHANGES.\n\n";
206 out << " unrecord Delete a patch from PATCHES, but *do not* alter WORKING.\n\n";
207 out << " revert Remove LOCAL CHANGES. Note that this command is interactive,\n";
208 out << " so you can use it to revert only some of these changes.\n\n";
209 out << " unrevert Undo the last revert operation.\n\n";
210 // out << " unpull Delete a patch from PATCHES and unapply it from WORKING.\n";
211 // out << " Note that this command works for any patch, not just one that\n";
212 // out << " was previously \"pull\"ed. If there are no LOCAL CHANGES,\n";
213 // out << " this command is equivalent to \"vng unrecord; vng revert\"\n\n";
214 // out << " rollback Create the inverse of a particular patch and add it to PATCHES,\n";
215 // out << " but DO NOT apply it to WORKING. Note that this command is the\n";
216 // out << " only way to wind up with a patch in PATCHES which has not been\n";
217 // out << " applied to WORKING.\n\n";
221 // a C main function
222 int main(int argc, char **argv)
224 VngCommandLine vng(argc, argv);
225 return vng.run();