raph 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE +202 -0
- data/LICENSE.txt +22 -0
- data/README.md +109 -0
- data/Rakefile +6 -0
- data/example/sample.rb +6 -0
- data/lib/raph.rb +61 -0
- data/lib/raph/parser/assignment_parser.rb +33 -0
- data/lib/raph/parser/base_parser.rb +47 -0
- data/lib/raph/parser/file_parser.rb +24 -0
- data/lib/raph/parser/flag_parser.rb +31 -0
- data/lib/raph/version.rb +3 -0
- data/raph.gemspec +26 -0
- data/spec/raph/parser/assinment_parser_spec.rb +46 -0
- data/spec/raph/parser/base_parser_spec.rb +47 -0
- data/spec/raph/parser/file_parser_spec.rb +25 -0
- data/spec/raph/parser/flag_parser_spec.rb +48 -0
- data/spec/raph_spec.rb +56 -0
- data/spec/spec_helper.rb +1 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: baa921e54bbb7172d06e69cffb2411f838771757
|
4
|
+
data.tar.gz: dbef23ebf8287434f68e61f8062a1b5d306d8013
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3daf13581b2e88f2e3baf4c6c275c677a4923dba339b9f915002d588ef6bbecf66e6a73a78602782c4c27fa631e1316e38f4ca27111c1dc0b207d7325a97eafd
|
7
|
+
data.tar.gz: 5305bb2ade9c880676aac8967dc35b39706710add5cc77e2f3d169dfddc3b697fdc4e62da0f92359217fb03bb6412f0bcbcb7a0fb838d7038fa764d5a108a2bd
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
202
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +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.
|
data/README.md
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
# raph [![Build Status](https://api.travis-ci.org/veelenga/raph.svg?branch=master)](https://travis-ci.org/veelenga/raph)
|
2
|
+
|
3
|
+
![This is Raph]
|
4
|
+
(http://upload.wikimedia.org/wikipedia/en/5/58/TMNTRaphael2012.png)
|
5
|
+
|
6
|
+
**R**uby **A**rgument **P**arsing for **H**umans
|
7
|
+
|
8
|
+
|
9
|
+
Inspired by [args](https://github.com/kennethreitz/args)
|
10
|
+
|
11
|
+
## Installation:
|
12
|
+
|
13
|
+
`$ gem install raph`
|
14
|
+
|
15
|
+
|
16
|
+
## Usage:
|
17
|
+
|
18
|
+
Here is application sample:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
# sample.rb
|
22
|
+
require 'raph'
|
23
|
+
|
24
|
+
puts "Arguments passed in: #{$raph.all}"
|
25
|
+
puts "Flags detected: #{$raph.flags}"
|
26
|
+
puts "Files detected: #{$raph.files}"
|
27
|
+
puts "Assignments detected: #{$raph.assignments}"
|
28
|
+
```
|
29
|
+
|
30
|
+
If you do not pass any arguments:
|
31
|
+
|
32
|
+
```sh
|
33
|
+
$ ruby sample.rb
|
34
|
+
Arguments passed in: []
|
35
|
+
Flags detected: []
|
36
|
+
Files detected: []
|
37
|
+
Assignments detected: []
|
38
|
+
```
|
39
|
+
|
40
|
+
If you have few arguments passed:
|
41
|
+
|
42
|
+
```sh
|
43
|
+
$ ruby sample.rb -v --flag1 --flag2 --formatter=simple --convert=true
|
44
|
+
Arguments passed in: ["-v", "--flag1", "--flag2", "--formatter=simple", "--convert=true"]
|
45
|
+
Flags detected: ["-v", "--flag1", "--flag2"]
|
46
|
+
Files detected: []
|
47
|
+
Assignments detected: ["--formatter=simple", "--convert=true"]
|
48
|
+
```
|
49
|
+
|
50
|
+
And finnaly if you pass expanded arguments:
|
51
|
+
|
52
|
+
```sh
|
53
|
+
$ ruby sample.rb -f spec/*.rb
|
54
|
+
Arguments passed in: ["-f", "spec/raph_spec.rb", "spec/spec_helper.rb"]
|
55
|
+
Flags detected: ["-f"]
|
56
|
+
Files detected: ["spec/raph_spec.rb", "spec/spec_helper.rb"]
|
57
|
+
Assignments detected: []
|
58
|
+
```
|
59
|
+
|
60
|
+
## Advanced usage:
|
61
|
+
|
62
|
+
You can use `raph` with custom parsers. For example:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
require 'raph'
|
66
|
+
|
67
|
+
include Raph
|
68
|
+
|
69
|
+
class AnimalParser < BaseParser
|
70
|
+
ANIMALS = ['cat', 'dog', 'pig', 'bear', 'elephant']
|
71
|
+
|
72
|
+
# Here we define dynamic attribute name.
|
73
|
+
def id
|
74
|
+
:animals
|
75
|
+
end
|
76
|
+
|
77
|
+
def parse(args)
|
78
|
+
animals = []
|
79
|
+
args.each do |arg|
|
80
|
+
animals << arg if ANIMALS.include? arg.strip.downcase
|
81
|
+
end
|
82
|
+
animals
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
args = [ '--my-animals', 'cat', 'bird', 'dog', 'elephant' ]
|
87
|
+
|
88
|
+
raph = Raph::Raph.new.tap do |r|
|
89
|
+
r.add_parser( AnimalParser.new )
|
90
|
+
r.add_parser( FlagParser.new )
|
91
|
+
r.parse( args )
|
92
|
+
end
|
93
|
+
|
94
|
+
# Raph#flags and Raph#animals attributes are dynamically
|
95
|
+
# added. Both are defined by Parser#id method.
|
96
|
+
puts "All: #{raph.all}"
|
97
|
+
puts "Flags: #{raph.flags}"
|
98
|
+
puts "My animals: #{raph.animals}"
|
99
|
+
|
100
|
+
#All: ["--my-animals", "cat", "bird", "dog", "elephant"]
|
101
|
+
#Flags: ["--my-animals"]
|
102
|
+
#My animals: ["cat", "dog", "elephant"]
|
103
|
+
|
104
|
+
```
|
105
|
+
|
106
|
+
## TODO:
|
107
|
+
- Grouped arguments parser.
|
108
|
+
- Not-files parser.
|
109
|
+
|
data/Rakefile
ADDED
data/example/sample.rb
ADDED
data/lib/raph.rb
ADDED
@@ -0,0 +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
|
+
|
@@ -0,0 +1,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
|
+
assgs << a if assignment? a
|
22
|
+
end
|
23
|
+
assgs
|
24
|
+
end
|
25
|
+
|
26
|
+
def assignment?(option)
|
27
|
+
option.include?('=')\
|
28
|
+
&& !option.start_with?('=')\
|
29
|
+
&& !option.end_with?('=')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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_underscore(name).to_sym
|
23
|
+
end
|
24
|
+
|
25
|
+
# Parses arguments and returns results of parsing.
|
26
|
+
def parse(args)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Returns name of current class.
|
32
|
+
def class_name
|
33
|
+
self.class.name.split('::').last || ''
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns underscored version of string
|
37
|
+
# (snake case format).
|
38
|
+
def to_underscore(str)
|
39
|
+
str.gsub(/::/, '/').
|
40
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
41
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
42
|
+
tr("-", "_").
|
43
|
+
downcase
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +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
|
@@ -0,0 +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
|
data/lib/raph/version.rb
ADDED
data/raph.gemspec
ADDED
@@ -0,0 +1,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 = 'Simple argument parser.'
|
13
|
+
spec.homepage = 'https://github.com/veelenga/raph'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($RS)
|
17
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(/^spec\//)
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 1.9.3'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
end
|
@@ -0,0 +1,46 @@
|
|
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
|
+
expect(subject.parse(['-h', '-on=20', '--two=my-file'])).
|
15
|
+
to match_array(['-on=20', '--two=my-file'])
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'has no to return assignments here' do
|
19
|
+
expect(subject.parse(['-h', 'file', '=', '123'])).
|
20
|
+
to match_array([])
|
21
|
+
expect(subject.parse([])).to match_array([])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#assignment?' do
|
26
|
+
def ass?(option)
|
27
|
+
subject.assignment? option
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'is an assignment' do
|
31
|
+
expect(ass? 'h=one').to be true
|
32
|
+
expect(ass? '-ass=two').to be true
|
33
|
+
expect(ass? '--ass=two').to be true
|
34
|
+
expect(ass? 'ass=one=two').to be true
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'is not as assignment' do
|
38
|
+
expect(ass? '-h').to be false
|
39
|
+
expect(ass? '-h=').to be false
|
40
|
+
expect(ass? 'h=').to be false
|
41
|
+
expect(ass? '=').to be false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +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
|
@@ -0,0 +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
|
@@ -0,0 +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
|
+
!subject.flag?(option).nil?
|
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
|
data/spec/raph_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
module Raph
|
5
|
+
describe Raph do
|
6
|
+
|
7
|
+
class NumberParser
|
8
|
+
attr_accessor :id, :max
|
9
|
+
|
10
|
+
def initialize(id, max=100)
|
11
|
+
@id = id
|
12
|
+
@max = max
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse(args)
|
16
|
+
args.select { |x| x <= max }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#parse' do
|
21
|
+
let (:numbers) { (1..100) }
|
22
|
+
let (:parser1) { NumberParser.new(:first, 5) }
|
23
|
+
let (:parser2) { NumberParser.new(:second, 6) }
|
24
|
+
|
25
|
+
it 'should parse nothing if parsers not added' do
|
26
|
+
raph = Raph.new.tap { |r| r.parse numbers }
|
27
|
+
expect(raph.all).to match_array numbers
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should have correspoding attribute if parser added' do
|
31
|
+
raph = Raph.new.tap do |r|
|
32
|
+
r.add_parser(parser1)
|
33
|
+
r.add_parser(parser2)
|
34
|
+
r.parse(numbers)
|
35
|
+
end
|
36
|
+
expect(raph.all).to match_array numbers
|
37
|
+
expect(raph.send(parser1.id)).to match_array (1..parser1.max)
|
38
|
+
expect(raph.send(parser2.id)).to match_array (1..parser2.max)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when we include Raph' do
|
44
|
+
it 'loads global variable' do
|
45
|
+
expect($raph).to be_a Raph
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'has proper attributes' do
|
49
|
+
raph = $raph
|
50
|
+
expect(raph.all).not_to be nil
|
51
|
+
expect(raph.send(FlagParser.new.id)).not_to be nil
|
52
|
+
expect(raph.send(FileParser.new.id)).not_to be nil
|
53
|
+
expect(raph.send(AssignmentParser.new.id)).not_to be nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'raph'
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: raph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vitalii Elenhaupt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Simple argument parser.
|
56
|
+
email:
|
57
|
+
- velenhaupt@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- example/sample.rb
|
70
|
+
- lib/raph.rb
|
71
|
+
- lib/raph/parser/assignment_parser.rb
|
72
|
+
- lib/raph/parser/base_parser.rb
|
73
|
+
- lib/raph/parser/file_parser.rb
|
74
|
+
- lib/raph/parser/flag_parser.rb
|
75
|
+
- lib/raph/version.rb
|
76
|
+
- raph.gemspec
|
77
|
+
- spec/raph/parser/assinment_parser_spec.rb
|
78
|
+
- spec/raph/parser/base_parser_spec.rb
|
79
|
+
- spec/raph/parser/file_parser_spec.rb
|
80
|
+
- spec/raph/parser/flag_parser_spec.rb
|
81
|
+
- spec/raph_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage: https://github.com/veelenga/raph
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 1.9.3
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.4.5
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Ruby Argument Parsing for Humans.
|
107
|
+
test_files:
|
108
|
+
- spec/raph/parser/assinment_parser_spec.rb
|
109
|
+
- spec/raph/parser/base_parser_spec.rb
|
110
|
+
- spec/raph/parser/file_parser_spec.rb
|
111
|
+
- spec/raph/parser/flag_parser_spec.rb
|
112
|
+
- spec/raph_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
has_rdoc:
|