How to parse rake arguments with OptionParser

前端 未结 4 505
情书的邮戳
情书的邮戳 2021-01-18 00:05

Reffering that answer I was trying to use OptionParser to parse rake arguments. I simplified example from there and I had to add two ARGV.shi

4条回答
  •  时光说笑
    2021-01-18 00:39

    You can use the method OptionParser#order! which returns ARGV without the wrong arguments:

    options = {}
    
    o = OptionParser.new
    
    o.banner = "Usage: rake user:create [options]"
    o.on("-u NAME", "--user NAME") { |username|
      options[:user] = username
    }
    args = o.order!(ARGV) {}
    o.parse!(args)
    puts "user: #{options[:user]}"
    

    You can pass args like that: $ rake foo:bar -- '--user=john'

提交回复
热议问题