class KCmdLineArgs


Table of contents
Modules
kdecore Classes
All Classes
Module kdecore
Namespace global
Class KCmdLineArgs
Inherits

A class for command-line argument handling.

KCmdLineArgs provides simple access to the command-line arguments for an application. It takes into account Qt-specific options, KDE-specific options and application specific options.

This class is used in %main() via the static method init().

A typical %KDE application using %KCmdLineArgs should look like this:

int main(int argc, char *argv[])
{
// Initialize command line args
KCmdLineArgs.init(argc, argv, appName, programName, version, description);

// Define the command line options using KCmdLineOptions KCmdLineOptions options; ....

// Register the supported options KCmdLineArgs.addCmdLineOptions( options );

// Add options from other components KUniqueApplication.addCmdLineOptions();

....

// Create application object without passing 'argc' and 'argv' again. KUniqueApplication app;

....

// Handle our own options/arguments // A KApplication will usually do this in main but this is not // necessary. // A KUniqueApplication might want to handle it in newInstance().

KCmdLineArgs *args = KCmdLineArgs.parsedArgs();

// A binary option (on / off) if (args->isSet("some-option")) ....

// An option which takes an additional argument QString anotherOptionArg = args->getOption("another-option");

// Arguments (e.g. files to open) for(int i = 0; i < args->count(); i++) // Counting start at 0! { openFile( args->arg(i)); // Or more convenient: // openUrl( args->url(i));

}

args->clear(); // Free up some memory. .... }

The options that an application supports are configured using the KCmdLineOptions class. An example is shown below:

KCmdLineOptions options;
options.add("a", ki18n("A short binary option"));
options.add("b \", ki18n("A short option which takes an argument"));
options.add("c \", ki18n("As above but with a default value"), "9600");
options.add("option1", ki18n("A long binary option, off by default"));
options.add("nooption2", ki18n("A long binary option, on by default"));
options.add(":", ki18n("Extra options:"));
options.add("option3 \", ki18n("A long option which takes an argument"));
options.add("option4 \", ki18n("A long option which takes an argument, defaulting to 9600"), "9600");
options.add("d");
options.add("option5", ki18n("A long option which has a short option as alias"));
options.add("e");
options.add("nooption6", ki18n("Another long option with an alias"));
options.add("f");



enums

enum details

methods