注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

星期五

明天不上班

 
 
 

日志

 
 
关于我

一个特立独行的Java程序员,比较宅,上上网,写博客,听音乐,看电影。

网易考拉推荐

Ruby之optparse  

2012-02-14 20:22:26|  分类: Ruby |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
用Ruby写命令行工具
这里主要用到 optparse
关于它的详细讲解 http://ruby-doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html

这里举例说明:关于ls.rb

#!/usr/bin/ruby -w
#author @hjzheng

require 'optparse'
require 'ostruct'


class OptparseLs


def self.parse(args)
options = OpenStruct.new
options.version = "Ruby ls 0.1"
options.list = ["."]
options.list_flag = 0
options.recursion = 0
options.args = args

opts = OptionParser.new do |opts|
opts.banner = "Usage: ls.rb [options]"

opts.separator ""
opts.separator "Specific options:"

#ls -l
opts.on("-l", "Use a long listing format") do
options.list_flag = 1
if args.length > 0 then
args.delete("-l")
options.list = args
end
end

#ls -r
opts.on("-r", "Recursion directory") do
options.recursion = 1
if args.length > 0 then
args.delete("-r")
options.list = args
end
end


#ls -l x y z
#opts.on("-l x y z", Array ,"Use a long listing format") do |list|
# list.each{|f|
# file_state = File.stat(f)
# mode = "%o" % file_state.mode
# puts "#{file_state.ftype} #{mode} #{file_state.nlink} #{file_state.size} #{file_state.atime} #{f}"
# }
# exit
#end

opts.separator ""
opts.separator "Common options:"

# help
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end

# print the version.
opts.on_tail("-v", "--version", "Show version") do
puts infos.version
exit
end

end


opts.parse!(args)
displayDir(options)

end # parse()

def self.displayDir(options)
if options.recursion == 1 && options.list_flag == 0 then
options.list.each{|dir|
Dir.glob(dir+"/*/**").each{|f| puts f + " "}
}
elsif options.recursion == 0 && options.list_flag == 0 then
options.list = options.args.length == 0 ? options.list : options.args
options.list.each{|dir|
if File.ftype(dir) != "directory" then
Dir.glob(dir).each{|f| puts f + " "}
else
Dir.glob(dir+"/*").each{|f| puts f + " "}
end
}
elsif options.recursion == 1 && options.list_flag == 1 then
options.list.each{|dir|
Dir.glob(dir+"/*/**").each{|f|
displayListDir(f)
}
}
elsif options.recursion == 0 && options.list_flag == 1 then
options.list.each{|dir|
if File.ftype(dir) != "directory" then
Dir.glob(dir).each{ |f|
displayListDir(f)
}
else
Dir.glob(dir+"/*").each{ |f|
displayListDir(f)
}
end
}
end
end

def self.displayListDir(f)
file_state = File.stat(f)
mode = "%o" % file_state.mode
puts "#{file_state.ftype} #{mode} #{file_state.nlink} #{file_state.size} #{file_state.atime} #{f}"
end

end # class OptparseLs

OptparseLs.parse(ARGV)

使用:(optParse会自动生成组合的情况)
hjzheng@hjzheng-ThinkPad-T520 ~/ruby_script $ ./ls.rb -l
file 100755 1 339 Sun Feb 12 22:19:01 +0800 2012 ./list_dir.rb
file 100755 1 2946 Tue Feb 14 20:00:02 +0800 2012 ./ls.rb
file 100755 1 557 Sun Feb 12 21:23:42 +0800 2012 ./ruby_templete.rb

hjzheng@hjzheng-ThinkPad-T520 ~/ruby_script $ ./ls.rb -h
Usage: ls.rb [options]

Specific options:
    -l                               Use a long listing format
    -r                               Recursion directory

Common options:
    -h, --help                       Show this message
    -v, --version                    Show version

hjzheng@hjzheng-ThinkPad-T520 ~/ruby_script $ ./ls.rb -l -r /home/hjzheng
file 100664 1 110590 Thu Feb 09 22:17:44 +0800 2012 /home/hjzheng/下载/trw2src.zip
file 100664 1 29132038 Thu Feb 09 22:21:48 +0800 2012 /home/hjzheng/下载/google-chrome-stable_current_i386.deb
file 100664 1 1497600 Sun Feb 12 20:41:54 +0800 2012 /home/hjzheng/下载/bmsft-code.zip
file 100664 1 2983 Tue Feb 14 19:01:28 +0800 2012 /home/hjzheng/下载/ls.rb
file 100664 1 2920201 Sun Feb 12 19:14:57 +0800 2012 /home/hjzheng/下载/Everyday+Scripting+with+Ruby.pdf
file 100664 1 1672781 Sun Feb 12 19:18:14 +0800 2012 /home/hjzheng/下载/Addison+Wesley+The+Ruby+Way+2Nd+Edition+Oct+2006.pdf
file 100664 1 902410 Sun Feb 12 20:16:39 +0800 2012 /home/hjzheng/下载/the%252Bruby%252Bway.pdf
file 100664 1 110590 Thu Feb 09 22:23:12 +0800 2012 /home/hjzheng/PDF/trw2src.zip
file 100664 1 1672781 Thu Feb 09 22:22:02 +0800 2012 /home/hjzheng/PDF/Addison+Wesley+The+Ruby+Way+2Nd+Edition+Oct+2006.pdf
file 100664 1 29132038 Sat Feb 04 03:36:22 +0800 2012 /home/hjzheng/software/google-chrome-stable_current_i386.deb

参考资料:
  评论这张
 
阅读(1113)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017