cmdline.cpp 22.6 KB
Newer Older
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
/* 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>
/*
 * 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);
Lisa's avatar
Lisa committed
42
    AkabeiOption removeconfig(APM::RemoveConfig, "n", "nsave", "Also remove configuration files", false);
43 44 45 46 47 48 49 50 51
    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
52
    AkabeiOperation database(APM::DatabaseOperationType, APM::DatabaseOperation, "D", "", "Database operation", false);
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
    
    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);
    
Lisa's avatar
Lisa committed
157 158 159 160 161
    AkabeiOperationList 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;
162
    
Lisa's avatar
Lisa committed
163 164 165 166
    /* These are the only operation we need to access directly later, so better speed up searches a bit */
    specialOperations << removeallcache << help << version;
    
    foreach (AkabeiOperation op, tmp) {
167 168 169 170 171
        /* These options are associated to every operation */
        op.addOption(rootdir);
        op.addOption(cachedir);
        op.addOption(dbpath);
        
Lisa's avatar
Lisa committed
172
        acceptedOperations.insertMulti(op.type, op);      
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
    }
}

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
214
 * This requires some explanation
215 216 217 218 219 220 221 222 223
 * 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
224
    if ((this->type == APM::SyncOperationType && other.name == APM::Install) || (this->type == APM::RemoveOperationType && other.name == APM::Remove)) {
225 226 227
        return;
    }
    
Lisa's avatar
Lisa committed
228
    if (this->name == APM::None || this->name == APM::Remove || this->name == APM::Install || (this->commandShort == "c" && other.name == APM::RemoveAllCache)) {
229 230 231
        this->name = other.name;
        this->type = other.type;
        this->options = other.options;
232 233
        this->commandLong = other.commandLong;
        this->commandShort = other.commandShort;
234 235 236 237 238 239
        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
240
        throw CmdLineException("multiple operations present.");
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 278 279 280 281
    }
}

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
282
     * Adds the only type which is not an operation
283
     */
Lisa's avatar
Lisa committed
284
    koptions.add("Q", ki18n("Query operation"));
285 286 287 288
    
    /*
     * Adds all the operations
     */
Lisa's avatar
Lisa committed
289
    foreach (AkabeiOperation op, acceptedOperations.values()) {
Lisa's avatar
Lisa committed
290 291 292
        if (!op.commandShort.isEmpty()) {
            if (!op.commandLong.isEmpty()) {
                koptions.add(op.commandLong);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
            }
            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, ...)
     */
    foreach (AkabeiOption opt, acceptedOptions) {
        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
326
    QList<QByteArray> found;
327 328 329 330 331
    
    /*
     * 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
332 333
        AkabeiOperation help = specialOperations[1];
        m_operation.set(help);
334
    } else if (args->isSet("v")) {
Lisa's avatar
Lisa committed
335 336
        AkabeiOperation version = specialOperations[2];
        m_operation.set(version);
337 338 339 340 341
    }
    else {
        for (QHash<QByteArray, APM::OperationType>::iterator it = typechars.begin(); it != typechars.end(); it++) {
            if (args->isSet(it.key())) {
                if (type != APM::NoType) {
342
                    throw CmdLineException("multiple types present.");
343 344
                }
                type = it.value();
Lisa's avatar
Lisa committed
345 346 347 348
                
                if (type == APM::QueryOperationType) {
                    found << "Q";
                }
349 350
            
                /*
Lisa's avatar
Lisa committed
351 352
                 * We look only for the acceptable operations
                 * all the others, if present, will be spotted later
353
                 */
Lisa's avatar
Lisa committed
354 355 356 357 358 359
                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()) {
360 361 362 363
                        continue;
                    }
                    
                    /*
Lisa's avatar
Lisa committed
364
                     * Saves up the commands for later inspection
365
                     */
Lisa's avatar
Lisa committed
366 367 368 369 370
                    if (args->isSet(op.commandShort)) {
                        m_operation.set(op);
                        found << op.commandShort;
                        if (!op.commandLong.isEmpty()) {
                            found << op.commandLong;
371 372 373
                        }
                    }
                }
Lisa's avatar
Lisa committed
374 375 376 377 378 379 380 381 382 383
                
                /*
                 * 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;
                }
384 385 386 387 388
            }
        }
    }
    
    if (m_operation.name == APM::None) {
389
        throw CmdLineException("no operation specified.");
390 391 392 393 394 395
    }
    
    foreach (AkabeiOption opt, acceptedOptions) {
        QByteArray command = (opt.commandShort.isEmpty()) ? opt.commandLong : opt.commandShort;
        
        /* 
Lisa's avatar
Lisa committed
396
         * If "found" contains the command, we it was an operation, so go on
397
         */
Lisa's avatar
Lisa committed
398
        if (!found.contains(command) && args->isSet(command)) {            
399
            if (!m_operation.isOptionSupported(opt.name)) {
400
                throw CmdLineException("invalid option for this operation");
401 402
            }
            
Lisa's avatar
Lisa committed
403 404 405 406 407 408 409
            if (!opt.commandShort.isEmpty()) {
                found << opt.commandShort;
            }
            if (!opt.commandLong.isEmpty()) {
                found << opt.commandLong;
            }
            
410 411 412 413 414 415 416 417 418 419
            /*
             * 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) {
420 421
                QString message = "the option \"" + command + "\" must not have an argument.";
                throw CmdLineException(message.toUtf8().data());
422 423 424 425 426 427
            }
            
            /*
             * In this case, we're sure :)
             */
            if (optargs.isEmpty() && opt.hasArg) {
428 429
                QString message = "missing argument for the option \"" + command + "\"";
                throw CmdLineException(message.toUtf8().data());
430 431 432 433 434 435 436 437
            }
            
            opt.args = optargs;
            m_options.insert(opt.name, opt);
        }
    }
    
    /*
Lisa's avatar
Lisa committed
438
     * Gets all the other arguments around
439 440 441 442 443 444
     */
    for (int i = 0; i < args->count(); i++) {
        m_freeArgs << args->arg(i);
    }
    
    if (!m_freeArgs.isEmpty() && !m_operation.hasFreeArgs) {
445
        throw CmdLineException("unexpected free arguments.");
446 447 448
    }
    
    if (m_freeArgs.isEmpty() && m_operation.hasFreeArgs) {
449
        throw CmdLineException("free arguments required but not present.");
450 451 452
    }
    
    args->clear(); /* frees up memory */
Lisa's avatar
Lisa committed
453
    secondParse(KCmdLineArgs::allArguments(), found);
454 455 456
    parsed = true;
}

457
/*
Lisa's avatar
Lisa committed
458 459 460 461
 * 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)
462
 */
Lisa's avatar
Lisa committed
463
void CmdLine::secondParse(QStringList args, QList<QByteArray>& foundCmds)
464
{
Lisa's avatar
Lisa committed
465
    QList<QByteArray> cmdline;
466
    
Lisa's avatar
Lisa committed
467
    foreach (QString part, args) {
468 469 470 471
        if (part.size() < 2) {
            continue;
        }
        
Lisa's avatar
Lisa committed
472
        /* Long command */
473
        if (part.at(0) == '-' && part.at(1) == '-') {
Lisa's avatar
Lisa committed
474 475 476 477 478 479 480 481
            part.remove(0, 2);
            
            int sep = part.indexOf("=");
            if (sep != -1) {
                part.truncate(sep);
            }
            cmdline << part.toUtf8();
            
Lisa's avatar
Lisa committed
482
        /* Short command: takes each letter separately */
483 484 485
        } else if (part.at(0) == '-') {
            part.remove(0, 1);
            
Lisa's avatar
Lisa committed
486 487 488 489 490
            int sep = part.indexOf("=");
            if (sep != -1) {
                part.truncate(sep);
            }
            
491 492 493 494 495 496
            /* 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
497
                    cmdline << "cc";
498 499 500 501
                }
            }
            
            for (int ch = 0; ch < part.length(); ch++) {
Lisa's avatar
Lisa committed
502 503
                QString tmp(part[ch]);
                cmdline << tmp.toUtf8();
504 505 506 507
            }
        }
    }
    
Lisa's avatar
Lisa committed
508 509 510 511 512 513 514 515 516 517 518 519 520
    /*
     * If the command line contains something else other than what we have already parsed
     * there's something wrong around
     */
    foreach (QByteArray com, cmdline) {
        if (!foundCmds.contains(com)) {
            throw CmdLineException("Out of context operation");
        }
    }
    
    /*
     * Counts the occurrence of the operation
     */
521 522 523 524 525
    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
526 527 528
    /*
     * Counts the occurrence of all the options we previously found
     */
529 530 531 532 533 534 535 536 537
    foreach (AkabeiOption opt, m_options.values()) {
        
        /* 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
538
            throw CmdLineException("can't repeat this option twice.");
539 540 541 542
        }
    }
}

543 544 545 546 547
APM::OperationType CmdLine::type()
{
    if (!parsed) {
        throw CmdLineException("Command line not parsed.");
    }
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    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;
}