raph 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt CHANGED
@@ -1,22 +1,22 @@
1
- Copyright (c) 2015 Vitalii Elenhaupt
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2015 Vitalii Elenhaupt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,109 +1,103 @@
1
- # raph [![Build Status](https://api.travis-ci.org/veelenga/raph.svg?branch=master)](https://travis-ci.org/veelenga/raph)
2
-
3
- ![This is Raph]
4
- (http://upload.wikimedia.org/wikipedia/en/5/58/TMNTRaphael2012.png)
5
-
6
- **R**uby **A**rgument **P**arsing for **H**umans
7
-
8
-
9
- Inspired by [args](https://github.com/kennethreitz/args)
10
-
11
- ## Installation:
12
-
13
- `$ gem install raph`
14
-
15
-
16
- ## Usage:
17
-
18
- Here is application sample:
19
-
20
- ```ruby
21
- # sample.rb
22
- require 'raph'
23
-
24
- puts "Arguments passed in: #{$raph.all}"
25
- puts "Flags detected: #{$raph.flags}"
26
- puts "Files detected: #{$raph.files}"
27
- puts "Assignments detected: #{$raph.assignments}"
28
- ```
29
-
30
- If you do not pass any arguments:
31
-
32
- ```sh
33
- $ ruby sample.rb
34
- Arguments passed in: []
35
- Flags detected: []
36
- Files detected: []
37
- Assignments detected: []
38
- ```
39
-
40
- If you have few arguments passed:
41
-
42
- ```sh
43
- $ ruby sample.rb -v --flag1 --flag2 --formatter=simple --convert=true
44
- Arguments passed in: ["-v", "--flag1", "--flag2", "--formatter=simple", "--convert=true"]
45
- Flags detected: ["-v", "--flag1", "--flag2"]
46
- Files detected: []
47
- Assignments detected: ["--formatter=simple", "--convert=true"]
48
- ```
49
-
50
- And finnaly if you pass expanded arguments:
51
-
52
- ```sh
53
- $ ruby sample.rb -f spec/*.rb
54
- Arguments passed in: ["-f", "spec/raph_spec.rb", "spec/spec_helper.rb"]
55
- Flags detected: ["-f"]
56
- Files detected: ["spec/raph_spec.rb", "spec/spec_helper.rb"]
57
- Assignments detected: []
58
- ```
59
-
60
- ## Advanced usage:
61
-
62
- You can use `raph` with custom parsers. For example:
63
-
64
- ```ruby
65
- require 'raph'
66
-
67
- include Raph
68
-
69
- class AnimalParser < BaseParser
70
- ANIMALS = ['cat', 'dog', 'pig', 'bear', 'elephant']
71
-
72
- # Here we define dynamic attribute name.
73
- def id
74
- :animals
75
- end
76
-
77
- def parse(args)
78
- animals = []
79
- args.each do |arg|
80
- animals << arg if ANIMALS.include? arg.strip.downcase
81
- end
82
- animals
83
- end
84
- end
85
-
86
- args = [ '--my-animals', 'cat', 'bird', 'dog', 'elephant' ]
87
-
88
- raph = Raph::Raph.new.tap do |r|
89
- r.add_parser( AnimalParser.new )
90
- r.add_parser( FlagParser.new )
91
- r.parse( args )
92
- end
93
-
94
- # Raph#flags and Raph#animals attributes are dynamically
95
- # added. Both are defined by Parser#id method.
96
- puts "All: #{raph.all}"
97
- puts "Flags: #{raph.flags}"
98
- puts "My animals: #{raph.animals}"
99
-
100
- #All: ["--my-animals", "cat", "bird", "dog", "elephant"]
101
- #Flags: ["--my-animals"]
102
- #My animals: ["cat", "dog", "elephant"]
103
-
104
- ```
105
-
106
- ## TODO:
107
- - Grouped arguments parser.
108
- - Not-files parser.
109
-
1
+ # raph [![Gem Version](https://badge.fury.io/rb/raph.svg)](https://rubygems.org/gems/raph) [![Build Status](https://api.travis-ci.org/veelenga/raph.svg?branch=master)](https://travis-ci.org/veelenga/raph)
2
+
3
+ ![This is Raph](http://upload.wikimedia.org/wikipedia/en/5/58/TMNTRaphael2012.png)
4
+
5
+ **R**uby **A**rgument **P**arsing for **H**umans
6
+
7
+ Inspired by [args](https://github.com/kennethreitz/args)
8
+
9
+ ## Installation:
10
+
11
+ `$ gem install raph`
12
+
13
+
14
+ ## Usage:
15
+
16
+ Here is application sample:
17
+
18
+ ```ruby
19
+ # sample.rb
20
+ require 'raph'
21
+
22
+ puts "Arguments passed in: #{$raph.all}"
23
+ puts "Flags detected: #{$raph.flags}"
24
+ puts "Files detected: #{$raph.files}"
25
+ puts "Assignments detected: #{$raph.assignments}"
26
+ ```
27
+
28
+ If you do not pass any arguments:
29
+
30
+ ```sh
31
+ $ ruby sample.rb
32
+ Arguments passed in: []
33
+ Flags detected: []
34
+ Files detected: []
35
+ Assignments detected: []
36
+ ```
37
+
38
+ If you have few arguments passed:
39
+
40
+ ```sh
41
+ $ ruby sample.rb -v --flag1 --flag2 --formatter=simple --convert=true
42
+ Arguments passed in: ["-v", "--flag1", "--flag2", "--formatter=simple", "--convert=true"]
43
+ Flags detected: [:v, :flag1, :flag2]
44
+ Files detected: []
45
+ Assignments detected: {:formatter=>"simple", :convert=>"true"}
46
+ ```
47
+
48
+ And finnaly if you pass expanded arguments:
49
+
50
+ ```sh
51
+ $ ruby sample.rb -f spec/*.rb
52
+ Arguments passed in: ["-f", "spec/raph_spec.rb", "spec/spec_helper.rb"]
53
+ Flags detected: [:f]
54
+ Files detected: ["spec/raph_spec.rb", "spec/spec_helper.rb"]
55
+ Assignments detected: {}
56
+ ```
57
+
58
+ ## Advanced usage:
59
+
60
+ You can use `raph` with custom parsers. For example:
61
+
62
+ ```ruby
63
+ require 'raph'
64
+
65
+ include Raph
66
+
67
+ class AnimalParser < BaseParser
68
+ ANIMALS = ['cat', 'dog', 'pig', 'bear', 'elephant']
69
+
70
+ def id
71
+ :animals
72
+ end
73
+
74
+ def parse(args)
75
+ animals = []
76
+ args.each do |arg|
77
+ animals << arg if ANIMALS.include? arg.strip.downcase
78
+ end
79
+ animals
80
+ end
81
+ end
82
+
83
+ args = [ '--my-animals', 'cat', 'bird', 'dog', 'elephant' ]
84
+
85
+ raph = Raph::Raph.new.tap do |r|
86
+ r.add_parser( AnimalParser.new )
87
+ r.parse( args )
88
+ end
89
+
90
+ # Raph#animals attribute is added dynamically.
91
+ # It is defined by AnimalParser#id method.
92
+ puts "All: #{raph.all}"
93
+ puts "My animals: #{raph.animals}"
94
+
95
+ #All: ["--my-animals", "cat", "bird", "dog", "elephant"]
96
+ #My animals: ["cat", "dog", "elephant"]
97
+
98
+ ```
99
+
100
+ ## TODO:
101
+ - Grouped arguments parser.
102
+ - Not-files parser.
103
+
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require 'bundler/gem_tasks'
2
- require 'rspec/core/rake_task'
3
-
4
- RSpec::Core::RakeTask.new(:spec)
5
-
6
- task default: [:spec]
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: [:spec]
data/example/sample.rb CHANGED
@@ -1,6 +1,6 @@
1
- require 'raph'
2
-
3
- puts "Arguments passed in: #{$raph.all}"
4
- puts "Flags detected: #{$raph.flags}"
5
- puts "Files detected: #{$raph.files}"
6
- puts "Assignments detected: #{$raph.assignments}"
1
+ require 'raph'
2
+
3
+ puts "Arguments passed in: #{$raph.all}"
4
+ puts "Flags detected: #{$raph.flags}"
5
+ puts "Files detected: #{$raph.files}"
6
+ puts "Assignments detected: #{$raph.assignments}"
data/lib/raph.rb CHANGED
@@ -1,61 +1,61 @@
1
- require 'raph/parser/file_parser'
2
- require 'raph/parser/flag_parser'
3
- require 'raph/parser/assignment_parser'
4
-
5
- module Raph
6
- # TODO: class description
7
- class Raph
8
- def initialize
9
- @parsed = {}
10
- @parsers = []
11
- end
12
-
13
- def parse(args)
14
- @all = args.dup
15
-
16
- @parsers.each do |p|
17
- @parsed[p.id.to_sym] = p.parse(@all)
18
- end
19
- end
20
-
21
- def all
22
- @all.dup
23
- end
24
-
25
- def add_parser(parser)
26
- @parsers.push parser
27
- end
28
-
29
- def method_missing(method_sym, *arguments, &block)
30
- # TODO: do not accept any arguments or block
31
- if has_attribute? method_sym
32
- get_attribute_value method_sym
33
- else
34
- super
35
- end
36
- end
37
-
38
- private
39
-
40
- # Returns true if this class has dynamic argument 'arg'
41
- def has_attribute?(arg)
42
- @parsed.include? arg
43
- end
44
-
45
- # Returns value of dynamic argument 'arg'
46
- def get_attribute_value(arg)
47
- @parsed[arg]
48
- end
49
- end
50
-
51
- include Parser
52
-
53
- $raph = Raph.new.tap do |r|
54
- r.add_parser(FileParser.new)
55
- r.add_parser(FlagParser.new)
56
- r.add_parser(AssignmentParser.new)
57
-
58
- r.parse(ARGV)
59
- end
60
- end
61
-
1
+ require 'raph/parser/file_parser'
2
+ require 'raph/parser/flag_parser'
3
+ require 'raph/parser/assignment_parser'
4
+
5
+ module Raph
6
+ # TODO: class description
7
+ class Raph
8
+ def initialize
9
+ @parsed = {}
10
+ @parsers = []
11
+ end
12
+
13
+ def parse(args)
14
+ @all = args.dup
15
+
16
+ @parsers.each do |p|
17
+ @parsed[p.id.to_sym] = p.parse(@all)
18
+ end
19
+ end
20
+
21
+ def all
22
+ @all.dup
23
+ end
24
+
25
+ def add_parser(parser)
26
+ @parsers.push parser
27
+ end
28
+
29
+ def method_missing(method_sym, *arguments, &block)
30
+ # TODO: do not accept any arguments or block
31
+ if has_attribute? method_sym
32
+ get_attribute_value method_sym
33
+ else
34
+ super
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ # Returns true if this class has dynamic argument 'arg'
41
+ def has_attribute?(arg)
42
+ @parsed.include? arg
43
+ end
44
+
45
+ # Returns value of dynamic argument 'arg'
46
+ def get_attribute_value(arg)
47
+ @parsed[arg]
48
+ end
49
+ end
50
+
51
+ include Parser
52
+
53
+ $raph = Raph.new.tap do |r|
54
+ r.add_parser(FileParser.new)
55
+ r.add_parser(FlagParser.new)
56
+ r.add_parser(AssignmentParser.new)
57
+
58
+ r.parse(ARGV)
59
+ end
60
+ end
61
+
@@ -1,33 +1,38 @@
1
- require 'raph/parser/assignment_parser'
2
-
3
- module Raph
4
- module Parser
5
- # Considers option as assignment if and only if
6
- # it has an assignment character (=) between
7
- # option key and option value.
8
- #
9
- # Assumes that each option doesn't have spaces.
10
- #
11
- # Example of assignments:
12
- # 'h=one' '-assg=two' '--config=my-file'
13
- #
14
- # Example of non-assignments:
15
- # '-h' '-h=' 'h=' '=' '--config='
16
- #
17
- class AssignmentParser < BaseParser
18
- def parse(args)
19
- assgs = []
20
- args.each do |a|
21
- assgs << a if assignment? a
22
- end
23
- assgs
24
- end
25
-
26
- def assignment?(option)
27
- option.include?('=')\
28
- && !option.start_with?('=')\
29
- && !option.end_with?('=')
30
- end
31
- end
32
- end
33
- end
1
+ require 'raph/parser/assignment_parser'
2
+
3
+ module Raph
4
+ module Parser
5
+ # Considers option as assignment if and only if
6
+ # it has an assignment character (=) between
7
+ # option key and option value.
8
+ #
9
+ # Assumes that each option doesn't have spaces.
10
+ #
11
+ # Example of assignments:
12
+ # 'h=one' '-assg=two' '--config=my-file'
13
+ #
14
+ # Example of non-assignments:
15
+ # '-h' '-h=' 'h=' '=' '--config='
16
+ #
17
+ class AssignmentParser < BaseParser
18
+ def parse(args)
19
+ assgs = {}
20
+ args.each do |a|
21
+ if assignment? a
22
+ kv = a.split('=')
23
+ k = to_underscored_sym(kv.first)
24
+ v = kv.last
25
+ assgs[k] = v
26
+ end
27
+ end
28
+ assgs
29
+ end
30
+
31
+ def assignment?(option)
32
+ option.count('=') == 1 &&
33
+ !option.start_with?('=') &&
34
+ !option.end_with?('=')
35
+ end
36
+ end
37
+ end
38
+ end