genie

Exit with error code from init block in Genie


In Vala, I can write the following code:

int main(string[] args) {
    if (args[1] == "secret") {
        return 0;
    } else {
        return 1;
    }
}

How would I do the same in Genie? The following does not work:

init
    if args[1] == "secret"
        return 0
    else
        return 1

… because returning a value from a void block is not allowed.


Solution

  • At present this is not possible using the init method. See Bug 707233 - Allow exit status to be set from init function.

    This can be achieved with GLib's Process.exit() call:

    [indent=4]
    init
        if args[ 1 ] == "secret"
            Process.exit( 0 )
        else
            Process.exit( 1 )
    

    Alternatively, if you are working in a Posix environment, compile the following with valac --pkg posix my_exit_example.gs:

    [indent=4]
    init
        if args[ 1 ] == "secret"
            Process.exit( Posix.EXIT_SUCCESS )
        else
            Process.exit( Posix.EXIT_FAILURE )
    

    If you would like to add this to the Genie parser then look at the parse_main_method_declaration() method in the vala/valagenieparser.vala source file. The syntax would have to be something like:

    [indent=4]
    init:int
        if args[ 1 ] == "secret"
            return 0
        else
            return 1