如何在 Genie 中调用 GNU ReadLine

How to call GNU ReadLine in Genie

我知道 stdin.read_line() 函数,但我想通过使用或更符合 python 中的 raw_input() 来使我的代码不那么冗长。

所以我在 this 关于 vala 的讨论中发现了 GNU ReadLine,但是我无法在 Genie 中重现它。

我要模仿的python代码是:

loop = 1
while loop == 1:
    response = raw_input("Enter something or 'quit' to end => ")
    if response == 'quit':
        print 'quitting'
        loop = 0
    else:
        print 'You typed %s' % response

我能到达的距离是:

[indent=4]

init
    var loop = 1
    while loop == 1
        // print "Enter something or 'quit' to end => "
        var response = ReadLine.read_line("Enter something or 'quit' to end => ")
        if response == "quit"
            print "quitting"
            loop = 0
        else 
            print "You typed %s", response

并尝试编译:

valac --pkg readline -X -lreadline loopwenquiry.gs 

但我收到错误消息:

loopwenquiry.gs:7.24-7.31: error: The name `ReadLine' does not exist in the context of `main'
        var response = ReadLine.read_line("Enter something or 'quit' to end => ")
                       ^^^^^^^^
loopwenquiry.gs:7.22-7.81: error: var declaration not allowed with non-typed initializer
        var response = ReadLine.read_line("Enter something or 'quit' to end => ")
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
loopwenquiry.gs:8.12-8.19: error: The name `response' does not exist in the context of `main'
        if response == "quit"
           ^^^^^^^^
loopwenquiry.gs:12.35-12.42: error: The name `response' does not exist in the context of `main'
            print "You typed %s", response
                                  ^^^^^^^^
Compilation failed: 4 error(s), 0 warning(s)

我做错了什么?

谢谢。

正如 Jens 的评论中所述,命名空间是 Readline,而不是 ReadLine。函数也是readline,不是read_line。所以你的工作代码是:

[indent=4]
init
    while true     
        response:string = Readline.readline("Enter something or 'quit' to end => ")
        if response == "quit"
            print "quitting"
            break
        else
            print "You typed %s", response

我注意到你使用 valac --pkg readline -X -lreadline loopwenquiry.gs 编译,这很好。 -X -lreadline 告诉 linker to use the readline library. In most cases you won't need to do this because there is a pkg-config file, these have a .pc file extension, that contains all the necessary information. It looks as though someone has submitted a patch 将此修复到 readline 库。所以使用 -X -l<em>library_i_am_using</em> 应该是例外,因为大多数库都有一个 .pc 文件。

我也用过while..break无限循环,看看你是否认为这是一种更清晰的风格。