1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "argumentscollector.h"
38 using namespace Utils;
40 ArgumentsCollector::ArgumentsCollector(const QStringList &args)
45 Parameters ArgumentsCollector::collect(bool &success) const
47 Parameters parameters;
49 bool authTypeGiven = false;
50 bool portGiven = false;
51 bool timeoutGiven = false;
52 bool smallFileCountGiven = false;
53 bool bigFileSizeGiven = false;
54 bool proxySettingGiven = false;
57 for (pos = 1; pos < m_arguments.count() - 1; ++pos) {
58 if (checkAndSetStringArg(pos, parameters.sshParams.host, "-h")
59 || checkAndSetStringArg(pos, parameters.sshParams.userName, "-u"))
61 if (checkAndSetIntArg(pos, port, portGiven, "-p")
62 || checkAndSetIntArg(pos, parameters.sshParams.timeout, timeoutGiven, "-t")
63 || checkAndSetIntArg(pos, parameters.smallFileCount, smallFileCountGiven, "-c")
64 || checkAndSetIntArg(pos, parameters.bigFileSize, bigFileSizeGiven, "-s"))
66 if (checkAndSetStringArg(pos, parameters.sshParams.password, "-pwd")) {
67 if (!parameters.sshParams.privateKeyFile.isEmpty())
68 throw ArgumentErrorException(QLatin1String("-pwd and -k are mutually exclusive."));
69 parameters.sshParams.authenticationType
70 = SshConnectionParameters::AuthenticationByPassword;
74 if (checkAndSetStringArg(pos, parameters.sshParams.privateKeyFile, "-k")) {
75 if (!parameters.sshParams.password.isEmpty())
76 throw ArgumentErrorException(QLatin1String("-pwd and -k are mutually exclusive."));
77 parameters.sshParams.authenticationType
78 = SshConnectionParameters::AuthenticationByKey;
82 if (!checkForNoProxy(pos, parameters.sshParams.proxyType, proxySettingGiven))
83 throw ArgumentErrorException(QLatin1String("unknown option ") + m_arguments.at(pos));
86 Q_ASSERT(pos <= m_arguments.count());
87 if (pos == m_arguments.count() - 1) {
88 if (!checkForNoProxy(pos, parameters.sshParams.proxyType, proxySettingGiven))
89 throw ArgumentErrorException(QLatin1String("unknown option ") + m_arguments.at(pos));
93 throw ArgumentErrorException(QLatin1String("No authentication argument given."));
94 if (parameters.sshParams.host.isEmpty())
95 throw ArgumentErrorException(QLatin1String("No host given."));
96 if (parameters.sshParams.userName.isEmpty())
97 throw ArgumentErrorException(QLatin1String("No user name given."));
99 parameters.sshParams.port = portGiven ? port : 22;
101 parameters.sshParams.timeout = 30;
102 if (!smallFileCountGiven)
103 parameters.smallFileCount = 1000;
104 if (!bigFileSizeGiven)
105 parameters.bigFileSize = 1024;
107 } catch (ArgumentErrorException &ex) {
108 cerr << "Error: " << qPrintable(ex.error) << endl;
115 void ArgumentsCollector::printUsage() const
117 cerr << "Usage: " << qPrintable(m_arguments.first())
118 << " -h <host> -u <user> "
119 << "-pwd <password> | -k <private key file> [ -p <port> ] "
120 << "[ -t <timeout> ] [ -c <small file count> ] "
121 << "[ -s <big file size in MB> ] [ -no-proxy ]" << endl;
124 bool ArgumentsCollector::checkAndSetStringArg(int &pos, QString &arg, const char *opt) const
126 if (m_arguments.at(pos) == QLatin1String(opt)) {
127 if (!arg.isEmpty()) {
128 throw ArgumentErrorException(QLatin1String("option ") + opt
129 + QLatin1String(" was given twice."));
131 arg = m_arguments.at(++pos);
132 if (arg.isEmpty() && QLatin1String(opt) != QLatin1String("-pwd"))
133 throw ArgumentErrorException(QLatin1String("empty argument not allowed here."));
139 bool ArgumentsCollector::checkAndSetIntArg(int &pos, int &val,
140 bool &alreadyGiven, const char *opt) const
142 if (m_arguments.at(pos) == QLatin1String(opt)) {
144 throw ArgumentErrorException(QLatin1String("option ") + opt
145 + QLatin1String(" was given twice."));
148 val = m_arguments.at(++pos).toInt(&isNumber);
150 throw ArgumentErrorException(QLatin1String("option ") + opt
151 + QLatin1String(" needs integer argument"));
159 bool ArgumentsCollector::checkForNoProxy(int &pos,
160 SshConnectionParameters::ProxyType &type, bool &alreadyGiven) const
162 if (m_arguments.at(pos) == QLatin1String("-no-proxy")) {
164 throw ArgumentErrorException(QLatin1String("proxy setting given twice."));
165 type = SshConnectionParameters::NoProxy;