wc.rb

情けないことにいっつもいっつもwcの出力の見方を忘れてhelpを見てるので(つまりそんなに頻繁に使わない)、

require 'optparse'
require 'shellwords'

echo       = true
targets    = []
add_target = lambda{|ident| lambda{ targets << ident } }
no_echo    = lambda{ echo = false }

OptionParser.new.instance_eval{
  on '-c', '--bytes',
           '--chars',           &add_target[:bytes]
  on '-l', '--lines',           &add_target[:lines]
  on '-L', '--max-line-length', &no_echo
  on '-w', '--words',           &add_target[:words]
  on       '--help',            &no_echo
  on       '--version',         &no_echo
  self
}.parse ARGV

targets.uniq!
order = [:lines, :words, :bytes]
targets = order.select{|e| targets.include?(e) }
targets = order if targets.empty?

puts targets.map{|s| s.id2name.rjust(7) }.join(' ') if echo
system "wc #{ARGV.shelljoin}"

こんな対策をしてみた。RUBY_VERSION >= '1.8.7'で。