optimus 0.0.1

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.
data/lib/optimus.rb ADDED
@@ -0,0 +1,21 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ #
3
+ # This file is part of optimus.
4
+ #
5
+ # optimus is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'optimus/optimus'
19
+ require 'optimus/implementations'
20
+
21
+ require 'optimus/version'
@@ -0,0 +1,30 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class Optimus
20
+
21
+ class Implementation
22
+ attr_reader :parser, :interface
23
+
24
+ def initialize (parser, interface)
25
+ @parser = parser
26
+ @interface = interface
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ #
3
+ # This file is part of optimus.
4
+ #
5
+ # optimus is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'optimus/implementations/standard'
@@ -0,0 +1,32 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class Optimus
20
+
21
+ module Parsers
22
+
23
+ class AdvancedParser
24
+
25
+ end
26
+
27
+ Advanced = AdvancedParser.new
28
+
29
+ end
30
+
31
+ end
32
+ # /--(?<commmand>\w+)(=(?<argument>.*))?/
@@ -0,0 +1,197 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'forwardable'
20
+ require 'date'
21
+
22
+ require 'optimus/option'
23
+ require 'optimus/options'
24
+
25
+ require 'optimus/implementation'
26
+ require 'optimus/interface'
27
+ require 'optimus/parser'
28
+
29
+ class Optimus
30
+
31
+ module Implementations
32
+
33
+ class Standard < Implementation
34
+ class Parser < Optimus::Parser
35
+ def initialize (implementation, options=nil)
36
+ super(implementation, options)
37
+
38
+ if !@options[:separators]
39
+ @options[:separators] = {}
40
+ end
41
+
42
+ if !@options[:separators][:long]
43
+ @options[:separators][:long] = '--'
44
+ end
45
+
46
+ if !@options[:separators][:short]
47
+ @options[:separators][:short] = '-'
48
+ end
49
+ end
50
+
51
+ def parse (result, values, options=nil)
52
+ options = @options.merge(options) if options.is_a? Hash
53
+ active = nil
54
+ type = nil
55
+
56
+ values.each {|value|
57
+ if active
58
+ if value.match(/^#{Regexp.escape(@options[:separators][:long])}|#{Regexp.escape(@options[:separators][:short])}\w/)
59
+ result.data[:parameters][active] = { :type => type, :value => true }
60
+ else
61
+ result.data[:parameters][active] = { :type => type, :value => value }
62
+ end
63
+
64
+ active = nil
65
+ else
66
+ if matches = value.match(/^#{Regexp.escape(@options[:separators][:long])}(\w+)$/)
67
+ active = matches[1]
68
+ type = :long
69
+ elsif matches = value.match(/^#{Regexp.escape(@options[:separators][:short])}(\w+)$/)
70
+ if matches[1].length == 1
71
+ active = matches[1]
72
+ type = :short
73
+ else
74
+ matches[1].chars.each {|char|
75
+ result.data[:parameters][char] = { :type => :short, :value => true }
76
+ }
77
+ end
78
+ else
79
+ result.data[:arguments] << value
80
+ end
81
+ end
82
+ }
83
+
84
+ result.normalize
85
+ result
86
+ end
87
+ end
88
+
89
+ class Interface < Optimus::Interface
90
+ attr_reader :parameters, :arguments, :options, :data
91
+
92
+ alias params parameters
93
+ alias args arguments
94
+
95
+ extend Forwardable
96
+
97
+ def_delegators :@options, :each_value, :merge!
98
+
99
+ alias each each_value
100
+
101
+ def initialize (implementation)
102
+ super(implementation)
103
+
104
+ @options = {}
105
+
106
+ @data = {
107
+ :parameters => {},
108
+ :arguments => []
109
+ }
110
+
111
+ @internal = {}
112
+ end
113
+
114
+ def merge (hash)
115
+ result = self.clone
116
+ result.merge! hash
117
+ result
118
+ end
119
+
120
+ def set (arguments)
121
+ if !arguments[:long]
122
+ raise 'You have to pass at least :long.'
123
+ end
124
+
125
+ arguments[:long] = arguments[:long].to_sym
126
+ arguments[:short] = arguments[:short].to_sym if arguments[:short]
127
+
128
+ option = Option.new(arguments)
129
+ option.name = arguments[:long]
130
+
131
+ @options[option.name] = option
132
+ end
133
+
134
+ def get (name)
135
+ @options[name]
136
+ end
137
+
138
+ def normalize
139
+ @parameters = {}
140
+ @arguments = @data[:arguments]
141
+
142
+ @options.each_value {|option|
143
+ if option.default
144
+ @parameters[option.long] = option.default
145
+ @parameters[option.short] = option.default
146
+ end
147
+ }
148
+
149
+ @data[:parameters].each {|key, value|
150
+ if value[:type] == :long
151
+ if @options[key]
152
+ @parameters[@options[key].long] = value[:value]
153
+ @parameters[@options[key].short] = value[:value]
154
+ end
155
+ else
156
+ @options.each_value {|option|
157
+ if option.short == key
158
+ @parameters[option.long] = value[:value]
159
+ @parameters[option.short] = value[:value]
160
+ end
161
+ }
162
+ end
163
+ }
164
+
165
+ @options.each_value {|option|
166
+ case option.type
167
+ when :numeric
168
+ value = @parameters[option.long]
169
+ value = (value.to_i == value.to_f) ? value.to_i : value.to_f
170
+
171
+ when :boolean
172
+ value = @parameters[option.long] == 'true'
173
+
174
+ when :date
175
+ value = Date.parse(@parameters[option.long])
176
+
177
+ when :array
178
+ value = @parameters[option.long].split(/,/)
179
+
180
+ when :hash
181
+ value = Hash[@parameters[option.long].split(/,/).split(/=/)]
182
+ end
183
+
184
+ @parameters[option.long] = value
185
+ @parameters[option.short] = value
186
+ }
187
+ end
188
+ end
189
+
190
+ def initialize (parserOptions=nil)
191
+ super(Parser.new(self, parserOptions), Interface.new(self))
192
+ end
193
+ end
194
+
195
+ end
196
+
197
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class Optimus
20
+
21
+ # This is only an abstract class to implement Optimus interfaces.
22
+ #
23
+ # Required methods are: LOL I DUNNO
24
+ class Interface
25
+ attr_reader :implementation
26
+
27
+ def initialize (implementation)
28
+ @implementation = implementation
29
+ end
30
+
31
+ def inspect
32
+ "#<Interface>"
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,59 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'optimus/options'
20
+
21
+ class Optimus
22
+ attr_reader :options, :implementation
23
+
24
+ # Initialize Optimus with an array of options or ARGV
25
+ def initialize (*args)
26
+ if !block_given?
27
+ options = args.shift || []
28
+ end
29
+
30
+ values = args.shift || ARGV
31
+ implementation = args.shift || Implementations::Standard.new
32
+
33
+ if block_given?
34
+ yield @options = Options.new(implementation)
35
+ @options.parse(values)
36
+ else
37
+ @options = Options.parse(options, values, implementation)
38
+ end
39
+
40
+ @implementation = implementation
41
+ end
42
+
43
+ # Parse additional options and merge them to the Optimus object
44
+ def parse (values, parserOptions=nil)
45
+ options = Options.parse(@options.options, values, @implementation, parserOptions)
46
+
47
+ if block_given?
48
+ yield options
49
+ end
50
+
51
+ @options.merge!(options)
52
+
53
+ return options
54
+ end
55
+
56
+ def method_missing (method, *args, &block)
57
+ @options.send(method, *args, &block)
58
+ end
59
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'ostruct'
20
+
21
+ class Optimus
22
+ class Option < OpenStruct
23
+ attr_accessor :name
24
+
25
+ def initialize (arguments)
26
+ if !arguments.is_a? Hash
27
+ raise 'You have to pass a Hash'
28
+ end
29
+
30
+ super(arguments)
31
+ end
32
+
33
+ alias to_hash marshal_dump
34
+ alias to_h marshal_dump
35
+ end
36
+ end
@@ -0,0 +1,48 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ require 'optimus/option'
20
+
21
+ class Optimus
22
+ class Options
23
+ attr_reader :implementation
24
+
25
+ def self.parse (options, values, implementation, parserOptions=nil)
26
+ result = Options.new(implementation)
27
+
28
+ options.each {|option|
29
+ result.set(option)
30
+ }
31
+
32
+ implementation.parser.parse(result, values, parserOptions)
33
+ end
34
+
35
+ def initialize (implementation)
36
+ @implementation = implementation
37
+ end
38
+
39
+ def parse (values, options=nil)
40
+ @implementation.parser.parse(self, values, options)
41
+ end
42
+
43
+ def method_missing (method, *args, &block)
44
+ @implementation.interface.send(method, *args, &block)
45
+ end
46
+ end
47
+ end
48
+
@@ -0,0 +1,39 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class Optimus
20
+
21
+ class Parser
22
+ attr_reader :implementation, :options
23
+
24
+ def initialize (implementation, options=nil)
25
+ @implementation = implementation
26
+
27
+ if !options.is_a? Hash
28
+ @options = {}
29
+ else
30
+ @options = options
31
+ end
32
+ end
33
+
34
+ def inspect
35
+ "#<Parser:#{@options.inspect}>"
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,22 @@
1
+ # Copyright (C) 2010 tilde [tilde AT autistici DOT org]
2
+ # meh [meh.ffff AT gmail DOT com]
3
+ #
4
+ # This file is part of optimus.
5
+ #
6
+ # optimus is free software: you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation, either version 3 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with optimus. If not, see <http://www.gnu.org/licenses/>.
18
+
19
+ class Optimus
20
+ Version = '0.0.1'
21
+ end
22
+
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optimus
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - tilde
13
+ - meh.
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-27 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: An automated yet fully customizable command line options parser.
23
+ email:
24
+ - tilde@autistici.org
25
+ - meh.ffff@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/optimus.rb
34
+ - lib/optimus/optimus.rb
35
+ - lib/optimus/parser.rb
36
+ - lib/optimus/options.rb
37
+ - lib/optimus/interface.rb
38
+ - lib/optimus/implementations.rb
39
+ - lib/optimus/version.rb
40
+ - lib/optimus/implementation.rb
41
+ - lib/optimus/implementations/standard.rb
42
+ - lib/optimus/implementations/advanced.rb
43
+ - lib/optimus/option.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/tilde/optimus
46
+ licenses: []
47
+
48
+ post_install_message:
49
+ rdoc_options: []
50
+
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A command line opt parser.
76
+ test_files: []
77
+