summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreban <eban@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-03-07 01:26:36 +0000
committereban <eban@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-03-07 01:26:36 +0000
commit681fcb37acfec3e93dc514f16ed81bccf6929207 (patch)
tree45b3536c0d9da9978a3da43b1bedce2c07aba3dd
parentcde930a4a4005da1d8e1a1ced719433fd07e3d3f (diff)
* lib/un.rb: should use OptionParser. (backported form HEAD)
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@8097 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog4
-rw-r--r--lib/un.rb52
2 files changed, 34 insertions, 22 deletions
diff --git a/ChangeLog b/ChangeLog
index 533a21666c5..f7555fbd49d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+Mon Mar 7 10:22:06 2005 WATANABE Hirofumi <eban@ruby-lang.org>
+
+ * lib/un.rb: should use OptionParser. (backported form HEAD)
+
Mon Mar 7 09:18:42 2005 Yukihiro Matsumoto <matz@ruby-lang.org>
* string.c (rb_str_cmp_m): should not return false but nil.
diff --git a/lib/un.rb b/lib/un.rb
index c665f659b1e..9bf6c13146a 100644
--- a/lib/un.rb
+++ b/lib/un.rb
@@ -21,20 +21,19 @@
# ruby -run -e touch -- [OPTION] FILE
# ruby -run -e help [COMMAND]
-require 'fileutils'
-require 'getopts'
+require "fileutils"
+require "optparse"
module FileUtils
-# @fileutils_label = ''
+# @fileutils_label = ""
@fileutils_output = $stdout
end
def setup(options = "")
- options += "v"
ARGV.map! do |x|
case x
when /^-/
- x.delete "^-#{options}"
+ x.delete "^-#{options}v"
when /[*?\[{]/
Dir[x]
else
@@ -42,13 +41,18 @@ def setup(options = "")
end
end
ARGV.flatten!
- ARGV.delete_if{|x| x == '-'}
- getopts(options)
- options = {}
- options[:verbose] = true if $OPT["v"]
- options[:force] = true if $OPT["f"]
- options[:preserve] = true if $OPT["p"]
- yield ARGV, options, $OPT
+ ARGV.delete_if{|x| x == "-"}
+ opt_hash = {}
+ OptionParser.new do |o|
+ options.scan(/.:?/) do |s|
+ o.on("-" + s.tr(":", " ")) do |val|
+ opt_hash[s.delete(":").intern] = val
+ end
+ end
+ o.on("-v") do opt_hash[:verbose] = true end
+ o.parse!
+ end
+ yield ARGV, opt_hash
end
##
@@ -62,9 +66,10 @@ end
#
def cp
- setup("pr") do |argv, options, opt|
+ setup("pr") do |argv, options|
cmd = "cp"
- cmd += "_r" if opt["r"]
+ cmd += "_r" if options.delete :r
+ options[:preserve] = true if options.delete :p
dest = argv.pop
argv = argv[0] if argv.size == 1
FileUtils.send cmd, argv, dest, options
@@ -82,9 +87,10 @@ end
#
def ln
- setup("sf") do |argv, options, opt|
+ setup("sf") do |argv, options|
cmd = "ln"
- cmd += "_s" if opt["s"]
+ cmd += "_s" if options.delete :s
+ options[:force] = true if options.delete :f
dest = argv.pop
argv = argv[0] if argv.size == 1
FileUtils.send cmd, argv, dest, options
@@ -118,9 +124,10 @@ end
#
def rm
- setup("fr") do |argv, options, opt|
+ setup("fr") do |argv, options|
cmd = "rm"
- cmd += "_r" if opt["r"]
+ cmd += "_r" if options.delete :r
+ options[:force] = true if options.delete :f
FileUtils.send cmd, argv, options
end
end
@@ -135,9 +142,9 @@ end
#
def mkdir
- setup("p") do |argv, options, opt|
+ setup("p") do |argv, options|
cmd = "mkdir"
- cmd += "_p" if options.delete :preserve
+ cmd += "_p" if options.delete :p
FileUtils.send cmd, argv, options
end
end
@@ -168,8 +175,9 @@ end
#
def install
- setup("pm:") do |argv, options, opt|
- options[:mode] = opt["m"] ? opt["m"].oct : 0755
+ setup("pm:") do |argv, options|
+ options[:mode] = (mode = options.delete :m) ? mode.oct : 0755
+ options[:preserve] = true if options.delete :p
dest = argv.pop
argv = argv[0] if argv.size == 1
FileUtils.install argv, dest, options