⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 02 - parsing command-line arguments.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
#!/usr/bin/ruby -w# cat.rbARGV.each { |filename| IO.readlines(filename).each { |line| puts line } }#---#!/usr/bin/ruby -w# cat_argf.rbARGF.each { |line| puts line }#---#!/usr/bin/ruby# cat2.rbrequire 'optparse'class CatArguments < Hash  def initialize(args)    super()    self[:show_ends] = ''    opts = OptionParser.new do |opts|      opts.banner = "Usage: #$0 [options]"      opts.on('-E', '--show-ends [STRING]',               'display [STRING] at end of each line') do |string|        self[:show_ends] = string || '$'      end      opts.on('-n', '--number', 'number all output lines') do        self[:number_lines] = true      end            opts.on_tail('-h', '--help', 'display this help and exit') do        puts opts        exit      end    end    opts.parse!(args)     end  endarguments = CatArguments.new(ARGV)#---counter = 0eol = ARGF.each do |line|    line.sub!(/$/, arguments[:show_ends])  print '%6.d  ' % (counter += 1) if arguments[:number_lines]  print lineend#---$ ./cat2.rb --helpUsage: ./cat2.rb [options]    -E, --show-ends [STRING]         display STRING at end of each line    -n, --number                     number all output lines    -h, --help                       display this help and exit$ ./cat2.rb file1 file2This is file one.Another line in file one.This is file two.I'm a lot more interesting than file one, I'll tell you that!$ ./cat2.rb file1 -E$ -n file2     1  This is file one.$     2  Another line in file one.$     3  This is file two.$     4  I'm a lot more interesting than file one, I'll tell you that!$$ ./cat2.rb --nosuchargument/usr/lib/ruby/1.8/optparse.rb:1445:in `complete': invalid option: --nosuchargument (OptionParser::InvalidOption)$ ./cat2.rb --show-ends=" STOP" -- --argument-looking-fileThe name of this file STOPlooks just like an argument STOPfor some odd reason. STOP#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -