将带引号的参数传递给 REBOL 3 脚本
我发现几乎不可能将带引号的参数(包含空格)传递给 REBOL 3 脚本。例如:
rebol -q script.r "foo bar" 40
如果您检查system/script/args
,它包含字符串“foo bar 40”
。这是没用的!信息丢失了。我需要知道 "foo bar"
是第一个参数,40
是第二个参数。如果我检查system/options/args
,我会得到以下块:["foo" "bar" "40"]
。再说一遍,没用!信息丢失了。
我怀疑解决这个问题的方法是使用某种参数分隔符,例如,
rebol -q script.r 'foo bar' -n 40
这可以很容易地通过 PARSE 处理,但我仍然不喜欢它。对于 system/options/args
来说,每个传递的参数包含一个字符串应该不是一件非常困难的事情。
REBOL 使用起来很愉快,这是我发现的第一个让我真正失望的东西。 :(
I've found it almost impossible to pass quoted arguments (containing spaces) to REBOL 3 scripts. For example:
rebol -q script.r "foo bar" 40
If you examine system/script/args
, it contains the string "foo bar 40"
. This is useless! Information was lost. I need to know that "foo bar"
was the first argument and 40
was the second. If I examine system/options/args
, I get the following block: ["foo" "bar" "40"]
. Again, useless! Information was lost.
I suspect that the solution to this is to use argument delimiters of some kind, e.g.,
rebol -q script.r 'foo bar' -n 40
This could easily be handled by PARSE
, but I still don't like it. It shouldn't be terribly difficult for system/options/args
to contain one string per passed argument.
REBOL's a pleasure to use, and this is the first thing I've found with which I was really disappointed. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 REBOL 3 中,您观察到的行为是一个已知错误。
(目前,R3 在内部将 args 作为单个字符串从操作系统传递到脚本,连接该过程中的所有原始参数。目前此过程不完全可逆,这就是此错误的原因。R3 可能应该将参数作为字符串列表,有效地保留了原始的 argv ,但删除了解释器本身使用的参数。)
在 REBOL 2 中,使用 system/options/args 更安全命令行参数,而
system/script/args
可用于更直接地在 REBOL 脚本之间传递值。我假设 R3 也会保留类似的行为。这是一个检查参数解析行为的快速脚本:
REBOL 2,在 OSX 上:
REBOL 3,在 OSX 上:
In REBOL 3, the behaviour you observe is a known bug.
(At the moment, R3 internally passes args from the OS to scripts as a single string, concatenating all original arguments in the process. Currently this process is not fully reversible, which is the cause of this bug. R3 probably should_ pass arguments as a list of strings instead, effectively preserving the original
argv
but stripped of arguments used by the interpreter itself.)In REBOL 2,
system/options/args
is safer to use for command-line arguments, whereassystem/script/args
can be used to pass values between REBOL scripts more directly. I assume that similar behaviour will be kept for R3.Here's a quick script to inspect argument parsing behaviour:
REBOL 2, on OSX:
REBOL 3, on OSX:
你可以转义引号:
不知道这是shell还是REBOL的缺点?
You can escape the quotes:
Don't know if this is a shortcoming of shell or REBOL?