cmdline.cpp 23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* This file is part of the Chakra project

   Copyright (C) 2011 Lisa "shainer" Vitolo <shainer@chakra-project.org>

   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.
*/

#include "cmdline.h"

#include <kaboutdata.h>
#include <klocalizedstring.h>
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
/*
 * Build our database of accepted operations and options with the right properties and associations
 */
CmdLine::CmdLine()
: parsed(false)
{
    typechars.insert("D", APM::DatabaseOperationType);
    typechars.insert("Q", APM::QueryOperationType);
    typechars.insert("R", APM::RemoveOperationType);
    typechars.insert("S", APM::SyncOperationType);
    typechars.insert("U", APM::UpgradeOperationType);
    
    /*
     * The argument list is only meaningful after reading the command line, so for now it stays empty
     */
    AkabeiOption querypkgfile(APM::QueryPackageFile, "p", "file", "Queries a package file instead of the database", true);
    AkabeiOption showlocal(APM::ShowLocal, "m", "foreign", "Shows packages which were not found in the Sync-db(s)", false);
    AkabeiOption showless(APM::ShowLess, "q", "quiet", "Shows less information in queries and searches", false);
    AkabeiOption installasdeps(APM::InstallAsDeps, "", "asdeps", "Install all packages as non-explicit", false);
    AkabeiOption installasexplicit(APM::InstallAsExplicit, "", "asexplicit", "APM::Install all packages as explicit", false);
    AkabeiOption skipdependencycheck(APM::SkipDependencyCheck, "w", "nodeps", "Skip dependency check", false);
    AkabeiOption force(APM::Force, "f", "force", "Force installation. Overwrite file conflicts", false);
    AkabeiOption downloadonly(APM::DownloadOnly, "d", "downloadonly", "Downloads packages without installing or upgrading them", false);
    AkabeiOption onlyneeded(APM::OnlyNeeded, "", "needed", "Up-to-date packages are not reinstalled", false);
    AkabeiOption ignore(APM::Ignore, "", "ignore", "Ignores a certain package. Can be used more than once.", true);
    AkabeiOption ignoregroup(APM::IgnoreGroup, "", "ignoregroup", "Ignores upgrade of a group", true);
    AkabeiOption databaseonly(APM::DatabaseOnly, "k", "dbonly", "Only remove database entries", false);
43
    AkabeiOption removeconfig(APM::RemoveConfig, "n", "nosave", "Also remove configuration files", false);
44 45 46 47 48 49 50 51 52
    AkabeiOption recursive(APM::Recursive, "s", "recursive", "Remove dependencies as well. (-ss includes explicitely installed dependencies)", false);
    AkabeiOption rootdir(APM::RootDir, "", "root", "Set a different root dir", true);
    AkabeiOption cachedir(APM::CacheDir, "", "cachedir", "Set a different cache dir", true);
    AkabeiOption dbpath(APM::DBPath, "b", "dbpath", "Set a different database dir", false);
    
    acceptedOptions << querypkgfile << showlocal << showless << installasdeps << installasexplicit << skipdependencycheck;
    acceptedOptions << force << downloadonly << onlyneeded << ignore << ignoregroup << databaseonly << removeconfig << recursive;
    acceptedOptions << rootdir << cachedir << dbpath;
    
Lisa's avatar
Lisa committed
53
    AkabeiOperation database(APM::DatabaseOperationType, APM::DatabaseOperation, "D", "", "Database operation", false);
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
    
    AkabeiOperation changelog(APM::QueryOperationType, APM::ShowChangelog, "c", "changelog", "Shows changelog of the package", true);
    changelog.addOption(querypkgfile);
    
    AkabeiOperation installedasdeps(APM::QueryOperationType, APM::ShowInstalledAsDeps, "d", "deps", "Shows packages which got installed as dependencies", false);
    installedasdeps.addOption(querypkgfile);
    installedasdeps.addOption(ignore);
    installedasdeps.addOption(ignoregroup);
    
    AkabeiOperation installedexp(APM::QueryOperationType, APM::ShowInstalledExplicitely, "e", "explicit", "Shows packages which got installed explicitely", false);
    installedexp.addOption(querypkgfile);
    installedexp.addOption(ignore);
    installedexp.addOption(ignoregroup);
    
    AkabeiOperation checkfiles(APM::QueryOperationType, APM::CheckFiles, "k", "check", "Check if the files of the package are available", true);
    checkfiles.addOption(querypkgfile);
    checkfiles.addOption(showlocal);
    
    AkabeiOperation showowner(APM::QueryOperationType, APM::ShowOwner, "o", "owns", "Returns the package which contains the file", true);
    showowner.addOption(querypkgfile);
    showowner.addOption(showlocal);
    
    AkabeiOperation notrequired(APM::QueryOperationType, APM::ShowNotRequired, "t", "unrequired", "Returns packages which are not required by others", false);
    notrequired.addOption(querypkgfile);
    notrequired.addOption(ignore);
    notrequired.addOption(ignoregroup);
    
    AkabeiOperation upgradeable(APM::QueryOperationType, APM::ShowUpgradeable, "u", "upgrades", "Returns upgradeable packages", false);
    upgradeable.addOption(querypkgfile);
    upgradeable.addOption(ignore);
    upgradeable.addOption(ignoregroup);
    
    AkabeiOperation groupsl(APM::QueryOperationType, APM::ShowPackagesOfGroupLocal, "g", "groups", "Shows all packages in a package group", true);
    groupsl.addOption(ignore);
    
    AkabeiOperation infol(APM::QueryOperationType, APM::ShowInformationLocal, "i", "info", "Shows package information", true);
    infol.addOption(showless);
    
    AkabeiOperation searchl(APM::QueryOperationType, APM::SearchLocal, "s", "search", "Searches repositories for a word", true);
    searchl.addOption(ignore);
    searchl.addOption(ignoregroup);
    
    AkabeiOperation remove(APM::RemoveOperationType, APM::Remove, "R", "remove", "Remove packages", true);
    remove.addOption(skipdependencycheck);
    remove.addOption(force);
    remove.addOption(databaseonly);
    remove.addOption(removeconfig);
    remove.addOption(recursive);
    
    AkabeiOperation cascade(APM::RemoveOperationType, APM::Cascade, "c", "cascade", "Remove packages with their dependencies", true);
    
    AkabeiOperation unneeded(APM::RemoveOperationType, APM::Unneeded, "u", "unneeded", "Remove not needed packages", false);
    unneeded.addOption(ignore);
    unneeded.addOption(ignoregroup);
    
    AkabeiOperation install(APM::SyncOperationType, APM::Install, "S", "install", "Install packages", true);
    install.addOption(installasdeps);
    install.addOption(installasexplicit);
    install.addOption(skipdependencycheck);
    install.addOption(force);
    install.addOption(downloadonly);
    install.addOption(onlyneeded);

    AkabeiOperation removeold(APM::SyncOperationType, APM::RemoveOldFromCache, "c", "clean", "Remove old packages from cache", false);
    
    AkabeiOperation removeallcache(APM::SyncOperationType, APM::RemoveAllCache, "cc", "", "Remove all packages from cache", false);
    
    AkabeiOperation groups(APM::SyncOperationType, APM::ShowPackagesOfGroup, "g", "groups", "Shows all packages in a package group", true);
    groups.addOption(ignore);
    
    AkabeiOperation info(APM::SyncOperationType, APM::ShowInformation, "i", "info", "Shows package information", true);
    info.addOption(showless);
    
    AkabeiOperation search(APM::SyncOperationType, APM::Search, "s", "search", "Searches repositories for a word", true);
    search.addOption(ignore);
    search.addOption(ignoregroup);
    
    AkabeiOperation listrepo(APM::SyncOperationType, APM::ListRepo, "l", "list", "List all packages in a repository", true);
    listrepo.addOption(querypkgfile);
    listrepo.addOption(showlocal);
    listrepo.addOption(ignore);
    listrepo.addOption(ignoregroup);
    
    AkabeiOperation updatedb(APM::SyncOperationType, APM::UpdateDatabases, "y", "refresh", "Refresh database", false);
    
    AkabeiOperation updatesys(APM::SyncOperationType, APM::UpdateSystem, "u", "sysupgrade", "Update system", false);
    updatesys.addOption(downloadonly);
    updatesys.addOption(ignore);
    updatesys.addOption(ignoregroup);
    
    AkabeiOperation updatedbandsys(APM::SyncOperationType, APM::UpdateDatabaseAndSystem, "", "", "Refresh database and update system", false);
    updatedbandsys.addOption(downloadonly);
    updatedbandsys.addOption(ignore);
    updatedbandsys.addOption(ignoregroup);
    
    AkabeiOperation upgrade(APM::UpgradeOperationType, APM::Upgrade, "U", "", "Upgrade operation", true);
    upgrade.addOption(downloadonly);
    upgrade.addOption(ignore);
    upgrade.addOption(ignoregroup);
    
    AkabeiOperation help(APM::NoType, APM::Help, "h", "help", "Shows the help message", false);
    
    AkabeiOperation version(APM::NoType, APM::Version, "v", "version", "Shows Akabei version", false);
    
158 159 160 161 162
    QList<AkabeiOperation *> tmp;
    tmp << &removeallcache << &help << &version << &install << &remove << &database << &changelog << &installedasdeps << &installedexp;
    tmp << &checkfiles << &showowner << &notrequired << &upgradeable << &cascade;
    tmp << &removeold << &groups << &info << &listrepo << &groupsl << &searchl << &infol;
    tmp << &search << &updatedb << &updatesys << &updatedbandsys << &upgrade;
Lisa's avatar
Lisa committed
163
    
164 165 166 167 168
    /*
     * It has to be pointers otherwise the changes in this foreach
     * aren't reflected in specialOperations below
     */
    foreach (AkabeiOperation *op, tmp) {
169
        /* These options are associated to every operation */
170 171 172
        op->addOption(rootdir);
        op->addOption(cachedir);
        op->addOption(dbpath);
173
        
174
        acceptedOperations.insertMulti(op->type, *op);      
175
    }
176 177 178
    
    /* These are the only operation we need to access directly later, so better speed up searches a bit */
    specialOperations << removeallcache << help << version;
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
}

AkabeiOption::AkabeiOption(APM::OptionName n, const char *cs, const char *cl, const char *desc, bool hasArg)
{
    QStringList unused;
    init(n, cs, cl, desc, hasArg, unused);
}

AkabeiOption::AkabeiOption(APM::OptionName n, const char *cs, const char *cl, const char *desc, bool hasArg, QStringList a)
{
    init(n, cs, cl, desc, hasArg, a);
}

void AkabeiOption::init(APM::OptionName n, const char *cs, const char *cl, const char *desc, bool hasArg, QStringList a)
{
    name = n;
    commandShort = QString::fromAscii(cs).toUtf8();
    commandLong = QString::fromAscii(cl).toUtf8();
    description = ki18n(desc);
    this->hasArg = hasArg;
    this->args = a;
}

AkabeiOperation::AkabeiOperation(APM::OperationType t, APM::OperationName n, const char *cs, const char *cl, const char *desc, bool hasFreeArgs)
{
    QStringList args;
    init(t, n, cs, cl, desc, hasFreeArgs, args);
}

AkabeiOperation::AkabeiOperation(APM::OperationType t, APM::OperationName n, const char *cs, const char *cl, const char *desc, bool hasFreeArgs, QStringList args)
{
    init(t, n, cs, cl, desc, hasFreeArgs, args);
}

AkabeiOperation::AkabeiOperation()
{
    init(APM::NoType, APM::None, "", "", "", false, QStringList());
}

/*
Lisa's avatar
Lisa committed
219
 * This requires some explanation
220 221 222 223 224 225 226 227 228
 * 0: APM::None means no operation has still been set, so no problem here.
 * 1: -S is Install, but for example -Ss is Search. Same for most removing operations. This overlapping is fine.
 * 2: the elements in a hash aren't sorted, so "s" may be found before "S".
 * 3: for -Scc, both "cc" and "c" are found. Only the first one is good.
 * 4: -Sy is UpdateDatabases, -Su is UpdateSystem, but -Syu is APM::UpdateDatabaseAndSystem (only linked operations accepted).
 * 5: All the rest is invalid
 */
void AkabeiOperation::set(AkabeiOperation& other)
{
Lisa's avatar
Lisa committed
229
    if ((this->type == APM::SyncOperationType && other.name == APM::Install) || (this->type == APM::RemoveOperationType && other.name == APM::Remove)) {
230 231 232
        return;
    }
    
Lisa's avatar
Lisa committed
233
    if (this->name == APM::None || this->name == APM::Remove || this->name == APM::Install || (this->commandShort == "c" && other.name == APM::RemoveAllCache)) {
234 235 236
        this->name = other.name;
        this->type = other.type;
        this->options = other.options;
237 238
        this->commandLong = other.commandLong;
        this->commandShort = other.commandShort;
239 240 241 242 243 244
        this->hasFreeArgs = other.hasFreeArgs;
    } else if ((this->name == APM::UpdateDatabases && other.name == APM::UpdateSystem) || (this->name == APM::UpdateSystem && other.name == APM::UpdateDatabases)) {
        this->name = APM::UpdateDatabaseAndSystem;
        this->type = APM::SyncOperationType;
        this->hasFreeArgs = false;
    } else {
Lisa's avatar
Lisa committed
245
        throw CmdLineException("multiple operations present.");
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 278 279 280 281 282 283 284 285 286
    }
}

void AkabeiOperation::init(APM::OperationType t, APM::OperationName n, const char *cs, const char *cl, const char *desc, bool hasFreeArgs, QStringList args)
{
    type = t;
    name = n;
    commandShort = QString::fromAscii(cs).toUtf8();
    commandLong = QString::fromAscii(cl).toUtf8();
    description = ki18n(desc);
    this->hasFreeArgs = hasFreeArgs;
    this->freeArgs = args;
}

void AkabeiOperation::addOption(AkabeiOption& opt)
{
    options.insert(opt.name, opt);
}

bool AkabeiOperation::isOptionSupported(APM::OptionName name)
{
    return options.contains(name);
}

/*
 * Actual parsing
 */
void CmdLine::parse(int argc, char** argv)
{
    KAboutData aboutData("akabei", 0, ki18n("Akabei"),
                         QByteArray("0.0.1"),
                         ki18n("Chakra's CLI package manager"),
                         KAboutData::License_GPL,
                         ki18n("(C) 2011 Lukas Appelhans"),
                         ki18n("<a href=\"mailto:boom1992@chakra-project.org\">boom1992@chakra-project.org</a>"));
    
    aboutData.addAuthor(ki18n("Lukas Appelhans"), ki18n("Maintainer"), "boom1992@chakra-project.org");
    KCmdLineArgs::init(argc, argv, &aboutData);
    KCmdLineOptions koptions;
    
    /*
Lisa's avatar
Lisa committed
287
     * Adds the only type which is not an operation
288
     */
Lisa's avatar
Lisa committed
289
    koptions.add("Q", ki18n("Query operation"));
290 291 292 293
    
    /*
     * Adds all the operations
     */
294
    foreach (const AkabeiOperation& op, acceptedOperations.values()) {
Lisa's avatar
Lisa committed
295 296 297
        if (!op.commandShort.isEmpty()) {
            if (!op.commandLong.isEmpty()) {
                koptions.add(op.commandLong);
298 299 300 301 302 303 304 305 306
            }
            koptions.add(op.commandShort, op.description);
        }
    }
    
    /*
     * Adds all the option with <argument> if they require one
     * TODO: make <argument> more explicative of its nature (package, group, filename, ...)
     */
307
    foreach (const AkabeiOption& opt, acceptedOptions) {
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
        QByteArray newcomlong = opt.commandLong;
        if (opt.hasArg) {
            newcomlong += " <argument>";
        }
        if (!opt.commandShort.isEmpty()) {
            koptions.add(opt.commandShort);
        }
        koptions.add(newcomlong, opt.description);
    }
    
    /*
     * Free arguments at the end
     */
    koptions.add("+[arg1]", ki18n("An optional argument 'arg1'"));
    
    /*
     * Parsing!
     */
    KCmdLineArgs::addCmdLineOptions(koptions);
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    
    APM::OperationType type = APM::NoType;
    m_operation.name = APM::None;
Lisa's avatar
Lisa committed
331
    QList<QByteArray> found;
332 333 334 335 336
    
    /*
     * These are considered separately and when found, they surpass any other operation that may be present.
     */
    if (args->isSet("h")) {
Lisa's avatar
Lisa committed
337 338
        AkabeiOperation help = specialOperations[1];
        m_operation.set(help);
339
    } else if (args->isSet("v")) {
Lisa's avatar
Lisa committed
340 341
        AkabeiOperation version = specialOperations[2];
        m_operation.set(version);
342 343 344 345 346
    }
    else {
        for (QHash<QByteArray, APM::OperationType>::iterator it = typechars.begin(); it != typechars.end(); it++) {
            if (args->isSet(it.key())) {
                if (type != APM::NoType) {
347
                    throw CmdLineException("multiple types present.");
348 349
                }
                type = it.value();
Lisa's avatar
Lisa committed
350 351 352 353
                
                if (type == APM::QueryOperationType) {
                    found << "Q";
                }
354 355
            
                /*
Lisa's avatar
Lisa committed
356 357
                 * We look only for the acceptable operations
                 * all the others, if present, will be spotted later
358
                 */
Lisa's avatar
Lisa committed
359 360 361 362 363 364
                foreach (AkabeiOperation op, acceptedOperations.values(type)) {
                    /*
                     * This eliminates the only "dummy" operation with no commands at all
                     * every other operation has got at least the short command
                     */
                    if (op.commandShort.isEmpty()) {
365 366 367 368
                        continue;
                    }
                    
                    /*
Lisa's avatar
Lisa committed
369
                     * Saves up the commands for later inspection
370
                     */
Lisa's avatar
Lisa committed
371 372 373 374 375
                    if (args->isSet(op.commandShort)) {
                        m_operation.set(op);
                        found << op.commandShort;
                        if (!op.commandLong.isEmpty()) {
                            found << op.commandLong;
376 377 378
                        }
                    }
                }
Lisa's avatar
Lisa committed
379 380 381 382 383 384 385 386 387 388
                
                /*
                 * This kind of command is unfortunately completely ignored (-S -cc is fine)
                 * so we have to check it separately. Again, malformed versions will be spotted later
                 */
                if (KCmdLineArgs::allArguments().contains("-Scc")) {
                    AkabeiOperation special = specialOperations.first();
                    m_operation.set(special);
                    found << special.commandShort;
                }
389 390 391 392 393
            }
        }
    }
    
    if (m_operation.name == APM::None) {
394
        throw CmdLineException("no operation specified.");
395 396 397 398 399
    }
    
    foreach (AkabeiOption opt, acceptedOptions) {
        QByteArray command = (opt.commandShort.isEmpty()) ? opt.commandLong : opt.commandShort;
        
400 401 402 403 404 405 406 407 408 409
        bool condition = !found.contains(command); /* if found contains the command, we already decided it's an operation */
        
        /* This is a special case for cause of the "nosave" option */
        if (opt.name == APM::RemoveConfig) {
            condition &= !args->isSet("save");
        } else {
            condition &= args->isSet(command); /* normal check */
        }
        
        if (condition) {            
410
            if (!m_operation.isOptionSupported(opt.name)) {
411
                throw CmdLineException("invalid option for this operation");
412 413
            }
            
Lisa's avatar
Lisa committed
414 415 416 417 418 419 420
            if (!opt.commandShort.isEmpty()) {
                found << opt.commandShort;
            }
            if (!opt.commandLong.isEmpty()) {
                found << opt.commandLong;
            }
            
421 422 423 424 425 426 427 428 429 430
            /*
             * Gets arguments associated with the option
             */
            QStringList optargs = args->getOptionList(command);

            /*
             * Remember, if it's not an option args it can be an argument to the operation
             * so we're not really sure an error has been made, unless the operation doesn't admit any argument
             */
            if (!optargs.isEmpty() && !opt.hasArg && !m_operation.hasFreeArgs) {
431 432
                QString message = "the option \"" + command + "\" must not have an argument.";
                throw CmdLineException(message.toUtf8().data());
433 434 435 436 437 438
            }
            
            /*
             * In this case, we're sure :)
             */
            if (optargs.isEmpty() && opt.hasArg) {
439 440
                QString message = "missing argument for the option \"" + command + "\"";
                throw CmdLineException(message.toUtf8().data());
441 442 443 444 445 446 447 448
            }
            
            opt.args = optargs;
            m_options.insert(opt.name, opt);
        }
    }
    
    /*
Lisa's avatar
Lisa committed
449
     * Gets all the other arguments around
450 451 452 453 454 455
     */
    for (int i = 0; i < args->count(); i++) {
        m_freeArgs << args->arg(i);
    }
    
    if (!m_freeArgs.isEmpty() && !m_operation.hasFreeArgs) {
456
        throw CmdLineException("unexpected free arguments.");
457 458 459
    }
    
    if (m_freeArgs.isEmpty() && m_operation.hasFreeArgs) {
460
        throw CmdLineException("free arguments required but not present.");
461 462 463
    }
    
    args->clear(); /* frees up memory */
Lisa's avatar
Lisa committed
464
    secondParse(KCmdLineArgs::allArguments(), found);
465 466 467
    parsed = true;
}

468
/*
Lisa's avatar
Lisa committed
469 470 471 472
 * This function handles all the parsing which can't be handled before
 * In particular, checks
 * * if the operation is repeated more than once (not accepted)
 * * if there's something "out of context" in the command line (like, some meaningless command)
473
 */
Lisa's avatar
Lisa committed
474
void CmdLine::secondParse(QStringList args, QList<QByteArray>& foundCmds)
475
{
Lisa's avatar
Lisa committed
476
    QList<QByteArray> cmdline;
477
    
Lisa's avatar
Lisa committed
478
    foreach (QString part, args) {
479 480 481 482
        if (part.size() < 2) {
            continue;
        }
        
Lisa's avatar
Lisa committed
483
        /* Long command */
484
        if (part.at(0) == '-' && part.at(1) == '-') {
Lisa's avatar
Lisa committed
485 486 487 488 489 490 491 492
            part.remove(0, 2);
            
            int sep = part.indexOf("=");
            if (sep != -1) {
                part.truncate(sep);
            }
            cmdline << part.toUtf8();
            
Lisa's avatar
Lisa committed
493
        /* Short command: takes each letter separately */
494 495 496
        } else if (part.at(0) == '-') {
            part.remove(0, 1);
            
Lisa's avatar
Lisa committed
497 498 499 500 501
            int sep = part.indexOf("=");
            if (sep != -1) {
                part.truncate(sep);
            }
            
502 503 504 505 506 507
            /* The only short commands with two letters is treated separately to avoid problems */
            if (part.contains("cc")) {
                if (part.count("cc") > 1) {
                    throw CmdLineException("can't accept linked operations.");
                } else {
                    part.remove("cc");
Lisa's avatar
Lisa committed
508
                    cmdline << "cc";
509 510 511 512
                }
            }
            
            for (int ch = 0; ch < part.length(); ch++) {
Lisa's avatar
Lisa committed
513 514
                QString tmp(part[ch]);
                cmdline << tmp.toUtf8();
515 516 517 518
            }
        }
    }
    
Lisa's avatar
Lisa committed
519 520 521 522
    /*
     * If the command line contains something else other than what we have already parsed
     * there's something wrong around
     */
523
    foreach (const QByteArray& com, cmdline) {
Lisa's avatar
Lisa committed
524 525 526 527 528 529 530 531
        if (!foundCmds.contains(com)) {
            throw CmdLineException("Out of context operation");
        }
    }
    
    /*
     * Counts the occurrence of the operation
     */
532 533 534 535 536
    int occurrences = cmdline.count(m_operation.commandLong) + cmdline.count(m_operation.commandShort);
    if (occurrences > 1) {
        throw CmdLineException("can't accept linked operations.");
    }
    
Lisa's avatar
Lisa committed
537 538 539
    /*
     * Counts the occurrence of all the options we previously found
     */
540
    foreach (const AkabeiOption& opt, m_options.values()) {
541 542 543 544 545 546 547 548
        
        /* The only options allowed to be repeated several times */
        if (opt.name == APM::Ignore || opt.name == APM::IgnoreGroup) {
            continue;
        }
        
        occurrences = cmdline.count(opt.commandLong) + cmdline.count(opt.commandShort);
        if (occurrences > 1) {
Lisa's avatar
Lisa committed
549
            throw CmdLineException("can't repeat this option twice.");
550 551 552 553
        }
    }
}

554 555 556 557 558
APM::OperationType CmdLine::type()
{
    if (!parsed) {
        throw CmdLineException("Command line not parsed.");
    }
559

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
    return m_operation.type;
}

APM::OperationName CmdLine::operation()
{
    if (!parsed) {
        throw CmdLineException("Command line not parsed.");
    }
    
    return m_operation.name;
}

QStringList CmdLine::freeArgs()
{
    if (!parsed) {
        throw CmdLineException("Command line not parsed.");
    }
    
    return m_freeArgs;
}

QHash< APM::OptionName, AkabeiOption > CmdLine::options()
{
    if (!parsed) {
        throw CmdLineException("Command line not parsed.");
    }
    
    return m_options;
}