1
像许多命令行实用程序一样,我如何在Ruby中解析字符串?我有类似于"command [--opt1=...] [--enable-opt2] --opt3=... arg1"
的字符串以及类似于command(opt1,opt2,opt3,arg1...)
的方法。我想让参数随机出现,其中一些可以是可选的。解析命令行的Ruby正则表达式
此刻,我正则表达式我需要解析新的命令,例如 解析每次wrilte“lastpost --chan = your_CHANNEL /节/”
我有这样的正则表达式:
text = "lastpost --chan=0chan.ru /s/"
command = (text.match /^\w+/)[0]
args = text.gsub(/^\w+/,'')
if args =~ /[[:blank:]]*(--chan\=([[:graph:]]+)[[:blank:]]+)*\/?(\w+)\/?/
chan = $2
section = $3
do_command(chan,section)
else
puts "wrong args"
end
我希望我有create_regexp(opts,args),它应该产生正则表达式。
如果您有(子)命令,检查了'commander'宝石。 – 2010-06-02 16:54:50