raph 0.0.1 → 0.0.2
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 -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
@@ -1,47 +1,50 @@
|
|
1
|
-
module Raph
|
2
|
-
# This module consists of argument parsers for Humans
|
3
|
-
module Parser
|
4
|
-
# Base class for all argument parsers.
|
5
|
-
#
|
6
|
-
# You can create a custom parser by subclassing
|
7
|
-
# `Raph::Parser::BaseParser` and overriding some methods,
|
8
|
-
# or by implementing all the methods by duck typing.
|
9
|
-
class BaseParser
|
10
|
-
# Parser unique id.
|
11
|
-
# If parser class name follows a convention NameParser
|
12
|
-
# then it's id will be automatically determined as it's
|
13
|
-
# name in snake case plus suffix 's'
|
14
|
-
#
|
15
|
-
# Example:
|
16
|
-
# FlagParser.new.id # => :flags
|
17
|
-
# FileParser.new.id # => :files
|
18
|
-
# BaseArgumentParser.new.id # => :base_arguments
|
19
|
-
def id
|
20
|
-
name = class_name.gsub(/parser$/i, '')
|
21
|
-
name << 's' # make it plural
|
22
|
-
|
23
|
-
end
|
24
|
-
|
25
|
-
# Parses arguments and returns results of parsing.
|
26
|
-
def parse(args)
|
27
|
-
end
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
# Returns
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
1
|
+
module Raph
|
2
|
+
# This module consists of argument parsers for Humans
|
3
|
+
module Parser
|
4
|
+
# Base class for all argument parsers.
|
5
|
+
#
|
6
|
+
# You can create a custom parser by subclassing
|
7
|
+
# `Raph::Parser::BaseParser` and overriding some methods,
|
8
|
+
# or by implementing all the methods by duck typing.
|
9
|
+
class BaseParser
|
10
|
+
# Parser unique id.
|
11
|
+
# If parser class name follows a convention NameParser
|
12
|
+
# then it's id will be automatically determined as it's
|
13
|
+
# name in snake case plus suffix 's'
|
14
|
+
#
|
15
|
+
# Example:
|
16
|
+
# FlagParser.new.id # => :flags
|
17
|
+
# FileParser.new.id # => :files
|
18
|
+
# BaseArgumentParser.new.id # => :base_arguments
|
19
|
+
def id
|
20
|
+
name = class_name.gsub(/parser$/i, '')
|
21
|
+
name << 's' # make it plural
|
22
|
+
to_underscored_sym(name)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Parses arguments and returns results of parsing.
|
26
|
+
def parse(args)
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
# Returns underscored symbol of string
|
32
|
+
# (snake case format).
|
33
|
+
def to_underscored_sym(str)
|
34
|
+
str.gsub(/^[-]+/, '').
|
35
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
36
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
37
|
+
tr("-", "_").
|
38
|
+
downcase.
|
39
|
+
to_sym
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
# Returns name of current class.
|
45
|
+
def class_name
|
46
|
+
self.class.name.split('::').last || ''
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,24 +1,24 @@
|
|
1
|
-
require 'raph/parser/base_parser'
|
2
|
-
|
3
|
-
module Raph
|
4
|
-
module Parser
|
5
|
-
# Argument is considered as file argument if and only if
|
6
|
-
# corresponding file exist on file system and match
|
7
|
-
# current argument.
|
8
|
-
#
|
9
|
-
# Example:
|
10
|
-
# 'test.rb' - file in current directory with name 'test.rb'
|
11
|
-
# '*.rb' - all files in current directory with extension 'rb'
|
12
|
-
# '**/*.xml' - all files in current directory and directories
|
13
|
-
# that are in this directory with extension 'xml'
|
14
|
-
class FileParser < BaseParser
|
15
|
-
def parse(args)
|
16
|
-
files = []
|
17
|
-
args.each do |a|
|
18
|
-
files.concat Dir[a]
|
19
|
-
end
|
20
|
-
files.uniq
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
1
|
+
require 'raph/parser/base_parser'
|
2
|
+
|
3
|
+
module Raph
|
4
|
+
module Parser
|
5
|
+
# Argument is considered as file argument if and only if
|
6
|
+
# corresponding file exist on file system and match
|
7
|
+
# current argument.
|
8
|
+
#
|
9
|
+
# Example:
|
10
|
+
# 'test.rb' - file in current directory with name 'test.rb'
|
11
|
+
# '*.rb' - all files in current directory with extension 'rb'
|
12
|
+
# '**/*.xml' - all files in current directory and directories
|
13
|
+
# that are in this directory with extension 'xml'
|
14
|
+
class FileParser < BaseParser
|
15
|
+
def parse(args)
|
16
|
+
files = []
|
17
|
+
args.each do |a|
|
18
|
+
files.concat Dir[a]
|
19
|
+
end
|
20
|
+
files.uniq
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,31 +1,31 @@
|
|
1
|
-
require 'raph/parser/base_parser'
|
2
|
-
|
3
|
-
module Raph
|
4
|
-
module Parser
|
5
|
-
# Considers option as flag if and only if
|
6
|
-
# it's name starts with one dash and follows by
|
7
|
-
# one word character or starts with two dashes
|
8
|
-
# and follows by 2 or more word chacters or dashes.
|
9
|
-
#
|
10
|
-
# Assumes that each option doesn't have spaces.
|
11
|
-
#
|
12
|
-
# Example of flags:
|
13
|
-
# '-h' '-T' '--config'
|
14
|
-
#
|
15
|
-
# Example of non-flags:
|
16
|
-
# 'option' '---option2' '--h'
|
17
|
-
class FlagParser < BaseParser
|
18
|
-
def parse(args)
|
19
|
-
flags = []
|
20
|
-
args.each do |a|
|
21
|
-
flags << a if flag? a
|
22
|
-
end
|
23
|
-
flags
|
24
|
-
end
|
25
|
-
|
26
|
-
def flag?(option)
|
27
|
-
option =~ /^-[\w]$/ || option =~ /^--[\w][\w-]+$/
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
1
|
+
require 'raph/parser/base_parser'
|
2
|
+
|
3
|
+
module Raph
|
4
|
+
module Parser
|
5
|
+
# Considers option as flag if and only if
|
6
|
+
# it's name starts with one dash and follows by
|
7
|
+
# one word character or starts with two dashes
|
8
|
+
# and follows by 2 or more word chacters or dashes.
|
9
|
+
#
|
10
|
+
# Assumes that each option doesn't have spaces.
|
11
|
+
#
|
12
|
+
# Example of flags:
|
13
|
+
# '-h' '-T' '--config'
|
14
|
+
#
|
15
|
+
# Example of non-flags:
|
16
|
+
# 'option' '---option2' '--h'
|
17
|
+
class FlagParser < BaseParser
|
18
|
+
def parse(args)
|
19
|
+
flags = []
|
20
|
+
args.each do |a|
|
21
|
+
flags << to_underscored_sym(a) if flag? a
|
22
|
+
end
|
23
|
+
flags
|
24
|
+
end
|
25
|
+
|
26
|
+
def flag?(option)
|
27
|
+
option =~ /^-[\w]$/ || option =~ /^--[\w][\w-]+$/
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/raph/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Raph
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
1
|
+
module Raph
|
2
|
+
VERSION = '0.0.2'
|
3
|
+
end
|
data/raph.gemspec
CHANGED
@@ -1,26 +1,29 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'raph/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = 'raph'
|
8
|
-
spec.version = Raph::VERSION
|
9
|
-
spec.authors = ['Vitalii Elenhaupt']
|
10
|
-
spec.email = ['velenhaupt@gmail.com']
|
11
|
-
spec.summary = 'Ruby Argument Parsing for Humans.'
|
12
|
-
spec.description =
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
spec.
|
17
|
-
spec.
|
18
|
-
|
19
|
-
spec.
|
20
|
-
|
21
|
-
spec.
|
22
|
-
|
23
|
-
|
24
|
-
spec.
|
25
|
-
|
26
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'raph/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'raph'
|
8
|
+
spec.version = Raph::VERSION
|
9
|
+
spec.authors = ['Vitalii Elenhaupt']
|
10
|
+
spec.email = ['velenhaupt@gmail.com']
|
11
|
+
spec.summary = 'Ruby Argument Parsing for Humans.'
|
12
|
+
spec.description = <<-DESC
|
13
|
+
Lightweight argument parsing with flexible structure.
|
14
|
+
Add your parser in few minutes.
|
15
|
+
DESC
|
16
|
+
spec.homepage = 'https://github.com/veelenga/raph'
|
17
|
+
spec.license = 'MIT'
|
18
|
+
|
19
|
+
spec.files = `git ls-files`.split($RS)
|
20
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(/^spec\//)
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.required_ruby_version = '>= 1.9.3'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
end
|
@@ -1,46 +1,45 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Raph
|
4
|
-
module Parser
|
5
|
-
describe AssignmentParser do
|
6
|
-
describe '#id' do
|
7
|
-
it 'has correct form' do
|
8
|
-
expect(subject.id).to eq :assignments
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe '#parse' do
|
13
|
-
it 'returns assignments only' do
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'has no to return assignments here' do
|
19
|
-
expect(subject.parse(['-h', 'file', '=', '123'])).
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
expect(ass? '
|
32
|
-
expect(ass? '
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
expect(ass? '
|
39
|
-
expect(ass? '
|
40
|
-
expect(ass? 'h=').to be false
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Raph
|
4
|
+
module Parser
|
5
|
+
describe AssignmentParser do
|
6
|
+
describe '#id' do
|
7
|
+
it 'has correct form' do
|
8
|
+
expect(subject.id).to eq :assignments
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#parse' do
|
13
|
+
it 'returns assignments only' do
|
14
|
+
parsed = subject.parse(['-h', '-on=20', '--two=my-file'])
|
15
|
+
expect(parsed).to include(:on => '20', :two => 'my-file')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'has no to return assignments here' do
|
19
|
+
expect(subject.parse(['-h', 'file', '=', '123'])).to be_empty
|
20
|
+
expect(subject.parse([])).to be_empty
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#assignment?' do
|
25
|
+
def ass?(option)
|
26
|
+
subject.assignment? option
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'is an assignment' do
|
30
|
+
expect(ass? 'h=one').to be true
|
31
|
+
expect(ass? '-ass=two').to be true
|
32
|
+
expect(ass? '--ass=two').to be true
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'is not as assignment' do
|
36
|
+
expect(ass? '-h').to be false
|
37
|
+
expect(ass? '-h=').to be false
|
38
|
+
expect(ass? 'h=').to be false
|
39
|
+
expect(ass? '=').to be false
|
40
|
+
expect(ass? '-h=one=two').to be false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -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
|