转义 TextMate 的 $DIALOG 命令的 Ruby 字符串
呼叫正则表达式专家。我现在在 Ruby 中转义字符串时遇到一些麻烦,以便我可以使用 exec
、%x{}
或类似工具将其传递到命令行实用程序。命令行命令是 TextMate 对话框功能,其基本工作原理如下(在 TextMate 中将其作为 shell 脚本运行。):
$DIALOG -mp "items=('string1','string2', 'string3');" RequestItem
但它也可以使用您指定的 nib 文件,例如“path/to/my/nib/file.nib” " 因此,我需要将整个命令作为字符串传递给 exec
command = <<string
$DIALOG -mp "items=('string1','string2', 'string3');" "path/to/my/nib/file.nib"
string
exec command
这自然可以正常工作。我遇到的问题是 items 数组的组成部分是通过编程方式确定的,因此我需要以编程方式转义该字符串,以便将其全部正确传递给 $DIALOG 命令。
到目前为止,我已经确定需要转义单引号、双引号、换行符和制表符。我已经尝试了大量潜在的正则表达式解决方案,但尚未找到可靠的转义机制。基本上,我需要填写
class String
def escape_for_dialog
self
end
end
正确的 self.gsub 调用,这些调用将转义我放入参数中的字符串。因此,让我们假设我的参数在运行时查找,这是我认为我需要使用上面相同的示例执行的操作:
command = <<string
$DIALOG -mp "items=('#{item1.escape_for_dialog}', '#{item2.escape_for_dialog}', '#{item3.escape_for_dialog}');" "path/to/my/nib/file.nib"
string
exec command
我发现heredoc样式有助于不必记住转义所有引号,但参数是仍然有问题,所以我要求有人在字符串操作和正则表达式方面比我更好,以帮助填写该字符串方法。我将不胜感激! :)
Calling regex gurus. I'm having some trouble right now with escaping a string in Ruby so that I can pass it into a command line utility using exec
, %x{}
or similar. The command line command is the TextMate dialog feature, which basically works like this (Run this as a shell script in TextMate.):
$DIALOG -mp "items=('string1','string2', 'string3');" RequestItem
but it can also use a nib file you specify like "path/to/my/nib/file.nib" So, I need to pass that whole command as a string to exec
command = <<string
$DIALOG -mp "items=('string1','string2', 'string3');" "path/to/my/nib/file.nib"
string
exec command
This works fine naturally. The problem that I am running into is that the components of the items array are determined programmatically, so I need to programmatically escape this string in a way that will pass it all to the $DIALOG
command properly.
So far, I have determined a need to escape single quotes, double quotes, and newline and tab characters. I've tried a ton of potential regular expression solutions but haven't yet been able to land on a reliable escaping mechanism. Basically, I need to fill in
class String
def escape_for_dialog
self
end
end
with the right self.gsub
calls that will escape the strings that I am putting into the parameters. So, let's assume my params are looked up at runtime, here's what I think I need to do using the same example above:
command = <<string
$DIALOG -mp "items=('#{item1.escape_for_dialog}', '#{item2.escape_for_dialog}', '#{item3.escape_for_dialog}');" "path/to/my/nib/file.nib"
string
exec command
I have figured out that the heredoc style helps with not having to remember to escape all the quotes, but the parameters are still problematic so I'm asking for someone better with string manipulation and regular expressions than I am to lend a hand at filling in that string method. I'll be most grateful! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
String.inspect 将转义分隔符 &等等,以一种让 shell 高兴的方式:
String.inspect will escape delimiters & etc. in a way that makes shell happy:
遗憾的是,这个问题的答案比看起来简单得多:TextMate 有一个 Ruby 方法!
TextMate.app/Contents/SharedSupport/Support/lib#e_sh
Sadly, the answer to this is much simpler than it would seem: TextMate has a Ruby method for it!
TextMate.app/Contents/SharedSupport/Support/lib#e_sh
您可以使用“escape”库进行 shell 转义,或者使用
exec
并向其传递数组 - 如果这样做,exec
将使用 SafeStringValue( ) 宏You can use the "escape" library to do shell escaping, or alternatively use
exec
and pass arrays to it - if done so,exec
will auto-escape using the SafeStringValue() macro