forked from grosser/pru
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backwards-incompatible changes: - '-r' changed to '-e' - map defaults to 'true' - stop messing with $LOAD_PATH
- Loading branch information
1 parent
3eef096
commit 40216a7
Showing
3 changed files
with
48 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,44 @@ | ||
#!/usr/bin/env ruby | ||
|
||
autoload :Pru, 'pru' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
blackwinter
Author
Member
|
||
require 'optparse' | ||
|
||
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') | ||
require 'pru' | ||
|
||
options = {} | ||
|
||
OptionParser.new { |opts| | ||
opts.banner = <<-EOS | ||
usage = <<-EOS | ||
Pipeable Ruby | ||
Use ruby in your pipes, forget about grep/sed/awk/wc... | ||
Map works on each line as String. | ||
Reduce works on all lines as Array (optional or via -r). | ||
Reduce works on all lines as Array (optional or via -e). | ||
Usage: | ||
something | pru 'map' ['reduce'] | ||
something | pru -r 'reduce' | ||
EOS | ||
something | pru -e 'reduce' | ||
EOS | ||
|
||
options = {} | ||
|
||
OptionParser.new { |opts| | ||
opts.banner = usage | ||
|
||
opts.separator '' | ||
opts.separator 'Options:' | ||
|
||
opts.on('-r', '--reduce CODE', 'Reduce via CODE') { |code| | ||
opts.on('-e', '--execute CODE', 'Reduce via CODE') { |code| | ||
options[:reduce] = code | ||
} | ||
|
||
opts.separator '' | ||
opts.separator 'Load options:' | ||
|
||
opts.on('-I', '--libdir LIBDIR', 'Include LIBDIR in the search path for required modules') { |dir| | ||
$LOAD_PATH << dir | ||
} | ||
|
||
opts.on('-r', '--require MODULE', 'Require MODULE before executing any code') { |mod| | ||
require mod | ||
} | ||
|
||
opts.separator '' | ||
opts.separator 'Generic options:' | ||
|
||
|
@@ -42,16 +53,18 @@ Usage: | |
} | ||
}.parse! | ||
|
||
abort usage if ARGV.size > 2 | ||
map, reduce = ARGV | ||
|
||
map ||= options[:map] | ||
reduce ||= options[:reduce] | ||
map = nil if map and map.empty? | ||
|
||
if map and not reduce | ||
Pru.map($stdin, map) { |x| puts x } | ||
elsif map and reduce | ||
map = 'true' if map.nil? || map.strip.empty? | ||
|
||
if reduce | ||
results = [] | ||
Pru.map($stdin, map) { |x| results << x } | ||
puts Pru.reduce(results, reduce) | ||
elsif reduce | ||
puts Pru.reduce($stdin.read.split($/), reduce) | ||
else | ||
Pru.map($stdin, map) { |x| puts x } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
LIB_DIR = File.expand_path('../../lib', __FILE__) | ||
PRU_CMD = "#{File.join(File.dirname(LIB_DIR), 'bin', 'pru')} -I#{LIB_DIR}" | ||
|
||
$LOAD_PATH.unshift LIB_DIR | ||
require 'pru' |
if you use ./bin/pru the installed rubygem would be used (insttead of the one in lib) ?