yaop 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (9) hide show
  1. data/.document +5 -0
  2. data/Gemfile +12 -0
  3. data/Gemfile.lock +24 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.md +50 -0
  6. data/Rakefile +27 -0
  7. data/VERSION +1 -0
  8. data/lib/yaop.rb +202 -0
  9. metadata +108 -0
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "hash-utils", ">= 0.1.0"
5
+ gem "types", ">= 0.1.0"
6
+
7
+ # Add dependencies to develop your gem here.
8
+ # Include everything needed to run rake, tests, features, etc.
9
+ group :development do
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ end
@@ -0,0 +1,24 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ abstract (1.0.0)
5
+ git (1.2.5)
6
+ hash-utils (0.15.0)
7
+ jeweler (1.5.2)
8
+ bundler (~> 1.0.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ multitype-introspection (0.1.2)
12
+ rake (0.9.0)
13
+ types (0.1.3)
14
+ abstract (>= 1.0.0)
15
+ multitype-introspection (>= 0.1.0)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.0.0)
22
+ hash-utils (>= 0.1.0)
23
+ jeweler (~> 1.5.2)
24
+ types (>= 0.1.0)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ YAOP -- Yet Another Options Parser
2
+ ==================================
3
+
4
+ **YAOP** parses the command-line for both named (*arguments*) and
5
+ unnamed (*parameters*) options and returns them in hash and array.
6
+ It's intended to be feature-less, simple and lightweight one. It uses
7
+ declarative approach. See example:
8
+
9
+ require "yaop"
10
+
11
+ options = YAOP::get do
12
+ option "--strip"
13
+ type Boolean, false # simple presency is enough
14
+
15
+ options "-l", "--level" # both of them
16
+ type Integer, 7 # first part, default value 7
17
+ type Integer, 8 # second part, default value 8
18
+ type Integer, 9 # third part, default value 9
19
+ end
20
+
21
+ If command line will be `script.rb -l 1 2 --strip "file1.txt" "file2.txt"`,
22
+ result will be:
23
+
24
+ p options.arguments
25
+ # will print { "--strip" => true, "-l" => [1, 2, 9], "--level" => [1, 2, 9] }
26
+
27
+ p options.parameters
28
+ # will print ["file1.txt", "file2.txt"]
29
+
30
+ Be warn, classic syntax like `-sl 1` isn't supported.
31
+
32
+
33
+ Contributing
34
+ ------------
35
+
36
+ 1. Fork it.
37
+ 2. Create a branch (`git checkout -b 20101220-my-change`).
38
+ 3. Commit your changes (`git commit -am "Added something"`).
39
+ 4. Push to the branch (`git push origin 20101220-my-change`).
40
+ 5. Create an [Issue][2] with a link to your branch.
41
+ 6. Enjoy a refreshing Diet Coke and wait.
42
+
43
+ Copyright
44
+ ---------
45
+
46
+ Copyright © 2011 [Martin Kozák][3]. See `LICENSE.txt` for
47
+ further details.
48
+
49
+ [2]: http://github.com/martinkozak/yaop/issues
50
+ [3]: http://www.martinkozak.net/
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+
14
+ require 'rake'
15
+ require 'jeweler'
16
+
17
+ Jeweler::Tasks.new do |gem|
18
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
19
+
20
+ gem.name = "yaop"
21
+ gem.homepage = "https://github.com/martinkozak/yaop"
22
+ gem.license = "MIT"
23
+ gem.summary = "Yet another options parser. Parses the command-line arguments and parameters. Simple, lightweight with nice declarative approach."
24
+ gem.email = "martinkozak@martinkozak.net"
25
+ gem.authors = ["Martin Kozák"]
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,202 @@
1
+ # encoding: utf-8
2
+ # (c) 2011 Martin Kozák (martinkozak@martinkozak.net)
3
+
4
+ require "types"
5
+ require "hash-utils/hash" # >= 0.1.0
6
+
7
+ ##
8
+ # Yet anothet options parser. Very rubyfied declarative
9
+ # definitions support.
10
+ #
11
+
12
+ class YAOP
13
+
14
+ ##
15
+ # Holds options currently set for parsing.
16
+ #
17
+
18
+ @options
19
+
20
+ ##
21
+ # Stack of option in current settings. Although maybe its holder
22
+ # rather than stack.
23
+ #
24
+
25
+ @stack
26
+
27
+ ##
28
+ # Result struct.
29
+ #
30
+
31
+ Result = Struct::new(:arguments, :parameters)
32
+
33
+ ##
34
+ # Returns command line options.
35
+ #
36
+ # @param [Proc] block arguments declaration
37
+ # @return [OptionsHash] hash with options
38
+ #
39
+
40
+ def self.get(&block)
41
+ parser = YAOP::new
42
+ parser.instance_eval(&block)
43
+ return parser.result
44
+ end
45
+
46
+ ##
47
+ # Constructor.
48
+ #
49
+
50
+ def initialize
51
+ @options = [ ]
52
+ @stack = nil
53
+ end
54
+
55
+ ##
56
+ # Sets option names. Assigning of the new option names will cause
57
+ # setting the current one to assigned options.
58
+ #
59
+ # @param [*String] args option names including leading character
60
+ #
61
+
62
+ def option(*args)
63
+ if not @stack.nil?
64
+ @options << @stack
65
+ end
66
+
67
+ __reset_stack
68
+ @stack.options = args.map { |i| i.to_s.to_sym }
69
+ end
70
+
71
+ alias :options :option
72
+
73
+ ##
74
+ # Defines type and default value specification for current option.
75
+ #
76
+ # Can be used multiple times as indication, argument receives more
77
+ # values treated as separated Ruby arguments.
78
+ #
79
+ # @param [Class] type type which can be +Boolean+, +String+ or +Integer+
80
+ # @default [Object] the default value
81
+ #
82
+
83
+ def type(type, default = nil)
84
+ @stack.types << [type, default]
85
+ end
86
+
87
+ ##
88
+ # Returns list of arguments and parameters according to definition.
89
+ # @return [YAOP::Result] struct with arguments and parameters
90
+ #
91
+
92
+ def result
93
+
94
+ # Commits last stack
95
+ if not @stack.nil?
96
+ @options << @stack
97
+ @stack = nil
98
+ end
99
+
100
+ # Creates options index
101
+ index = { }
102
+ @options.each do |i|
103
+ i.options.each do |id|
104
+ index[id] = i
105
+ end
106
+ end
107
+
108
+ # Parses
109
+ last = nil # note: it's used below
110
+ params = [ ]
111
+
112
+ ARGV.each do |arg|
113
+ if index.has_key? arg.to_sym
114
+ last = arg.to_sym
115
+ index[last].present = true
116
+ elsif not last.nil?
117
+ index[last].values << arg
118
+ else
119
+ params << arg
120
+ end
121
+ end
122
+
123
+ # Converts datatypes
124
+ result = Hash::new { |dict, key| dict[key] = [ ] }
125
+
126
+ index.each_pair do |opt, spec|
127
+ spec.types.each_index do |i|
128
+ type, default = spec.types[i]
129
+ value = nil
130
+
131
+ case type.hash
132
+ when Integer.hash
133
+ value = i < spec.values.length ? spec.values[i].to_i : default
134
+ when String.hash
135
+ value = i < spec.values.length ? spec.values[i].to_s : default
136
+ when Boolean.hash
137
+ value = spec.present ? true : default
138
+ end
139
+
140
+ if not value.nil?
141
+ result[opt] << value
142
+ end
143
+ end
144
+ end
145
+
146
+ # Converts single value arguments to single-ones
147
+ result.each_pair do |opt, values|
148
+ if index[opt].types.length == 1
149
+ result[opt] = values.first
150
+ end
151
+ end
152
+
153
+ # Takes parameters from last argument
154
+ if not last.nil?
155
+ parameters = index[last].values[index[last].types.length..-1]
156
+ else
157
+ parameters = params
158
+ end
159
+
160
+ parameters = [ ] if parameters.nil?
161
+
162
+
163
+ return self.class::Result::new(
164
+ result.map_keys! { |k| k.to_s }, # converts keys from symbols back to string
165
+ parameters
166
+ )
167
+
168
+ end
169
+
170
+ ##
171
+ # Resets stack.
172
+ #
173
+
174
+ private
175
+ def __reset_stack
176
+ @stack = __struct::new([], [], [], false)
177
+ end
178
+
179
+ ##
180
+ # Creates and returns stack struct.
181
+ #
182
+
183
+ private
184
+ def __struct
185
+ if @struct.nil?
186
+ @struct = Struct::new(:options, :types, :values, :present)
187
+ end
188
+
189
+ @struct
190
+ end
191
+ end
192
+
193
+ =begin
194
+ p YAOP::get {
195
+ option "-s", "--strip"
196
+ type Boolean, false
197
+
198
+ option "-l", "--level"
199
+ type Integer, 7
200
+ type Integer, 8
201
+ }
202
+ =end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaop
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - "Martin Koz\xC3\xA1k"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-29 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hash-utils
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: types
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.5.2
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ description:
60
+ email: martinkozak@martinkozak.net
61
+ executables: []
62
+
63
+ extensions: []
64
+
65
+ extra_rdoc_files:
66
+ - LICENSE.txt
67
+ - README.md
68
+ files:
69
+ - .document
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - VERSION
76
+ - lib/yaop.rb
77
+ homepage: https://github.com/martinkozak/yaop
78
+ licenses:
79
+ - MIT
80
+ post_install_message:
81
+ rdoc_options: []
82
+
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 1846092196582087239
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.4
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Yet another options parser. Parses the command-line arguments and parameters. Simple, lightweight with nice declarative approach.
107
+ test_files: []
108
+