Skip to content

Commit

Permalink
show usage when no arguments are given
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Aug 27, 2011
1 parent 312fef1 commit 6ea5460
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 11 additions & 2 deletions bin/pru
Expand Up @@ -4,7 +4,9 @@ require 'optparse'
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'pru'

usage = nil
options = {}

OptionParser.new do |opts|
opts.banner = <<BANNER
Pipeable Ruby
Expand Down Expand Up @@ -35,17 +37,24 @@ BANNER
opts.separator ''
opts.on("-h", "--help","Show this.") { puts opts; exit }
opts.on('-v', '--version','Show Version'){ puts Pru::VERSION; exit}
usage = opts
end.parse!

if ARGV.empty? and options.empty? # no arguments -> show usage
puts usage
exit
end

abort "Too many arguments, see --help" if ARGV.size > 2

map, reduce = ARGV
reduce ||= options[:reduce]
map = 'true' if not map or map.empty?

if reduce
results = []
Pru.map($stdin, map){|x| results << x }
Pru.map($stdin, map){|usage| results << usage }
puts Pru.reduce(results, reduce)
else
Pru.map($stdin, map){|x| puts x }
Pru.map($stdin, map){|usage| puts usage }
end
4 changes: 4 additions & 0 deletions spec/pru_spec.rb
Expand Up @@ -5,6 +5,10 @@
Pru::VERSION.should =~ /^\d+\.\d+\.\d+$/
end

it "shows help when no arguments are given" do
`./bin/pru`.should include('Usage:')
end

describe 'map' do
it "selects" do
`ls -l | ./bin/pru 'include?("G")'`.split("\n").size.should == 2
Expand Down

0 comments on commit 6ea5460

Please sign in to comment.