pipewrench 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb3fe83f805ee02ecc16847b508fe05c4acd54e1
4
- data.tar.gz: bbc7fb1bf620f6b61fe814969310d526a6fba47d
3
+ metadata.gz: 2f2152e145d0f7941faa0049ffe67a330d45d0e1
4
+ data.tar.gz: c7121bf392717c30ebab9fadb44bc7293dfb0bf1
5
5
  SHA512:
6
- metadata.gz: 497ee217ed9c1101babba0749a4a0252a914cb2fbd1e71f1756faf233ecb240fe0cff4d13115fdcd47944683107cd01d6eb1a4c328cfc731b914834756a5994c
7
- data.tar.gz: 085218714ba6d0365c83c5b065efee4377ce5c198bd784eef263f660994ebaf0e60002da8e29ff41cd42372a4c316ed8e1d3b00c5454b81863988b89301826a1
6
+ metadata.gz: 5e7b7455d135f7e0bef841362d5f791812f48e75f4fd4e7660ac0083658cad202f76639ddafad9f0ce3ef51cf6f0b5ba767223343f3a179813f0088e7eab4d85
7
+ data.tar.gz: 929b38bd8f5145910e4323a3faf4d0f9441d2bf765b0896865b876233d3688fa7b10e37593b3fb2d6155a6f9783b31990356f6acb03b1c655a87018616592681
data/README.md CHANGED
@@ -8,7 +8,70 @@ General purpose command line pipe processing tool.
8
8
 
9
9
  ## Usage
10
10
 
11
- *Coming soon...*
11
+ ```
12
+ Usage: pipewrench [options] expression
13
+ -c, --compact Remove nil lines from output
14
+ -m, --map Run each line through the expression
15
+ -s, --strip Strip trailing whitespace from each line before running
16
+ -h, --help Show this message
17
+ --version Show version
18
+ ```
19
+
20
+ Pipewrench evaluates the given expression against standard input.
21
+ This allows you to write powerful ruby expressions as part of your pipeline.
22
+
23
+ Here are some examples:
24
+
25
+ ```
26
+ # Given a list of integers, add them up
27
+ $ seq 1 10 | pipewrench 'map(&:to_i).inject(:+)'
28
+ 55
29
+
30
+ # Extract regex from matching lines
31
+ $ echo "My Cat" >> pipewrench.txt
32
+ $ echo "His Dog" >> pipewrench.txt
33
+ $ echo "Her Fish" >> pipewrench.txt
34
+ $ echo "My Frog" >> pipewrench.txt
35
+ $ cat pipewrench.txt | pipewrench 'grep(/^My (\w+)/) {$1}'
36
+ Cat
37
+ Frog
38
+ ```
39
+
40
+ ### -m, --map
41
+
42
+ Evaluate the expression for each line of input.
43
+
44
+ ```
45
+ # Convert input to upper case
46
+ $ cat pipewrench.txt | pipewrench -m upcase
47
+ MY CAT
48
+ HIS DOG
49
+ HER FISH
50
+ MY FROG
51
+ ```
52
+
53
+ ### -c, --compact
54
+
55
+ Remove nils from the output. This is primarily useful in conjunction with `--map` but can be used on its own as well.
56
+
57
+ ```
58
+ # Only show lines less than 5
59
+ $ seq 1 10 | pipewrench -c -m 'self if to_i < 5'
60
+ 1
61
+ 2
62
+ 3
63
+ 4
64
+ ```
65
+
66
+ ### -s, --strip
67
+
68
+ Remove trailing whitespace from each line before evaluating the expression.
69
+
70
+ ```
71
+ # Join a list of numbers as a comma separated list
72
+ $ seq 1 10 | pipewrench -s 'join(", ")'
73
+ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
74
+ ```
12
75
 
13
76
  ## Development
14
77
 
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
  require "pipewrench"
3
3
 
4
- puts Pipewrench::Shell.new($stdin).run(ARGV.join(" "))
4
+ options = Pipewrench::Options.new.parse!
5
+ puts Pipewrench.new($stdin, options).run(ARGV.join(" ")) || []
@@ -1,18 +1,35 @@
1
1
  require "pipewrench/version"
2
+ require "pipewrench/options"
2
3
 
3
- module Pipewrench
4
- class Shell
5
- include Enumerable
4
+ class Pipewrench
5
+ attr_reader :options
6
+ include Enumerable
6
7
 
7
- def initialize(stdin)
8
- @stdin = stdin
8
+ def initialize(stdin, options=Options.new)
9
+ @stdin = stdin
10
+ @options = options
11
+ end
12
+
13
+ def run(cmd)
14
+ if options.compact
15
+ evaluate(cmd).compact
16
+ else
17
+ evaluate(cmd)
9
18
  end
19
+ end
10
20
 
11
- def run(cmd)
12
- instance_eval(cmd)
21
+ def evaluate(cmd)
22
+ if options.map
23
+ map { |line| line.instance_eval(cmd) }
24
+ else
25
+ to_a.instance_eval(cmd)
13
26
  end
27
+ end
14
28
 
15
- def each(&blk)
29
+ def each(&blk)
30
+ if options.strip
31
+ @stdin.each_line.map(&:rstrip).map(&blk)
32
+ else
16
33
  @stdin.each_line(&blk)
17
34
  end
18
35
  end
@@ -0,0 +1,54 @@
1
+ require "optparse"
2
+ require "pipewrench/version"
3
+
4
+ class Pipewrench
5
+ class Options
6
+ attr_reader :map, :strip, :compact
7
+
8
+ def parse!(*args)
9
+ parser.parse!(*args)
10
+ self
11
+ rescue OptionParser::InvalidOption => e
12
+ puts e
13
+ help
14
+ end
15
+
16
+ def to_s
17
+ parser.to_s
18
+ end
19
+
20
+ def help
21
+ puts self
22
+ exit
23
+ end
24
+
25
+ private
26
+
27
+ def parser
28
+ @parser ||= OptionParser.new do |opts|
29
+ opts.banner = "Usage: pipewrench [options] expression"
30
+
31
+ opts.on("-c", "--compact", "Remove nil lines from output") do |m|
32
+ @compact = true
33
+ end
34
+
35
+ opts.on("-m", "--map", "Run each line through the expression") do |m|
36
+ @map = true
37
+ end
38
+
39
+ opts.on("-s", "--strip", "Strip trailing whitespace from each line before running") do |m|
40
+ @strip = true
41
+ end
42
+
43
+ opts.on_tail("-h", "--help", "Show this message") do
44
+ help
45
+ end
46
+
47
+ opts.on_tail("--version", "Show version") do
48
+ puts "Pipewrench #{Pipewrench::VERSION}"
49
+ exit
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
- module Pipewrench
2
- VERSION = "0.1.0"
1
+ class Pipewrench
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipewrench
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Olive
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-10 00:00:00.000000000 Z
11
+ date: 2015-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.10'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.10'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - '>='
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description:
@@ -60,9 +60,9 @@ executables:
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - ".gitignore"
64
- - ".rspec"
65
- - ".travis.yml"
63
+ - .gitignore
64
+ - .rspec
65
+ - .travis.yml
66
66
  - Gemfile
67
67
  - README.md
68
68
  - Rakefile
@@ -70,6 +70,7 @@ files:
70
70
  - bin/setup
71
71
  - exe/pipewrench
72
72
  - lib/pipewrench.rb
73
+ - lib/pipewrench/options.rb
73
74
  - lib/pipewrench/version.rb
74
75
  - pipewrench.gemspec
75
76
  homepage: https://github.com/sionide21/pipewrench
@@ -81,17 +82,17 @@ require_paths:
81
82
  - lib
82
83
  required_ruby_version: !ruby/object:Gem::Requirement
83
84
  requirements:
84
- - - ">="
85
+ - - '>='
85
86
  - !ruby/object:Gem::Version
86
87
  version: '0'
87
88
  required_rubygems_version: !ruby/object:Gem::Requirement
88
89
  requirements:
89
- - - ">="
90
+ - - '>='
90
91
  - !ruby/object:Gem::Version
91
92
  version: '0'
92
93
  requirements: []
93
94
  rubyforge_project:
94
- rubygems_version: 2.2.2
95
+ rubygems_version: 2.0.14
95
96
  signing_key:
96
97
  specification_version: 4
97
98
  summary: General purpose command line pipe processing tool.