我经常发现自己这样做:
optparse = OptionParser.new do |opts|
options[:directory] = "/tmp/"
opts.on('-d','--dir DIR', String, 'Directory to put the output in.') do |x|
raise "No such directory" unless File.directory?(x)
options[:directory] = x
end
end
Run Code Online (Sandbox Code Playgroud)
如果我可以指定Dir或Pathname代替,那会更好String.是否有模式或我的Ruby式方式这样做?
您可以将OptionParser配置为接受(例如)路径名
require 'optparse'
require 'pathname'
OptionParser.accept(Pathname) do |pn|
begin
Pathname.new(pn) if pn
# code to verify existence
rescue ArgumentError
raise OptionParser::InvalidArgument, s
end
end
Run Code Online (Sandbox Code Playgroud)
然后,您可以将代码更改为
opts.on('-d','--dir DIR',Pathname, 'Directory to put the output in.') do |x|
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
678 次 |
| 最近记录: |