We don't want vng executable to be placed inside Vng.app/Contents/MacOS on Mac OS X.
[vng.git] / VngCommandLine.cpp
blob8db58a18c2114a596ac68e9513e8511c136520e7
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 "Initialize.h"
26 #include "Push.h"
27 #include "Revert.h"
28 #include "Record.h"
29 #include "UnRevert.h"
30 #include "UnRecord.h"
31 #include "WhatsNew.h"
33 #include <QTextStream>
35 #define VERSION "vng version 0.22 (>2008-03-28)"
37 VngCommandLine::VngCommandLine(int argc, char **argv)
38 : QCoreApplication(argc, argv)
40 CommandLineParser::init(argc, argv);
41 if (argc > 1)
42 m_command = QString(argv[1]);
45 VngCommandLine::~VngCommandLine()
47 delete CommandLineParser::instance();
50 int VngCommandLine::run()
52 if (m_command == "--extended-help") {
53 printExtendedHelp();
54 return 0;
56 if (m_command == "--version" || m_command == "-v"
57 #if defined(Q_OS_WIN)
58 || m_command == "/V"
59 #endif
60 ) {
61 QTextStream out(stdout);
62 out << VERSION << endl;
63 return 0;
65 if (m_command.isEmpty()) {
66 printHelp();
67 return 0;
70 // autoComplete command
71 QStringList commands;
72 // keep this list sorted in the sourcecode. In vim just select the lines and do a :'<,'>!sort
73 commands << "add";
74 commands << "help";
75 commands << "initialize";
76 commands << "push";
77 commands << "record";
78 commands << "revert";
79 commands << "unrecord";
80 commands << "unrevert";
81 commands << "whatsnew";
83 if (m_command == "--commands") { // for commandline completion.
84 Configuration config("");
85 QTextStream out(stdout);
86 out << "help" << endl;
87 out << "--help" << endl;
88 out << "--version" << endl;
89 out << "--extended--help" << endl;
90 if (QDir(config.repository().absolutePath() + "/.git").exists()) {
91 out << "add" << endl;
92 out << "push" << endl;
93 out << "record" << endl;
94 out << "revert" << endl;
95 out << "unrecord" << endl;
96 out << "unrevert" << endl;
97 out << "whatsnew" << endl;
99 return 0;
103 QString command;
104 foreach(QString s, commands) {
105 if (m_command == s) { // perfect match
106 command = m_command;
107 break;
109 if (! command.isEmpty()) {
110 if (s.startsWith(m_command)) {
111 QTextStream out(stdout);
112 out << "vng failed: " << "Ambiguous command..." << endl;
113 return -1;
115 else
116 break; // found best match.
118 if (s.startsWith(m_command)) {
119 command = s;
120 continue;
123 if (command.isEmpty()) {
124 if (! m_command.isEmpty()) {
125 QTextStream out(stdout);
126 out << "vng failed: " << "Invalid command `" << m_command << "'\n\n";
128 printHelp();
129 return 0;
131 m_command = command;
133 AbstractCommand *ac = 0;
134 if (m_command == "whatsnew")
135 ac = new WhatsNew();
136 else if (m_command == "add")
137 ac = new Add();
138 else if (m_command == "initialize")
139 ac = new Initialize();
140 else if (m_command == "push")
141 ac = new Push();
142 else if (m_command == "revert")
143 ac = new Revert();
144 else if (m_command == "unrecord")
145 ac = new UnRecord();
146 else if (m_command == "unrevert")
147 ac = new UnRevert();
148 else if (command == "help") {
149 printHelp();
150 return 0;
152 else if (m_command == "record")
153 ac = new Record();
155 Q_ASSERT(ac);
156 return ac->start();
159 void VngCommandLine::printHelp()
161 QTextStream out(stdout);
162 out << "Usage: vng COMMAND ...\n\n" << VERSION << endl;
163 out << "Commands:" << endl;
164 out << " help Display help for vng or a single commands." << endl;
165 out << "Changing and querying the working copy:" << endl;
166 out << " add Add one or more new files or directories." << endl;
167 out << " revert Revert to the recorded version (safe the first time only)." << endl;
168 out << " unrevert Undo the last revert (may fail if changes after the revert)." << endl;
169 out << " whatsnew Display unrecorded changes in the working copy." << endl;
170 out << "Copying changes between the working copy and the repository:" << endl;
171 out << " record Save changes in the working copy to the repository as a patch." << endl;
172 out << " unrecord Remove recorded patches without changing the working copy." << endl;
173 out << "Copying patches between repositories with working copy update:" << endl;
174 out << " push Copy and apply patches from this repository to another one." << endl;
175 out << "Administrating repositories:" << endl;
176 out << " initialize Initialize a new source tree as a vng repository." << endl;
177 out << endl;
178 out << "Use 'vng --extended-help' for more detailed help." << endl;
179 out << "Use 'vng COMMAND --help' for help on a single command." << endl;
180 out << "Use 'vng --version' to see the vng version number." << endl;
183 void VngCommandLine::printExtendedHelp()
185 QTextStream out(stdout);
186 out << VERSION << endl;
187 out << "Usage: vng COMMAND ...\n\n";
188 out << "Extended Help:\n\n";
189 out << "A vng repository consists of:\n\n";
190 out << " - a set of PATCHES\n";
191 out << " - a WORKING directory\n\n";
192 out << "Here is a description of which of these components is altered by each\n";
193 out << "command, and how it is used or altered:\n\n";
194 out << " whatsnew Show the differences between WORKING and the \"recorded\"\n";
195 out << " version, that is, the result of applying all PATCHES in the\n";
196 out << " repository. This difference, we will call LOCAL CHANGES.\n\n";
197 out << " record Add a patch to PATCHES representing the LOCAL CHANGES.\n\n";
198 out << " unrecord Delete a patch from PATCHES, but *do not* alter WORKING.\n\n";
199 out << " revert Remove LOCAL CHANGES. Note that this command is interactive,\n";
200 out << " so you can use it to revert only some of these changes.\n\n";
201 out << " unrevert Undo the last revert operation.\n\n";
202 // out << " unpull Delete a patch from PATCHES and unapply it from WORKING.\n";
203 // out << " Note that this command works for any patch, not just one that\n";
204 // out << " was previously \"pull\"ed. If there are no LOCAL CHANGES,\n";
205 // out << " this command is equivalent to \"vng unrecord; vng revert\"\n\n";
206 // out << " rollback Create the inverse of a particular patch and add it to PATCHES,\n";
207 // out << " but DO NOT apply it to WORKING. Note that this command is the\n";
208 // out << " only way to wind up with a patch in PATCHES which has not been\n";
209 // out << " applied to WORKING.\n\n";
213 // a C main function
214 int main(int argc, char **argv)
216 VngCommandLine vng(argc, argv);
217 return vng.run();