Application

Extensible class to do all the common task needed for an application to run.

This class also implements its own extension interface, so it's easy to write an application using the hooks in extensions without having to write a separate extension. The order of this extension is 0, establishing a reference order for all the other extensions. Extensions that should be executed before the application hooks should have a negative order, while extensions that have to be executed after the application hooks should have a positive order value.

The common usage is to derive from Application and override the run() method. Optionally you can override the extensions methods too.

Constructors

this
this(istring name, istring desc)

Constructor.

Members

Aliases

Application
alias Application = .Application

Alias of Application, for use by sub-classes without needing to import ocean.util.app.Application.

Functions

atExit
void atExit(IApplication app, istring[] args, int status, ExitException exception)
Undocumented in source. Be warned that the author may not have intended to support it.
exit
void exit(int status, istring msg)

Exit cleanly from the application.

main
int main(istring[] args)

Runs the application.

name
istring name()
onExitException
ExitException onExitException(IApplication app, istring[] args, ExitException exception)
Undocumented in source. Be warned that the author may not have intended to support it.
order
int order()

Default application extension order.

postRun
void postRun(IApplication app, istring[] args, int status)
Undocumented in source. Be warned that the author may not have intended to support it.
preRun
void preRun(IApplication app, istring[] args)

IApplicationExtension methods dummy implementation.

printExitException
void printExitException(ExitException e)

Prints the message in an ExitException.

run
int run(istring[] args)

Do the actual application work.

Mixins

__anonymous
mixin ExtensibleClassMixin!(IApplicationExtension)

Adds a list of extensions (this.extensions) and methods to handle them. See ExtensibleClassMixin documentation for details.

Variables

args
istring[] args;

Command line arguments passed to the application.

desc
istring desc;

Short description of the application.

status
int status;

Application exit status code.

Inherited Members

From IApplication

name
istring name()
exit
void exit(int status, istring msg)

Exit cleanly from the application.

Examples

import ocean.util.app.Application;
import ocean.io.Stdout;

class MyApp : Application
{
    this ( )
    {
        super("myapp", "A test application");
    }
    protected override void preRun ( Application app, istring[] args )
    {
        if ( args.length < 4 )
        {
            this.exit(1, "Too few arguments");
        }
    }
    protected override int run ( istring[] args )
    {
        Stdout.formatln("Application is running!");

        return 0;
    }
    protected override void postRun ( Application app, istring[] args,
            int status )
    {
        Stdout.formatln("Application returned {}", status);

    }
}

int main(istring[] args)
{
    auto app = new MyApp;
    return app.main(args);
}

As seen in the example, this class also provides a clean way to exit an application from anywhere in the program, the exit() method.

The main() method is the real only needed public API, and the one calling all the extension code. The real application call must be in the run() method, which is abstract, and mandatory to implement.

The full power and usefulness of this class comes from extensions though.

Meta