如何在Geni中调用GNU ReadLine

2024-10-01 05:06:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道标准读取线()函数,但我希望通过使用python中的raw_input()或更符合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)

我做错什么了?你知道吗

谢谢。你知道吗


Tags: ortoloopgsreadlineresponsevarerror
1条回答
网友
1楼 · 发布于 2024-10-01 05:06:20

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

[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用于无限循环,以查看您是否认为这是一种稍微清晰的样式。你知道吗

相关问题 更多 >