从 Genie 中的 init 块退出并显示错误代码
Exit with error code from init block in Genie
在Vala中,我可以编写如下代码:
int main(string[] args) {
if (args[1] == "secret") {
return 0;
} else {
return 1;
}
}
我如何在精灵中做同样的事情?以下不起作用:
init
if args[1] == "secret"
return 0
else
return 1
... 因为不允许从 void 块返回值。
目前无法使用 init
方法。参见 Bug 707233 - Allow exit status to be set from init function。
这可以通过 GLib 的 Process.exit() 调用来实现:
[indent=4]
init
if args[ 1 ] == "secret"
Process.exit( 0 )
else
Process.exit( 1 )
或者,如果您在 Posix 环境中工作,请使用 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 )
如果您想将其添加到 Genie 解析器,请查看 vala/valagenieparser.vala 源文件中的 parse_main_method_declaration()
方法。语法必须类似于:
[indent=4]
init:int
if args[ 1 ] == "secret"
return 0
else
return 1
在Vala中,我可以编写如下代码:
int main(string[] args) {
if (args[1] == "secret") {
return 0;
} else {
return 1;
}
}
我如何在精灵中做同样的事情?以下不起作用:
init
if args[1] == "secret"
return 0
else
return 1
... 因为不允许从 void 块返回值。
目前无法使用 init
方法。参见 Bug 707233 - Allow exit status to be set from init function。
这可以通过 GLib 的 Process.exit() 调用来实现:
[indent=4]
init
if args[ 1 ] == "secret"
Process.exit( 0 )
else
Process.exit( 1 )
或者,如果您在 Posix 环境中工作,请使用 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 )
如果您想将其添加到 Genie 解析器,请查看 vala/valagenieparser.vala 源文件中的 parse_main_method_declaration()
方法。语法必须类似于:
[indent=4]
init:int
if args[ 1 ] == "secret"
return 0
else
return 1