2

我正在使用Red 绑定来读取和写入文件,并且硬编码文件名版本运行良好。但我想从命令行动态获取文件名。因为Red现在没有这样的实用程序。所以我试着用Red/System. 我现在可以获得命令行参数,但我不知道如何将它传递给Red部件。像下面的例子一样,我需要传递source-fileandtarget-filereadand write

Red []

#include %input-output.red

#system-global [
    args: system/args-list
    args: args + 1
    source-file: args/item
    args: args + 1
    target-file: args/item

    print [source-file target-file ]

]

data: read source-file
probe data
write target-file data
4

2 回答 2

2

像这样的东西应该可以工作,只需将您的#system-global 代码转换为例程函数:

Red [
    File: "test-read.red"
]

read-arg: routine [
    files [block!]
    /local
        str  [red-string!]
        args [str-array!]
][
    args: system/args-list
    args: args + 1

    str: string/load symbol/duplicate args/item  1 + length? args/item UTF-8
    block/rs-append files as red-value! str

    args: args + 1
    str: string/load symbol/duplicate args/item  1 + length? args/item UTF-8
    block/rs-append files as red-value! str
]

probe read-arg []

然后一旦编译,你应该得到这个结果:

C:\Dev\Red>test-read fileA fileB
["fileA" "fileB"]
于 2014-11-11T11:40:29.580 回答
1

假设 read 和 write 函数的参数是字符串!,您将需要编写一个返回红色字符串的例程!红色/系统 c 字符串!。

由于 Nenad 正忙于开发 Red 1.0,因此 Red API 尚未记录在案,这是可以理解的。这是一个相反的函数,字符串!到 c 字符串!这可能有助于了解需要什么 - https://github.com/PeterWAWood/Red-System-Libs/blob/master/UTF8/string-c-string.reds

可能有一种更简单的方法来使用其他人可以建议的 Red api。

于 2014-11-11T03:28:09.107 回答