raph 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,103 +1,102 @@
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
-
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
+ puts "Grouped arguments: #{$raph.grouped_args}"
27
+ ```
28
+
29
+ If you do not pass any arguments:
30
+
31
+ ```sh
32
+ $ ruby sample.rb
33
+ Arguments passed in: []
34
+ Flags detected: []
35
+ Files detected: []
36
+ Assignments detected: {}
37
+ Grouped arguments: {}
38
+ ```
39
+
40
+ If you have few arguments passed:
41
+
42
+ ```sh
43
+ $ ruby sample.rb -v 1 2 3 --flag1 3 --flag2 --formatter=simple true
44
+ Arguments passed in: ["-v", "1", "2", "3", "--flag1", "3", "--flag2", "--formatter=simple", "true"]
45
+ Flags detected: [:v, :flag1, :flag2]
46
+ Files detected: []
47
+ Assignments detected: {:formatter=>"simple"}
48
+ Grouped arguments: {:v=>["1", "2", "3"], :flag1=>["3"], :flag2=>[], :"formatter=simple"=>["true"]}
49
+ ```
50
+
51
+ And finnaly if you pass expanded arguments:
52
+
53
+ ```sh
54
+ $ ruby sample.rb -f spec/*.rb
55
+ Arguments passed in: ["-f", "spec/raph_spec.rb", "spec/spec_helper.rb"]
56
+ Flags detected: [:f]
57
+ Files detected: ["spec/raph_spec.rb", "spec/spec_helper.rb"]
58
+ Assignments detected: {}
59
+ Grouped arguments: {:f=>["spec/raph_spec.rb", "spec/spec_helper.rb"]}
60
+ ```
61
+
62
+ ## Advanced usage:
63
+
64
+ You can use `raph` with custom parsers. For example:
65
+
66
+ ```ruby
67
+ require 'raph'
68
+
69
+ include Raph
70
+
71
+ class AnimalParser < BaseParser
72
+ ANIMALS = ['cat', 'dog', 'pig', 'bear', 'elephant']
73
+
74
+ def id
75
+ :animals
76
+ end
77
+
78
+ def parse(args)
79
+ animals = []
80
+ args.each do |arg|
81
+ animals << arg if ANIMALS.include? arg.strip.downcase
82
+ end
83
+ animals
84
+ end
85
+ end
86
+
87
+ args = [ '--my-animals', 'cat', 'bird', 'dog', 'elephant' ]
88
+
89
+ raph = Raph::Raph.new.tap do |r|
90
+ r.add_parser( AnimalParser.new )
91
+ r.parse( args )
92
+ end
93
+
94
+ # Raph#animals attribute is added dynamically.
95
+ # It is defined by AnimalParser#id method.
96
+ puts "All: #{raph.all}"
97
+ puts "My animals: #{raph.animals}"
98
+
99
+ #All: ["--my-animals", "cat", "bird", "dog", "elephant"]
100
+ #My animals: ["cat", "dog", "elephant"]
101
+
102
+ ```
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]
@@ -1,6 +1,7 @@
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}"
7
+ puts "Grouped arguments: #{$raph.grouped_args}"
@@ -1,61 +1,110 @@
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
+ require 'raph/parser/grouped_arg_parser'
5
+
6
+ module Raph
7
+ #
8
+ # Ruby Argument Parser for Humans.
9
+ # Parses arguments using external parsers.
10
+ #
11
+ # class Parser1 < BaseParser
12
+ # def id
13
+ # 'parser1_result'
14
+ # end
15
+ #
16
+ # def parse(arg)
17
+ # # return your parsed arguments
18
+ # end
19
+ # end
20
+ #
21
+ # class Parser2 < Base
22
+ # def id
23
+ # 'parser2_result'
24
+ # end
25
+ #
26
+ # def parse(arg)
27
+ # # return your parsed arguments
28
+ # end
29
+ # end
30
+ #
31
+ # raph = Raph.new.tap do |r|
32
+ # r.add_parser(Parser1.new)
33
+ # r.add_parser(Parser2.new)
34
+ # # ...
35
+ #
36
+ # r.parse (arguments)
37
+ # end
38
+ #
39
+ # puts raph.parser1_result # parsed arguments by Parser1
40
+ # puts raph.parser2_result # parsed arguments by Parser2
41
+ class Raph
42
+ #
43
+ # Initializes Raph.
44
+ def initialize
45
+ @parsed = {}
46
+ @parsers = []
47
+ end
48
+
49
+ #
50
+ # Parses arguments using external parsers.
51
+ #
52
+ # +args+:: arguments to be parsed.
53
+ def parse(args)
54
+ @all = args.dup
55
+
56
+ @parsers.each do |p|
57
+ @parsed[p.id.to_sym] = p.parse(@all)
58
+ end
59
+ end
60
+
61
+ #
62
+ # Returns all arguments.
63
+ def all
64
+ @all.dup
65
+ end
66
+
67
+ #
68
+ # Adds new external parser to parser list.
69
+ #
70
+ # +parser+:: external parser.
71
+ def add_parser(parser)
72
+ @parsers.push parser
73
+ end
74
+
75
+ def method_missing(method_sym, *arguments, &block)
76
+ if has_attribute? method_sym
77
+ raise 'Arguments not applicable' if arguments.length > 0
78
+ raise 'Block not applicable' if block_given?
79
+ get_attribute_value method_sym
80
+ else
81
+ super
82
+ end
83
+ end
84
+
85
+ private
86
+
87
+ #
88
+ # Returns true if this class has dynamic argument +arg+.
89
+ def has_attribute?(arg)
90
+ @parsed.include? arg
91
+ end
92
+
93
+ #
94
+ # Returns value of dynamic argument +arg+
95
+ def get_attribute_value(arg)
96
+ @parsed[arg]
97
+ end
98
+ end
99
+
100
+ include Parser
101
+
102
+ $raph = Raph.new.tap do |r|
103
+ r.add_parser(FileParser.new)
104
+ r.add_parser(FlagParser.new)
105
+ r.add_parser(AssignmentParser.new)
106
+ r.add_parser(GroupedArgParser.new)
107
+
108
+ r.parse(ARGV)
109
+ end
110
+ end