raph 0.0.1 → 0.0.2
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 -0
- data/Gemfile +4 -4
- data/LICENSE +202 -202
- data/LICENSE.txt +22 -22
- data/README.md +103 -109
- data/Rakefile +6 -6
- data/example/sample.rb +6 -6
- data/lib/raph.rb +61 -61
- data/lib/raph/parser/assignment_parser.rb +38 -33
- data/lib/raph/parser/base_parser.rb +50 -47
- data/lib/raph/parser/file_parser.rb +24 -24
- data/lib/raph/parser/flag_parser.rb +31 -31
- data/lib/raph/version.rb +3 -3
- data/raph.gemspec +29 -26
- data/spec/raph/parser/assinment_parser_spec.rb +45 -46
- 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_spec.rb +56 -56
- data/spec/spec_helper.rb +1 -1
- metadata +17 -15
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 [](https://travis-ci.org/veelenga/raph)
|
2
|
-
|
3
|
-
![This is Raph]
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
puts "
|
25
|
-
puts "
|
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
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
#
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
```
|
105
|
-
|
106
|
-
## TODO:
|
107
|
-
- Grouped arguments parser.
|
108
|
-
- Not-files parser.
|
109
|
-
|
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
|
+
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
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|