raph 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- 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
@@ -1,47 +1,47 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Raph
|
4
|
-
module Parser
|
5
|
-
describe BaseParser do
|
6
|
-
describe '#id' do
|
7
|
-
it 'returns correct value for base class' do
|
8
|
-
expect(subject.id).to eql :bases
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'returns correct value for child class' do
|
12
|
-
class BaseArgumentParser < BaseParser; end
|
13
|
-
expect(BaseArgumentParser.new.id).to eql :base_arguments
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'returns correct value for modulized class' do
|
17
|
-
module A
|
18
|
-
module B
|
19
|
-
class TestParser < BaseParser; end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
expect(A::B::TestParser.new.id).to eql :tests
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'is correct when no name convention' do
|
26
|
-
class Plural < BaseParser; end
|
27
|
-
expect(Plural.new.id).to eql :plurals
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'can be redefined' do
|
31
|
-
class Plural < BaseParser
|
32
|
-
def id
|
33
|
-
:tests
|
34
|
-
end
|
35
|
-
end
|
36
|
-
expect(Plural.new.id).to eql :tests
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe '#parse' do
|
41
|
-
it 'returns args' do
|
42
|
-
expect(subject.parse [1, 2, 3]).to be nil
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Raph
|
4
|
+
module Parser
|
5
|
+
describe BaseParser do
|
6
|
+
describe '#id' do
|
7
|
+
it 'returns correct value for base class' do
|
8
|
+
expect(subject.id).to eql :bases
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns correct value for child class' do
|
12
|
+
class BaseArgumentParser < BaseParser; end
|
13
|
+
expect(BaseArgumentParser.new.id).to eql :base_arguments
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns correct value for modulized class' do
|
17
|
+
module A
|
18
|
+
module B
|
19
|
+
class TestParser < BaseParser; end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
expect(A::B::TestParser.new.id).to eql :tests
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'is correct when no name convention' do
|
26
|
+
class Plural < BaseParser; end
|
27
|
+
expect(Plural.new.id).to eql :plurals
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'can be redefined' do
|
31
|
+
class Plural < BaseParser
|
32
|
+
def id
|
33
|
+
:tests
|
34
|
+
end
|
35
|
+
end
|
36
|
+
expect(Plural.new.id).to eql :tests
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#parse' do
|
41
|
+
it 'returns args' do
|
42
|
+
expect(subject.parse [1, 2, 3]).to be nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -1,25 +1,25 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
require 'raph/parser/file_parser'
|
4
|
-
|
5
|
-
module Raph
|
6
|
-
module Parser
|
7
|
-
describe FileParser do
|
8
|
-
describe '#parse' do
|
9
|
-
it 'detects files' do
|
10
|
-
expect(subject.parse(['**/*.rb'])).to include(Dir["**/file_parser_spec.rb"].join)
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'does not detect non files' do
|
14
|
-
expect(subject.parse(['no-such-file.py'])).to be_empty
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
describe '#id' do
|
19
|
-
it 'has correct form' do
|
20
|
-
expect(subject.id).to eql :files
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'raph/parser/file_parser'
|
4
|
+
|
5
|
+
module Raph
|
6
|
+
module Parser
|
7
|
+
describe FileParser do
|
8
|
+
describe '#parse' do
|
9
|
+
it 'detects files' do
|
10
|
+
expect(subject.parse(['**/*.rb'])).to include(Dir["**/file_parser_spec.rb"].join)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'does not detect non files' do
|
14
|
+
expect(subject.parse(['no-such-file.py'])).to be_empty
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#id' do
|
19
|
+
it 'has correct form' do
|
20
|
+
expect(subject.id).to eql :files
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,48 +1,48 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Raph
|
4
|
-
module Parser
|
5
|
-
describe FlagParser do
|
6
|
-
describe '#id' do
|
7
|
-
it 'has correct form' do
|
8
|
-
expect(subject.id).to eq :flags
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe '#parse' do
|
13
|
-
it 'returns flags only' do
|
14
|
-
expect(subject.parse(['-h', '-9', '123', '--config', 'config.xml'])).
|
15
|
-
to match_array([:h, :'9', :config])
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'has no flags here' do
|
19
|
-
expect(subject.parse(['true', '-', '123'])).to match_array([])
|
20
|
-
expect(subject.parse([])).to match_array([])
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
describe '#flag?' do
|
25
|
-
def flag?(option)
|
26
|
-
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'is a flag' do
|
30
|
-
expect(flag? '-h').to be true
|
31
|
-
expect(flag? '-T').to be true
|
32
|
-
expect(flag? '--config').to be true
|
33
|
-
expect(flag? '--my-flag').to be true
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'is not a flag?' do
|
37
|
-
expect(flag? '').to be false
|
38
|
-
expect(flag? '-').to be false
|
39
|
-
expect(flag? '--').to be false
|
40
|
-
expect(flag? '---').to be false
|
41
|
-
expect(flag? '--h').to be false
|
42
|
-
expect(flag? 'option').to be false
|
43
|
-
expect(flag? '---option').to be false
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Raph
|
4
|
+
module Parser
|
5
|
+
describe FlagParser do
|
6
|
+
describe '#id' do
|
7
|
+
it 'has correct form' do
|
8
|
+
expect(subject.id).to eq :flags
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#parse' do
|
13
|
+
it 'returns flags only' do
|
14
|
+
expect(subject.parse(['-h', '-9', '123', '--config', 'config.xml'])).
|
15
|
+
to match_array([:h, :'9', :config])
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'has no flags here' do
|
19
|
+
expect(subject.parse(['true', '-', '123'])).to match_array([])
|
20
|
+
expect(subject.parse([])).to match_array([])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#flag?' do
|
25
|
+
def flag?(option)
|
26
|
+
subject.flag?(option)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'is a flag' do
|
30
|
+
expect(flag? '-h').to be true
|
31
|
+
expect(flag? '-T').to be true
|
32
|
+
expect(flag? '--config').to be true
|
33
|
+
expect(flag? '--my-flag').to be true
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'is not a flag?' do
|
37
|
+
expect(flag? '').to be false
|
38
|
+
expect(flag? '-').to be false
|
39
|
+
expect(flag? '--').to be false
|
40
|
+
expect(flag? '---').to be false
|
41
|
+
expect(flag? '--h').to be false
|
42
|
+
expect(flag? 'option').to be false
|
43
|
+
expect(flag? '---option').to be false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Raph
|
4
|
+
module Parser
|
5
|
+
describe GroupedArgParser do
|
6
|
+
describe '#id' do
|
7
|
+
it 'has correct form' do
|
8
|
+
expect(subject.id).to eq :grouped_args
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#parse' do
|
13
|
+
it 'returns groups' do
|
14
|
+
parsed = subject.parse(['-s', '1', '2', '3', '--assignment=one'])
|
15
|
+
expect(parsed.size).to be 2
|
16
|
+
expect(parsed).to include(:s => ['1', '2', '3'])
|
17
|
+
expect(parsed).to include(:'assignment=one' => [])
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns empty hash when group not found' do
|
21
|
+
parsed = subject.parse(['1', '2', '3', 'one', 'two'])
|
22
|
+
expect(parsed).to be_a Hash
|
23
|
+
expect(parsed).to be_empty
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'returns empty hash when there are no parameters' do
|
27
|
+
parsed = subject.parse([])
|
28
|
+
expect(parsed).to be_a Hash
|
29
|
+
expect(parsed).to be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'selects only arguments that belongs to the group' do
|
33
|
+
parsed = subject.parse(['one', 'two', '-f', 'three', 'four'])
|
34
|
+
expect(parsed.size).to be 1
|
35
|
+
expect(parsed).to include(:f => ['three', 'four'])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#group?' do
|
40
|
+
def group?(option)
|
41
|
+
subject.group?(option)
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'is a group' do
|
45
|
+
positive_options = ['-h', '--help', '--assignment=true']
|
46
|
+
positive_options.each do |option|
|
47
|
+
expect(group? option).to be true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'is not a group' do
|
52
|
+
negative_options = ['', '-', '--', ' ', 'option', '1', '-assign=', '=']
|
53
|
+
negative_options.each do |option|
|
54
|
+
expect(group? option).to be false
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/raph_spec.rb
CHANGED
@@ -1,56 +1,78 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'fileutils'
|
3
|
-
|
4
|
-
module Raph
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
raph
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
r.add_parser(
|
33
|
-
r.
|
34
|
-
|
35
|
-
|
36
|
-
expect(raph.
|
37
|
-
expect(raph.send(
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module Raph
|
5
|
+
class NumberParser
|
6
|
+
attr_accessor :id, :max
|
7
|
+
|
8
|
+
def initialize(id, max=100)
|
9
|
+
@id = id
|
10
|
+
@max = max
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse(args)
|
14
|
+
args.select { |x| x <= max }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe Raph do
|
19
|
+
let (:numbers) { (1..100) }
|
20
|
+
let (:parser1) { NumberParser.new(:first, 5) }
|
21
|
+
let (:parser2) { NumberParser.new(:second, 6) }
|
22
|
+
|
23
|
+
describe '#parse' do
|
24
|
+
it 'should parse nothing if parsers not added' do
|
25
|
+
raph = Raph.new.tap { |r| r.parse numbers }
|
26
|
+
expect(raph.all).to match_array numbers
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should have correspoding attribute if parser added' do
|
30
|
+
raph = Raph.new.tap do |r|
|
31
|
+
r.add_parser(parser1)
|
32
|
+
r.add_parser(parser2)
|
33
|
+
r.parse(numbers)
|
34
|
+
end
|
35
|
+
expect(raph.all).to match_array numbers
|
36
|
+
expect(raph.send(parser1.id)).to match_array (1..parser1.max)
|
37
|
+
expect(raph.send(parser2.id)).to match_array (1..parser2.max)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#method_missing' do
|
42
|
+
it 'should throw when argument passed' do
|
43
|
+
raph = Raph.new.tap do |r|
|
44
|
+
r.add_parser(parser1)
|
45
|
+
r.parse(numbers)
|
46
|
+
end
|
47
|
+
expect{ raph.send(parser1.id, 'argument') }.to raise_error
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should throw when block passed' do
|
51
|
+
raph = Raph.new.tap do |r|
|
52
|
+
r.add_parser(parser1)
|
53
|
+
r.parse(numbers)
|
54
|
+
end
|
55
|
+
expect{ raph.send(parser1.id) { 'my block' } }.to raise_error
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should throw when wrong id given' do
|
59
|
+
raph = Raph.new.tap { |r| r.parse numbers }
|
60
|
+
expect{ raph.send(:wrong_id) }.to raise_error
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when we include Raph' do
|
66
|
+
it 'loads global variable' do
|
67
|
+
expect($raph).to be_a Raph
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'has proper attributes' do
|
71
|
+
raph = $raph
|
72
|
+
expect(raph.all).not_to be nil
|
73
|
+
expect(raph.send(FlagParser.new.id)).not_to be nil
|
74
|
+
expect(raph.send(FileParser.new.id)).not_to be nil
|
75
|
+
expect(raph.send(AssignmentParser.new.id)).not_to be nil
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'raph'
|
1
|
+
require 'raph'
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: raph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vitalii Elenhaupt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-22 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.7'
|
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.7'
|
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: '3.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: '3.0'
|
55
55
|
description: |2
|
@@ -61,9 +61,10 @@ executables: []
|
|
61
61
|
extensions: []
|
62
62
|
extra_rdoc_files: []
|
63
63
|
files:
|
64
|
-
- .gitignore
|
65
|
-
- .travis.yml
|
66
|
-
- .yardopts
|
64
|
+
- ".gitignore"
|
65
|
+
- ".travis.yml"
|
66
|
+
- ".yardopts"
|
67
|
+
- CHANGELOG.md
|
67
68
|
- Gemfile
|
68
69
|
- LICENSE
|
69
70
|
- LICENSE.txt
|
@@ -75,12 +76,14 @@ files:
|
|
75
76
|
- lib/raph/parser/base_parser.rb
|
76
77
|
- lib/raph/parser/file_parser.rb
|
77
78
|
- lib/raph/parser/flag_parser.rb
|
79
|
+
- lib/raph/parser/grouped_arg_parser.rb
|
78
80
|
- lib/raph/version.rb
|
79
81
|
- raph.gemspec
|
80
82
|
- spec/raph/parser/assinment_parser_spec.rb
|
81
83
|
- spec/raph/parser/base_parser_spec.rb
|
82
84
|
- spec/raph/parser/file_parser_spec.rb
|
83
85
|
- spec/raph/parser/flag_parser_spec.rb
|
86
|
+
- spec/raph/parser/grouped_args_parser_spec.rb
|
84
87
|
- spec/raph_spec.rb
|
85
88
|
- spec/spec_helper.rb
|
86
89
|
homepage: https://github.com/veelenga/raph
|
@@ -93,17 +96,17 @@ require_paths:
|
|
93
96
|
- lib
|
94
97
|
required_ruby_version: !ruby/object:Gem::Requirement
|
95
98
|
requirements:
|
96
|
-
- -
|
99
|
+
- - ">="
|
97
100
|
- !ruby/object:Gem::Version
|
98
101
|
version: 1.9.3
|
99
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
103
|
requirements:
|
101
|
-
- -
|
104
|
+
- - ">="
|
102
105
|
- !ruby/object:Gem::Version
|
103
106
|
version: '0'
|
104
107
|
requirements: []
|
105
108
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.4.6
|
107
110
|
signing_key:
|
108
111
|
specification_version: 4
|
109
112
|
summary: Ruby Argument Parsing for Humans.
|
@@ -112,5 +115,7 @@ test_files:
|
|
112
115
|
- spec/raph/parser/base_parser_spec.rb
|
113
116
|
- spec/raph/parser/file_parser_spec.rb
|
114
117
|
- spec/raph/parser/flag_parser_spec.rb
|
118
|
+
- spec/raph/parser/grouped_args_parser_spec.rb
|
115
119
|
- spec/raph_spec.rb
|
116
120
|
- spec/spec_helper.rb
|
121
|
+
has_rdoc:
|