1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Separated VCS.define_options for common VCS options

This commit is contained in:
Nobuyoshi Nakada 2019-09-07 00:36:08 +09:00
parent 4068be1d9f
commit 799de9122e
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60

View file

@ -131,14 +131,15 @@ class VCS
@@dirs << [dir, self, pred] @@dirs << [dir, self, pred]
end end
def self.detect(path = '.', options = {}, argv = ::ARGV) def self.detect(path = '.', options = {}, parser = nil)
uplevel_limit = options.fetch(:uplevel_limit, 0) uplevel_limit = options.fetch(:uplevel_limit, 0)
curr = path curr = path
begin begin
@@dirs.each do |dir, klass, pred| @@dirs.each do |dir, klass, pred|
if pred ? pred[curr, dir] : File.directory?(File.join(curr, dir)) if pred ? pred[curr, dir] : File.directory?(File.join(curr, dir))
vcs = klass.new(curr) vcs = klass.new(curr)
vcs.parse_options(argv) vcs.define_options(parser) if parser
vcs.set_options(options)
return vcs return vcs
end end
end end
@ -155,6 +156,13 @@ class VCS
String === path or path.respond_to?(:to_path) String === path or path.respond_to?(:to_path)
end end
def self.define_options(parser, opts = {})
parser.separator(" VCS common options:")
parser.define("--[no-]dryrun") {|v| opts[:dryrun] = v}
parser.define("--[no-]debug") {|v| opts[:debug] = v}
opts
end
attr_reader :srcdir attr_reader :srcdir
def initialize(path) def initialize(path)
@ -166,21 +174,12 @@ class VCS
@srcdir = path @srcdir = path
end end
def parse_options(opts, parser = OptionParser.new) def define_options(parser)
case opts end
when Array
parser.on("--[no-]dryrun") {|v| @dryrun = v} def set_options(opts)
parser.on("--[no-]debug") {|v| @debug = v} @debug = opts.fetch(:debug) {$DEBUG}
parser.parse(opts) @dryrun = opts.fetch(:dryrun) {@debug}
@debug = $DEBUG unless defined?(@debug)
@dryrun = @debug unless defined?(@dryrun)
when Hash
unless (keys = opts.keys - [:debug, :dryrun]).empty?
raise "Unknown options: #{keys.join(', ')}"
end
@debug = opts.fetch(:debug) {$DEBUG}
@dryrun = opts.fetch(:dryrun) {@debug}
end
end end
attr_reader :dryrun, :debug attr_reader :dryrun, :debug