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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
/*
CommandLine.cpp
This file is part of Charm, a task-based time tracking application.
Copyright (C) 2008-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
Author: Mirko Boehm <mirko.boehm@kdab.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 2 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 "CommandLine.h"
#include "Exceptions.h"
#include <QObject>
#include <QString>
extern "C" {
#include <getopt.h>
}
#include <iostream>
CommandLine::CommandLine(int argc, char **argv)
: m_mode(Mode_None)
, m_index()
, m_userid()
{
opterr = 0;
int ch;
while ((ch = getopt(argc, argv, "vhza:x:c:ri:u:m:")) != -1)
{
if (ch == '?') {
// unparsable argument
int option = optopt;
if (option == 'a') {
throw UsageException(QObject::tr(
"Option -a requires a filename argument"));
}
if (option == 'i') {
throw UsageException(QObject::tr(
"Option -i requires an index argument"));
}
if (option == 'x') {
throw UsageException(QObject::tr(
"Option -x requires a filename argument"));
}
if (option == 'm')
throw UsageException(QObject::tr("Option -m requires a user comment argument"));
if (isprint(option)) {
throw UsageException(
QObject::tr("Unknown option %1").arg(option));
} else {
int code = static_cast<int>(option);
throw UsageException(QObject::tr("Unknown character %1").arg(
code));
}
}
switch (ch) {
case 'a':
if (m_mode != Mode_None) {
QString msg = QObject::tr(
"Multiple mode selections, please use only one");
throw UsageException(msg);
}
// mode
m_filename = QString::fromLocal8Bit(optarg);
m_mode = Mode_AddTimesheet;
break;
case 'x':
if (m_mode != Mode_None) {
QString msg = QObject::tr(
"Multiple mode selections, please use only one");
throw UsageException(msg);
}
// mode
m_exportFilename = QString::fromLocal8Bit(optarg);
m_mode = Mode_ExportProjectcodes;
break;
case 'c':
if (m_mode != Mode_None) {
QString msg = QObject::tr(
"Multiple mode selections, please use only one");
throw UsageException(msg);
}
m_userName = QString::fromLocal8Bit(optarg);
m_mode = Mode_CheckOrCreateUser;
break;
case 'r': // remove
if (m_mode != Mode_None) {
QString msg = QObject::tr(
"Multiple mode selections, please use only one");
throw UsageException(msg);
}
m_mode = Mode_RemoveTimesheet;
break;
case 'u': // user id
{
if (m_userid != 0) {
QString msg = QObject::tr(
"Multiple user id selections, please use only one");
throw UsageException(msg);
}
QString arg = QString::fromLocal8Bit(optarg);
bool ok;
m_userid = arg.toInt(&ok);
if (!ok || m_userid < 1) {
throw UsageException(
QObject::tr(
"Argument to option -u must be an integer user id larger than zero"));
}
break;
}
case 'i':
{
// index
if (m_index != 0) {
QString msg = QObject::tr(
"Multiple index selections, please use only one");
throw UsageException(msg);
}
QString arg = QString::fromLocal8Bit(optarg);
bool ok;
m_index = arg.toInt(&ok);
if (!ok || m_index < 1) {
throw UsageException(
QObject::tr(
"Argument to option -i must be an integer index larger than zero"));
}
break;
}
case 'm':
{
if (!m_userComment.isEmpty()) {
QString msg = QObject::tr("Multiple user comments specified, please use only one");
throw UsageException(msg);
}
QString arg = QString::fromLocal8Bit(optarg);
m_userComment = arg;
break;
}
case 'z':
// initialize the database
m_mode = Mode_InitializeDatabase;
break;
case 'v':
m_mode = Mode_PrintVersion;
break;
case 'h':
default:
// help/usage
m_mode = Mode_DescribeUsage;
//return;
}
}
// check for other command line arguments and yell at the user:
if (optind < argc) {
QString msg;
for (int index = optind; index < argc; index++) {
msg += QObject::tr("Unknown extra argument \"%1\"\n").arg(
QString::fromLatin1(argv[index]));
}
throw UsageException(msg);
}
// final checks:
QString msg;
if (m_mode == Mode_None) {
msg += QObject::tr("No mode selected. Use one of -a filename, -r.");
} else if (m_mode == Mode_RemoveTimesheet) {
if (m_index < 1) {
msg += QObject::tr("No index specified. -a filename, "
" -r require an index specified with -i.");
}
if (m_userid < 1) {
msg += QObject::tr("No userid specified. -a filename, "
" -r require a user id specified with -u.");
}
} else if (m_mode == Mode_AddTimesheet) {
if (m_index > 0)
msg += QObject::tr(
"Specifying an index when adding a time sheet is not supported anymore.");
}
if (!msg.isEmpty())
throw UsageException(msg);
}
CommandLine::CommandLine(const QString file, const int userId)
{
m_filename = file;
m_userid = userId;
}
CommandLine::CommandLine(const int userId, const int index)
{
m_userid = userId;
m_index = index;
}
CommandLine::Mode CommandLine::mode() const
{
return m_mode;
}
QString CommandLine::userName() const
{
return m_userName;
}
QString CommandLine::userComment() const
{
return m_userComment;
}
QString CommandLine::filename() const
{
return m_filename;
}
QString CommandLine::exportFilename() const
{
return m_exportFilename;
}
int CommandLine::userid() const
{
return m_userid;
}
int CommandLine::index() const
{
return m_index;
}
void CommandLine::usage()
{
using namespace std;
cout << "Timesheet Processor, (C) 2008 Mirko Boehm, KDAB" << endl
<< "Usage: " << endl
<< " * TimesheetProzessor -h <-- get help"
<< endl
<< " * TimesheetProzessor -v <-- print version"
<< endl
<< " * TimesheetProzessor -a filename -u userid -m comment <-- add timesheet from file"
<< endl
<<
" * TimesheetProzessor -r -i index -u userid <-- remove timesheet at index"
<< endl
<<
" * TimesheetProzessor -c username <-- create user if user does not exist"
<< endl
<<
" * TimesheetProzessor -x filename <-- export project codes to XML file"
<< endl
<<
" * TimesheetProzessor -z <-- initialize database (careful!)"
<< endl;
}
|