【问题标题】:How to get values from the Red/System parts in a Red file如何从红色文件中的红色/系统部分获取值
【发布时间】:2014-11-10 16:46:25
【问题描述】:

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

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

【问题讨论】:

    标签: red


    【解决方案1】:

    这样的东西应该可以工作,只需将您的#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"]
    

    【讨论】:

      【解决方案2】:

      假设 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。

      【讨论】: