如何在Genie中调用GNU ReadLine

时间:2015-10-05 10:05:36

标签: python vala genie

我知道stdin.read_line()函数,但是我希望通过使用或者更符合python中raw_input()的内容来使我的代码更简洁。

所以我在关于vala的this讨论中发现了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)

我做错了什么?

感谢。

1 个答案:

答案 0 :(得分:1)

正如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使用readline库。在大多数情况下,您不需要这样做,因为有一个pkg-config文件,这些文件具有.pc文件扩展名,其中包含所有必要信息。看起来好像有人submitted a patch将其修复到readline库。因此,使用-X -llibrary_i_am_using应该是例外,因为大多数库都有.pc文件。

我还使用while..break作为无限循环,看看你是否认为这是一种稍微清晰的风格。