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