raph 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +16 -16
- data/.travis.yml +9 -9
- data/.yardopts +2 -2
- data/CHANGELOG.md +7 -0
- data/Gemfile +4 -4
- data/LICENSE +202 -202
- data/LICENSE.txt +22 -22
- data/README.md +102 -103
- data/Rakefile +6 -6
- data/example/sample.rb +7 -6
- data/lib/raph.rb +110 -61
- data/lib/raph/parser/assignment_parser.rb +38 -38
- data/lib/raph/parser/base_parser.rb +50 -50
- data/lib/raph/parser/file_parser.rb +24 -24
- data/lib/raph/parser/flag_parser.rb +31 -31
- data/lib/raph/parser/grouped_arg_parser.rb +42 -0
- data/lib/raph/version.rb +3 -3
- data/raph.gemspec +29 -29
- data/spec/raph/parser/assinment_parser_spec.rb +45 -45
- data/spec/raph/parser/base_parser_spec.rb +47 -47
- data/spec/raph/parser/file_parser_spec.rb +25 -25
- data/spec/raph/parser/flag_parser_spec.rb +48 -48
- data/spec/raph/parser/grouped_args_parser_spec.rb +60 -0
- data/spec/raph_spec.rb +78 -56
- data/spec/spec_helper.rb +1 -1
- metadata +19 -14
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,103 +1,102 @@
|
|
1
|
-
# raph [](https://rubygems.org/gems/raph) [](https://travis-ci.org/veelenga/raph)
|
2
|
-
|
3
|
-

|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
def
|
75
|
-
animals
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
animals
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
#
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
1
|
+
# raph [](https://rubygems.org/gems/raph) [](https://travis-ci.org/veelenga/raph)
|
2
|
+
|
3
|
+

|
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]
|
data/example/sample.rb
CHANGED
@@ -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}"
|
data/lib/raph.rb
CHANGED
@@ -1,61 +1,110 @@
|
|
1
|
-
require 'raph/parser/file_parser'
|
2
|
-
require 'raph/parser/flag_parser'
|
3
|
-
require 'raph/parser/assignment_parser'
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|