rprogram 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ == 0.1.2 / 2008-01-18
2
+
3
+ * DRY'ed up lib/rprogram/task.
4
+ * Added Task.define_option.
5
+ * Added OptionList so that Option may contain sub-options.
6
+ * Touched up documenation.
7
+
1
8
  == 0.1.1 / 2008-01-18
2
9
 
3
10
  * Added support for the Option argument separators.
@@ -14,8 +21,8 @@
14
21
  == 0.1.0 / 2008-01-17
15
22
 
16
23
  * Removed redundent methods in Program:
17
- * +Program.find_by_name+
18
- * +Program.find_by_names+
24
+ * Program.find_by_name
25
+ * Program.find_by_names
19
26
  * Added Program#create.
20
27
  * Made Program Nameable by default.
21
28
  * Prevented arbitrary command-injection in Program#run.
data/Manifest.txt CHANGED
@@ -9,7 +9,10 @@ lib/rprogram/exceptions/program_not_found.rb
9
9
  lib/rprogram/exceptions.rb
10
10
  lib/rprogram/extensions/meta/object.rb
11
11
  lib/rprogram/extensions/meta.rb
12
+ lib/rprogram/extensions/array.rb
13
+ lib/rprogram/extensions/hash.rb
12
14
  lib/rprogram/extensions.rb
15
+ lib/rprogram/option_list.rb
13
16
  lib/rprogram/option.rb
14
17
  lib/rprogram/non_option.rb
15
18
  lib/rprogram/options.rb
@@ -0,0 +1,7 @@
1
+ class Array
2
+
3
+ def arguments
4
+ compact
5
+ end
6
+
7
+ end
@@ -0,0 +1,15 @@
1
+ class Hash
2
+
3
+ def arguments
4
+ map { |key,value|
5
+ if value
6
+ if value==true
7
+ key.to_s
8
+ else
9
+ "#{key}=#{value}"
10
+ end
11
+ end
12
+ }.compact
13
+ end
14
+
15
+ end
@@ -1 +1,3 @@
1
1
  require 'rprogram/extensions/meta'
2
+ require 'rprogram/extensions/array'
3
+ require 'rprogram/extensions/hash'
@@ -1,3 +1,5 @@
1
+ require 'rprogram/extensions'
2
+
1
3
  module RProgram
2
4
  class Option
3
5
 
@@ -13,21 +15,30 @@ module RProgram
13
15
  # Argument separator
14
16
  attr_reader :separator
15
17
 
18
+ # Does the option contain sub-options
19
+ attr_reader :sub_options
20
+
16
21
  #
17
22
  # Creates a new Option object with the specified _options_. If a _block_
18
23
  # is given it will be used for the custom formating of the option. If a
19
24
  # _block_ is not given, the option will use the default_format when
20
25
  # generating the arguments.
21
26
  #
22
- # _options_ may contain the following keys:
27
+ # _options_ must contain the following key:
23
28
  # <tt>:flag</tt>:: The command-line flag to use.
24
- # <tt>:equals</tt>:: Implies the option maybe formated as '--flag=value'.
25
- # Defaults to +falue+, if not given.
26
- # <tt>:multuple</tt>:: Implies the option maybe given an Array of
29
+ #
30
+ # _options_ may also contain the following keys:
31
+ # <tt>:equals</tt>:: Implies the option maybe formated as
32
+ # <tt>"--flag=value"</tt>. Defaults to +falue+, if
33
+ # not given.
34
+ # <tt>:multuple</tt>:: Specifies the option maybe given an Array of
27
35
  # values. Defaults to +false+, if not given.
28
36
  # <tt>:separator</tt>:: The separator to use for formating multiple
29
37
  # arguments into one +String+. Cannot be used
30
- # with +:multiple+.
38
+ # with <tt>:multiple</tt>.
39
+ # <tt>:sub_options</tt>:: Specifies that the option contains
40
+ # sub-options. Defaults to false, if not given.
41
+ #
31
42
  #
32
43
  def initialize(options={},&block)
33
44
  @flag = options[:flag]
@@ -35,6 +46,7 @@ module RProgram
35
46
  @equals = options[:equals] || false
36
47
  @multiple = options[:multiple] || false
37
48
  @separator = options[:separator]
49
+ @sub_options = options[:sub_options] || false
38
50
 
39
51
  @formating = block
40
52
  end
@@ -47,24 +59,21 @@ module RProgram
47
59
  return [@flag] if value==true
48
60
  return [] if (value==nil || value==false)
49
61
 
50
- if value.kind_of?(Hash)
51
- value = value.map { |name,value| "#{name}=#{value}" }
52
- elsif value.kind_of?(Array)
53
- value = value.compact
62
+ if value.respond_to?(:arguments)
63
+ value = value.arguments
54
64
  end
55
65
 
56
66
  if @multiple
57
- args = []
58
-
59
- value.each { |arg| args += format(arg) }
60
- return args
61
- else
62
- if (value.kind_of?(Array) && @separator)
63
- value = value.join(@separator)
67
+ if value.respond_to?(:map)
68
+ return value.map { |arg| format(arg) }
64
69
  end
70
+ end
65
71
 
66
- return format(value)
72
+ if (value.kind_of?(Array) && @separator)
73
+ value = value.join(@separator)
67
74
  end
75
+
76
+ return format(value)
68
77
  end
69
78
 
70
79
  protected
@@ -0,0 +1,27 @@
1
+ require 'rprogram/extensions/hash'
2
+
3
+ module RProgram
4
+ class OptionList < Hash
5
+
6
+ def initialize(options={})
7
+ super(options)
8
+ end
9
+
10
+ protected
11
+
12
+ def method_missing(sym,*args,&block)
13
+ name = sym.to_s
14
+
15
+ unless block
16
+ if (name =~ /=$/ && args.length==1)
17
+ return self[name.chop.to_sym] = args[0]
18
+ elsif args.length==0
19
+ return self[sym]
20
+ end
21
+ end
22
+
23
+ super(sym,*args,&block)
24
+ end
25
+
26
+ end
27
+ end
data/lib/rprogram/task.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rprogram/options'
2
+ require 'rprogram/option_list'
2
3
 
3
4
  module RProgram
4
5
  class Task
@@ -163,43 +164,71 @@ module RProgram
163
164
  end
164
165
 
165
166
  #
166
- # Defines a long-option with the given _opts_.
167
+ # Defines a long-option with the specified _opts_.
168
+ #
169
+ # _opts_ must contain the following keys:
170
+ # <tt>:flag</tt>:: The flag to use for the option.
171
+ #
172
+ # _opts_ may also contain the following keys:
173
+ # <tt>:name</tt>:: The name of the option. Defaults to the
174
+ # flag_namify'ed form of <tt>opts[:flag]</tt>, if not
175
+ # given.
176
+ # <tt>:multiply</tt>:: Specifies that the option may appear multiple
177
+ # times in the arguments.
178
+ # <tt>:sub_options</tt>:: Specifies that the option contains multiple
179
+ # sub-options.
167
180
  #
168
181
  # long_option :flag => '--output'
169
182
  #
170
183
  # long_option :flag => '-f', :name => :file
171
184
  #
172
185
  def self.long_option(opts={},&block)
173
- flag = opts[:flag].to_s
174
- method_name = (opts[:name] || Task.flag_namify(flag)).to_sym
175
-
176
- self.options[method_name] = Option.new(opts,&block)
186
+ opts[:name] ||= Task.flag_namify(opts[:flag])
177
187
 
178
- class_def(method_name) do
179
- if opts[:multiple]
180
- @options[method_name] ||= []
181
- else
182
- @options[method_name]
183
- end
184
- end
185
-
186
- class_def("#{method_name}=") do |value|
187
- @options[method_name] = value
188
- end
188
+ define_option(opts,&block)
189
189
  end
190
190
 
191
191
  #
192
- # Defines a short_option with the given _opts_.
192
+ # Defines a short_option with the specified _opts_.
193
+ #
194
+ # _opts_ must contain the following keys:
195
+ # <tt>:name</tt>:: The name of the option.
196
+ # <tt>:flag</tt>:: The flag to use for the option.
197
+ #
198
+ # _opts_ may also contain the following keys:
199
+ # <tt>:multiply</tt>:: Specifies that the option may appear multiple
200
+ # times in the arguments.
201
+ # <tt>:sub_options</tt>:: Specifies that the option contains multiple
202
+ # sub-options.
193
203
  #
194
204
  # short_option :flag => '-c', :name => :count
195
205
  #
196
- def self.short_option(opts={},&block)
206
+ def self.short_option(opts,&block)
207
+ define_option(opts,&block)
208
+ end
209
+
210
+ #
211
+ # Defines an option with the specified _opts_ and the given _block_.
212
+ #
213
+ # _opts_ must contain the following keys:
214
+ # <tt>:name</tt>:: The name of the option.
215
+ # <tt>:flag</tt>:: The flag to use for the option.
216
+ #
217
+ # _opts_ may also contain the following keys:
218
+ # <tt>:multiply</tt>:: Specifies that the option may appear multiple
219
+ # times in the arguments.
220
+ # <tt>:sub_options</tt>:: Specifies that the option contains multiple
221
+ # sub-options.
222
+ #
223
+ def self.define_option(opts,&block)
197
224
  method_name = opts[:name].to_sym
198
225
 
199
226
  self.options[method_name] = Option.new(opts,&block)
200
227
 
201
228
  class_def(method_name) do
202
- if opts[:multiple]
229
+ if opts[:sub_options]
230
+ @options[method_name] ||= OptionList.new
231
+ elsif opts[:multiple]
203
232
  @options[method_name] ||= []
204
233
  else
205
234
  @options[method_name]
@@ -207,7 +236,11 @@ module RProgram
207
236
  end
208
237
 
209
238
  class_def("#{method_name}=") do |value|
210
- @options[method_name] = value
239
+ if opts[:sub_options]
240
+ @options[method_name] = OptionList.new(value)
241
+ else
242
+ @options[method_name] = value
243
+ end
211
244
  end
212
245
  end
213
246
 
@@ -1,3 +1,3 @@
1
1
  module RProgram
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rprogram
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Postmodern Modulus III
@@ -43,7 +43,10 @@ files:
43
43
  - lib/rprogram/exceptions.rb
44
44
  - lib/rprogram/extensions/meta/object.rb
45
45
  - lib/rprogram/extensions/meta.rb
46
+ - lib/rprogram/extensions/array.rb
47
+ - lib/rprogram/extensions/hash.rb
46
48
  - lib/rprogram/extensions.rb
49
+ - lib/rprogram/option_list.rb
47
50
  - lib/rprogram/option.rb
48
51
  - lib/rprogram/non_option.rb
49
52
  - lib/rprogram/options.rb