【问题标题】:How to call GNU ReadLine in Genie如何在 Genie 中调用 GNU ReadLine
【发布时间】:2015-10-05 10:05:35
【问题描述】:

我知道 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)

我做错了什么?

谢谢。

【问题讨论】:

  • 这是Readline 而不是ReadLine(注意大小写的区别)。
  • 顺便说一句:你为​​什么不写你自己的raw_input函数?

标签: python vala genie


【解决方案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 -l<em>library_i_am_using</em> 应该是个例外,因为大多数库都有一个.pc 文件。

我还使用while..break 进行无限循环,看看您是否认为这是一种更清晰的样式。

【讨论】:

  • 感谢您的全部回复,感谢 while..break 循环。这是对代码的增强。 AIThomas,知道何时在代码开头添加 uses 命令(类似于 import)以及如何确定我的代码是否需要 --pkg 参数才能正确编译的最佳方法是什么?为什么在这个例子中我不必添加类似的东西:使用 GNUReadline?
  • 'uses' 不是导入的,因为它只是使用命名空间。您可以使用“uses Readline”重写上述代码,然后将“Readline.readline”更改为“readline”,因为您现在已将所有内容都包含在 Readline 命名空间中。节省了一点打字。如果你不使用'uses',那么你只需要在所有东西前面加上绑定的命名空间。 --pkg 是“导入”,您需要在所有情况下使用它。 Genie 设置为自动使用 --pkg glib-2.0 和 --pkg gobject-2.0。将 --verbose 与 valac 结合使用以查看实际情况。
  • 我也在文档中写了这个 - 见wiki.gnome.org/Projects/Genie#Using_a_C_Library
猜你喜欢
  • 2023-03-24
  • 2017-05-04
  • 1970-01-01
  • 2010-12-03
  • 2011-11-01
  • 1970-01-01
  • 2012-07-14
  • 2010-11-06
  • 2023-04-06
相关资源
最近更新 更多