Esempio n. 1
0
File: main.cpp Progetto: KDE/solid
int main(int argc, char **argv)
{
    qputenv("SOLID_POWER_BACKEND", "FREE_DESKTOP");

    QCoreApplication app(argc, argv);
    app.setApplicationName(QLatin1Literal("solid-power"));

    QCommandLineParser parser;
    parser.setApplicationDescription(QCoreApplication::translate("solid-power", "Tool to know and set the power management state of your device"));
    parser.addHelpOption();
    parser.addPositionalArgument("command", QCoreApplication::translate("solid-power", "Command to execute"));

    QCommandLineOption commands("commands", QCoreApplication::translate("solid-power", "Show available commands"));
    parser.addOption(commands);

    parser.process(app);

    if (parser.isSet(commands))
    {
        sOut << endl << QCoreApplication::translate("solid-hardware", "Syntax:") << endl << endl;

        sOut << "  solid-power show" << endl;
        sOut << QCoreApplication::translate("solid-power",
                "             # Show all the power management information from the system.\n"
                "             # - acPlugged: whether the device is connected to the AC or not\n") << endl;
        return 1;
    }

    SolidPower power;

    QStringList args = parser.positionalArguments();
    if (args.count() < 1) {
        parser.showHelp(1);
        return 1;
    }

    parser.clearPositionalArguments();

    QString command(args.at(0));

    if (command == QLatin1String("show")) {
        power.show();
    } else if(command == QLatin1String("listen")) {
        sOut << "Listening to events:" << endl;
        power.listen();
        app.exec();
    } else {
        sOut << "Not recognized command" << endl;
    }
}
int main (int argc, char *argv[])
{
  LibertineConfig config;
  ContainerConfigList* containers;
  QCommandLineParser commandlineParser;

  QCoreApplication app(argc, argv);
  app.setApplicationName(LIBERTINE_APPLICATION_NAME);

  initialize_python();

  containers = new ContainerConfigList(&config);

  commandlineParser.setApplicationDescription("Command-line tool to manage sandboxes for running legacy DEB-packaged X11-based applications");
  commandlineParser.addHelpOption();

  commandlineParser.addPositionalArgument("command", "[create | destroy | install-package | update | list]");

  commandlineParser.parse(QCoreApplication::arguments());

  const QStringList args = commandlineParser.positionalArguments();
  const QString command = args.isEmpty() ? QString() : args.first();

  if (command == "create")
  {
    commandlineParser.clearPositionalArguments();
    commandlineParser.addPositionalArgument("create", QCoreApplication::translate("main", "Create a new Libertine container."));
    commandlineParser.addOption({{"t", "type"}, QCoreApplication::translate("main", "Type of container.  Either 'lxc' or 'chroot'."), "container_type"});
    commandlineParser.process(app);

    QString password;
    const QString container_type = commandlineParser.value("type");

    if (container_type == "lxc")
    {
      PasswordHelper passwordHelper;
      int i = 1;

      while (1)
      {
        password = passwordHelper.GetPassword();

        if (password.isNull())
        {
          return 0;
        }
        else if (passwordHelper.VerifyUserPassword(password))
        {
          break;
        }
        else if (i == 3)
        {
          cout << "Too many password attempts." << endl;
          return 0;
        }
        else
        {
          cout << "Wrong password entered.  Please try again." << endl;
          ++i;
        }
      }
    }

    QVariantMap image;
    image.insert("id", "wily");
    image.insert("name", "Ubuntu 'Wily Werewolf'");
    QString container_id = containers->addNewContainer(image, container_type);

    ContainerManagerWorker *worker = new ContainerManagerWorker(ContainerManagerWorker::ContainerAction::Create,
                                                                container_id,
                                                                container_type,
                                                                password);
    QObject::connect(worker, SIGNAL(finished()), &app, SLOT(quit()));
    worker->start();
  }
  else if (command == "destroy")
  {
    commandlineParser.clearPositionalArguments();
    commandlineParser.addPositionalArgument("destroy", QCoreApplication::translate("main", "Destroy an existing Libertine container."));
    commandlineParser.addOption({{"n", "name"}, QCoreApplication::translate("main", "Name of container"), "container_name"});

    commandlineParser.process(app);

    QString container_id;
    if (commandlineParser.isSet("name"))
    {
      container_id = commandlineParser.value("name");
    }
    else
    {
      container_id = containers->default_container_id();
    }

    if (containers->deleteContainer(container_id))
    {
      ContainerManagerWorker *worker = new ContainerManagerWorker(ContainerManagerWorker::ContainerAction::Destroy,
                                                                  container_id,
                                                                  containers->getContainerType(container_id));
      QObject::connect(worker, SIGNAL(finished()), &app, SLOT(quit()));
      worker->start();
    }
    else
    {
      cout << QCoreApplication::translate("main", "The container name specified does not exist.").toStdString().c_str() << endl;
      return -1;
    }
  }
  else if (command == "install-package")
  {
    commandlineParser.clearPositionalArguments();
    commandlineParser.addPositionalArgument("install-package", QCoreApplication::translate("main", "Install a package in an existing Libertine container."));

    commandlineParser.addOption({{"n", "name"}, QCoreApplication::translate("main", "Name of container"), "container_name"});
    commandlineParser.addOption({{"p", "package"}, QCoreApplication::translate("main", "Name of package to install"), "package_name"});

    commandlineParser.process(app);

    QString container_id;
    if (commandlineParser.isSet("name"))
    {
      container_id = commandlineParser.value("name");
    }
    else
    {
      container_id = containers->default_container_id();
    }

    if (commandlineParser.isSet("package"))
    {
      const QString package_name = commandlineParser.value("package");

      containers->addNewApp(container_id, package_name);

      ContainerManagerWorker *worker = new ContainerManagerWorker(ContainerManagerWorker::ContainerAction::Install,
                                                                  container_id,
                                                                  containers->getContainerType(container_id),
                                                                  package_name);
      QObject::connect(worker, SIGNAL(finished()), &app, SLOT(quit()));
      worker->start();
    }
    else
    {
      cout << QCoreApplication::translate("main", "You must specify a package name when using the install-package command!").toStdString().c_str() << endl;
      commandlineParser.showHelp(-1);
    }
  }
  else if (command == "update")
  {
    commandlineParser.clearPositionalArguments();
    commandlineParser.addPositionalArgument("update", QCoreApplication::translate("main", "Update packages in an existing Libertine container."));

    commandlineParser.addOption({{"n", "name"}, QCoreApplication::translate("main", "Name of container"), "container_name"});

    commandlineParser.process(app);

    QString container_id;
    if (commandlineParser.isSet("name"))
    {
      container_id = commandlineParser.value("name");
    }
    else
    {
      container_id = containers->default_container_id();
    }

    ContainerManagerWorker *worker = new ContainerManagerWorker(ContainerManagerWorker::ContainerAction::Update,
                                                                container_id,
                                                                containers->getContainerType(container_id));
    QObject::connect(worker, SIGNAL(finished()), &app, SLOT(quit()));
    worker->start();
  }
  else if (command == "list")
  {
    commandlineParser.clearPositionalArguments();
    commandlineParser.addPositionalArgument("list", "List all existing Libertine containers.");
    commandlineParser.process(app);

    int count = containers->size();
    QVariant name, id;

    cout << setw(10) << left << "id" << setw(30) << left << "Container Name" << endl;
    for (int i = 0; i < count; ++i)
    {
      name = containers->data(containers->index(i, 0), (int)ContainerConfigList::DataRole::ContainerName);
      id = containers->data(containers->index(i, 0), (int)ContainerConfigList::DataRole::ContainerId);
      cout << setw(10) << left << id.toString().toStdString() << setw(30) << left << name.toString().toStdString() << endl;
    }

    return 0;
  }
  else
  {
    cout << QCoreApplication::translate("main", "Invalid command specified!").toStdString().c_str() << endl;
    commandlineParser.showHelp(-1);
  }

  return app.exec();
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    app.setApplicationVersion("1.0");

    // Test for QCoreApplication::arguments()
    const QStringList incomingArgs = QCoreApplication::arguments();
    for (int i = 0; i < argc; ++i) {
        if (incomingArgs.at(i) != QLatin1String(argv[i]))
            qDebug() << "ERROR: arguments[" << i << "] was" << incomingArgs.at(i) << "expected" << argv[i];
    }

    QCommandLineParser parser;
    parser.setApplicationDescription("Test helper");
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument("parsingMode", "The parsing mode to test.");
    parser.addPositionalArgument("command", "The command to execute.");
    parser.addOption(QCommandLineOption("load", "Load file from URL.", "url"));
    parser.addOption(QCommandLineOption(QStringList() << "o" << "output", "Set output file.", "file"));
    parser.addOption(QCommandLineOption("D", "Define macro.", "key=value"));

    // An option with a longer description, to test wrapping
    QCommandLineOption noImplicitIncludesOption(QStringList() << QStringLiteral("n") << QStringLiteral("no-implicit-includes"));
    noImplicitIncludesOption.setDescription(QStringLiteral("Disable magic generation of implicit #include-directives."));
    parser.addOption(noImplicitIncludesOption);

    QCommandLineOption newlineOption(QStringList() << QStringLiteral("newline"));
    newlineOption.setDescription(QString::fromLatin1("This is an option with a rather long\n"
                "description using explicit newline characters "
                "(but testing automatic wrapping too). In addition, "
                "here, we test breaking after a comma. Testing -option. "
                "Long URL: http://qt-project.org/wiki/How_to_create_a_library_with_Qt_and_use_it_in_an_application "
                "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
    parser.addOption(newlineOption);

    // A hidden option
    QCommandLineOption hiddenOption(QStringList() << QStringLiteral("hidden"));
    hiddenOption.setDescription(QStringLiteral("THIS SHOULD NEVER APPEAR"));
    hiddenOption.setFlags(QCommandLineOption::HiddenFromHelp);
    parser.addOption(hiddenOption);
    QCommandLineOption hiddenOption2(QStringList() << QStringLiteral("hidden2"));
    hiddenOption2.setDescription(QStringLiteral("NEITHER SHOULD THIS"));
    hiddenOption2.setHidden(true);
    parser.addOption(hiddenOption2);

    // This program supports different options depending on the "command" (first argument).
    // Call parse() to find out the positional arguments.
    parser.parse(QCoreApplication::arguments());

    QStringList args = parser.positionalArguments();
    if (args.isEmpty())
        parser.showHelp(1);
    parser.setSingleDashWordOptionMode(QCommandLineParser::SingleDashWordOptionMode(args.takeFirst().toInt()));
    const QString command = args.isEmpty() ? QString() : args.first();
    if (command == "resize") {
        parser.clearPositionalArguments();
        parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]");
        parser.addOption(QCommandLineOption("size", "New size.", "size"));
        parser.process(app);
        const QString size = parser.value("size");
        printf("Resizing %s to %s and saving to %s\n", qPrintable(parser.value("load")), qPrintable(size), qPrintable(parser.value("o")));
    } else {
        // Call process again, to handle unknown options this time.
        parser.process(app);
    }

    printf("Positional arguments: %s\n", qPrintable(parser.positionalArguments().join(",")));
    printf("Macros: %s\n", qPrintable(parser.values("D").join(",")));

    return 0;
}
int main(int argc, char **argv)
{

{
QCommandLineParser parser;
//! [0]
bool verbose = parser.isSet("verbose");
//! [0]
}

{
//! [1]
QCoreApplication app(argc, argv);
QCommandLineParser parser;
QCommandLineOption verboseOption("verbose");
parser.addOption(verboseOption);
parser.process(app);
bool verbose = parser.isSet(verboseOption);
//! [1]
}

{
QCommandLineParser parser;
//! [2]
// Usage: image-editor file
//
// Arguments:
//   file                  The file to open.
parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open."));

// Usage: web-browser [urls...]
//
// Arguments:
//   urls                URLs to open, optionally.
parser.addPositionalArgument("urls", QCoreApplication::translate("main", "URLs to open, optionally."), "[urls...]");

// Usage: cp source destination
//
// Arguments:
//   source                Source file to copy.
//   destination           Destination directory.
parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy."));
parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory."));
//! [2]
}

{
//! [3]
QCoreApplication app(argc, argv);
QCommandLineParser parser;

parser.addPositionalArgument("command", "The command to execute.");

// Call parse() to find out the positional arguments.
parser.parse(QCoreApplication::arguments());

const QStringList args = parser.positionalArguments();
const QString command = args.isEmpty() ? QString() : args.first();
if (command == "resize") {
    parser.clearPositionalArguments();
    parser.addPositionalArgument("resize", "Resize the object to a new size.", "resize [resize_options]");
    parser.addOption(QCommandLineOption("size", "New size.", "new_size"));
    parser.process(app);
    // ...
}

/*
This code results in context-dependent help:

$ tool --help
Usage: tool command

Arguments:
  command  The command to execute.

$ tool resize --help
Usage: tool resize [resize_options]

Options:
  --size <size>  New size.

Arguments:
  resize         Resize the object to a new size.
*/
//! [3]
}

{
//! [4]
QCommandLineParser parser;
parser.setApplicationDescription(QCoreApplication::translate("main", "The best application in the world"));
parser.addHelpOption();
//! [4]
}

}