rprogram 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ module RProgram
4
4
  # +false+ if they are not enabled.
5
5
  #
6
6
  def RProgram.debug
7
- @@debug ||= false
7
+ @@rprogram_debug ||= false
8
8
  end
9
9
 
10
10
  #
@@ -12,6 +12,6 @@ module RProgram
12
12
  # specified _value_.
13
13
  #
14
14
  def RProgram.debug=(value)
15
- @@debug = value
15
+ @@rprogram_debug = value
16
16
  end
17
17
  end
@@ -49,7 +49,7 @@ module RProgram
49
49
  @options.each do |name,value|
50
50
  non_opt = get_non_option(name)
51
51
 
52
- if (non_opt && non_opt.leading)
52
+ if (non_opt && non_opt.leading?)
53
53
  args += non_opt.arguments(value)
54
54
  end
55
55
  end
@@ -99,7 +99,7 @@ module RProgram
99
99
  @options.each do |name,value|
100
100
  non_opt = get_non_option(name)
101
101
 
102
- if (non_opt && non_opt.tailing)
102
+ if (non_opt && non_opt.tailing?)
103
103
  args += non_opt.arguments(value)
104
104
  end
105
105
  end
@@ -123,6 +123,8 @@ module RProgram
123
123
  # subtask :extra, ExtraTask
124
124
  #
125
125
  def self.subtask(name,task)
126
+ name = name.to_s
127
+
126
128
  class_eval %{
127
129
  def #{name}(options={},&block)
128
130
  if @subtasks[#{name.dump}]
@@ -139,19 +141,19 @@ module RProgram
139
141
  end
140
142
 
141
143
  #
142
- # Defines a non-option with the given _opts_.
144
+ # Defines a non-option with the given _options_.
143
145
  #
144
146
  # non_option :name => 'input_file', :tailing => true
145
147
  #
146
148
  # non_option :name => 'file', :tailing => true, :multiple => true
147
149
  #
148
- def self.non_option(opts={})
149
- name = opts[:name].to_sym
150
+ def self.non_option(options={})
151
+ name = options[:name].to_sym
150
152
 
151
- self.non_options[name] = NonOption.new(opts)
153
+ self.non_options[name] = NonOption.new(options)
152
154
 
153
155
  class_def(name) do
154
- if opts[:multiple]
156
+ if options[:multiple]
155
157
  @options[name] ||= []
156
158
  else
157
159
  @options[name]
@@ -164,15 +166,15 @@ module RProgram
164
166
  end
165
167
 
166
168
  #
167
- # Defines a long-option with the specified _opts_.
169
+ # Defines a long-option with the specified _options_.
168
170
  #
169
- # _opts_ must contain the following keys:
171
+ # _options_ must contain the following keys:
170
172
  # <tt>:flag</tt>:: The flag to use for the option.
171
173
  #
172
- # _opts_ may also contain the following keys:
174
+ # _options_ may also contain the following keys:
173
175
  # <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
+ # flag_namify'ed form of <tt>options[:flag]</tt>,
177
+ # if not given.
176
178
  # <tt>:multiply</tt>:: Specifies that the option may appear multiple
177
179
  # times in the arguments.
178
180
  # <tt>:sub_options</tt>:: Specifies that the option contains multiple
@@ -182,20 +184,22 @@ module RProgram
182
184
  #
183
185
  # long_option :flag => '-f', :name => :file
184
186
  #
185
- def self.long_option(opts={},&block)
186
- opts[:name] ||= Task.flag_namify(opts[:flag])
187
+ def self.long_option(options={},&block)
188
+ if (options[:name].nil? && options[:flag])
189
+ options[:name] = Task.flag_namify(options[:flag])
190
+ end
187
191
 
188
- define_option(opts,&block)
192
+ return define_option(options,&block)
189
193
  end
190
194
 
191
195
  #
192
- # Defines a short_option with the specified _opts_.
196
+ # Defines a short_option with the specified _options_.
193
197
  #
194
- # _opts_ must contain the following keys:
198
+ # _options_ must contain the following keys:
195
199
  # <tt>:name</tt>:: The name of the option.
196
200
  # <tt>:flag</tt>:: The flag to use for the option.
197
201
  #
198
- # _opts_ may also contain the following keys:
202
+ # _options_ may also contain the following keys:
199
203
  # <tt>:multiply</tt>:: Specifies that the option may appear multiple
200
204
  # times in the arguments.
201
205
  # <tt>:sub_options</tt>:: Specifies that the option contains multiple
@@ -203,32 +207,32 @@ module RProgram
203
207
  #
204
208
  # short_option :flag => '-c', :name => :count
205
209
  #
206
- def self.short_option(opts,&block)
207
- define_option(opts,&block)
210
+ def self.short_option(options,&block)
211
+ define_option(options,&block)
208
212
  end
209
213
 
210
214
  #
211
- # Defines an option with the specified _opts_ and the given _block_.
215
+ # Defines an option with the specified _options_ and the given _block_.
212
216
  #
213
- # _opts_ must contain the following keys:
217
+ # _options_ must contain the following keys:
214
218
  # <tt>:name</tt>:: The name of the option.
215
219
  # <tt>:flag</tt>:: The flag to use for the option.
216
220
  #
217
- # _opts_ may also contain the following keys:
221
+ # _options_ may also contain the following keys:
218
222
  # <tt>:multiply</tt>:: Specifies that the option may appear multiple
219
223
  # times in the arguments.
220
224
  # <tt>:sub_options</tt>:: Specifies that the option contains multiple
221
225
  # sub-options.
222
226
  #
223
- def self.define_option(opts,&block)
224
- method_name = opts[:name].to_sym
227
+ def self.define_option(options,&block)
228
+ method_name = options[:name].to_sym
225
229
 
226
- self.options[method_name] = Option.new(opts,&block)
230
+ self.options[method_name] = Option.new(options,&block)
227
231
 
228
232
  class_def(method_name) do
229
- if opts[:sub_options]
233
+ if options[:sub_options]
230
234
  @options[method_name] ||= OptionList.new
231
- elsif opts[:multiple]
235
+ elsif options[:multiple]
232
236
  @options[method_name] ||= []
233
237
  else
234
238
  @options[method_name]
@@ -236,7 +240,7 @@ module RProgram
236
240
  end
237
241
 
238
242
  class_def("#{method_name}=") do |value|
239
- if opts[:sub_options]
243
+ if options[:sub_options]
240
244
  @options[method_name] = OptionList.new(value)
241
245
  else
242
246
  @options[method_name] = value
@@ -250,19 +254,19 @@ module RProgram
250
254
  # Task.flag_namify('--output-file') # => "output_file"
251
255
  #
252
256
  def Task.flag_namify(flag)
253
- flag = flag.to_s
257
+ flag = flag.to_s.downcase
254
258
 
255
259
  # remove leading dashes
256
- if flag =~ /^--/
260
+ if flag[0..1] == '--'
257
261
  method_name = flag[2..-1]
258
- elsif flag =~ /^-/
262
+ elsif flag[0..0] == '-'
259
263
  method_name = flag[1..-1]
260
264
  else
261
265
  method_name = flag
262
266
  end
263
267
 
264
268
  # replace remaining dashes with underscores
265
- return method_name.gsub(/[-.]/,'_')
269
+ return method_name.gsub(/[-_\.\s]+/,'_')
266
270
  end
267
271
 
268
272
  end
@@ -1,3 +1,3 @@
1
1
  module RProgram
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'rprogram/nameable'
2
+
3
+ class AliasedProgram
4
+
5
+ include RProgram::Nameable
6
+
7
+ name_program 'ls'
8
+ alias_program 'dir'
9
+
10
+ end
@@ -0,0 +1,8 @@
1
+ require 'rprogram/program'
2
+
3
+ class LS < RProgram::Program
4
+
5
+ name_program 'ls'
6
+ alias_program 'dir'
7
+
8
+ end
@@ -0,0 +1,7 @@
1
+ require 'classes/ls_task'
2
+
3
+ class LSSELinuxTask < LSTask
4
+
5
+ long_option :flag => '--lcontext', :name => :security_context
6
+
7
+ end
@@ -0,0 +1,12 @@
1
+ require 'rprogram/task'
2
+
3
+ class LSTask < RProgram::Task
4
+
5
+ short_option :flag => '-a', :name => :all
6
+ long_option :flag => '--author'
7
+ long_option :flag => '--group-directories-first', :name => :group_dirs_first
8
+ long_option :flag => '--hide', :multiple => true
9
+
10
+ non_option :tailing => true, :multiple => true, :name => :files
11
+
12
+ end
@@ -0,0 +1,9 @@
1
+ require 'rprogram/nameable'
2
+
3
+ class NamedProgram
4
+
5
+ include RProgram::Nameable
6
+
7
+ name_program 'ls'
8
+
9
+ end
@@ -0,0 +1,21 @@
1
+ require 'rprogram/compat'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Compat do
6
+ it "should have a list of directories that contain programs" do
7
+ Compat.paths.should_not be_empty
8
+
9
+ Compat.paths.any? { |dir|
10
+ File.directory?(dir)
11
+ }.should == true
12
+ end
13
+
14
+ it "should be able to find programs" do
15
+ File.executable?(Compat.find_program('dir')).should == true
16
+ end
17
+
18
+ it "should be able to find programs by multiple names" do
19
+ File.executable?(Compat.find_program_by_names('ls','dir')).should == true
20
+ end
21
+ end
@@ -0,0 +1,55 @@
1
+ require 'rprogram/nameable'
2
+
3
+ require 'spec_helper'
4
+ require 'classes/named_program'
5
+ require 'classes/aliased_program'
6
+
7
+ describe Nameable do
8
+ describe "named program" do
9
+ it "should be able to give a class a program name" do
10
+ NamedProgram.program_name.should == 'ls'
11
+ end
12
+
13
+ it "should not have any program aliases" do
14
+ NamedProgram.program_aliases.should be_empty
15
+ end
16
+
17
+ it "should have one program name" do
18
+ NamedProgram.program_names.should == ['ls']
19
+ end
20
+
21
+ it "should provide an instance method for the program name" do
22
+ obj = NamedProgram.new
23
+
24
+ obj.program_name.should == 'ls'
25
+ end
26
+
27
+ it "should provide an instance method for the program names" do
28
+ obj = NamedProgram.new
29
+
30
+ obj.program_names.should == ['ls']
31
+ end
32
+ end
33
+
34
+ describe "aliased program" do
35
+ it "should have program aliases" do
36
+ AliasedProgram.program_aliases.should == ['dir']
37
+ end
38
+
39
+ it "should have one program name" do
40
+ AliasedProgram.program_names.should == ['ls', 'dir']
41
+ end
42
+
43
+ it "should provide an instance method for the program aliases" do
44
+ obj = AliasedProgram.new
45
+
46
+ obj.program_aliases.should == ['dir']
47
+ end
48
+
49
+ it "should provide an instance method for the program names" do
50
+ obj = AliasedProgram.new
51
+
52
+ obj.program_names.should == ['ls', 'dir']
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,48 @@
1
+ require 'rprogram/non_option'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe NonOption do
6
+ before(:all) do
7
+ @non_option = NonOption.new(:name => 'files')
8
+ end
9
+
10
+ it "should keep :leading and :tailing options mutually exclusive" do
11
+ leading = NonOption.new(:name => 'files', :leading => true)
12
+ tailing = NonOption.new(:name => 'files', :tailing => true)
13
+
14
+ leading.should be_leading
15
+ leading.should_not be_tailing
16
+
17
+ tailing.should_not be_leading
18
+ tailing.should be_tailing
19
+ end
20
+
21
+ it "should return an empty Array when passed nil" do
22
+ @non_option.arguments(nil).should == []
23
+ end
24
+
25
+ it "should return an empty Array when passed false" do
26
+ @non_option.arguments(false).should == []
27
+ end
28
+
29
+ it "should return an empty Array when passed []" do
30
+ @non_option.arguments([]).should == []
31
+ end
32
+
33
+ it "should return an Array when passed a single value" do
34
+ @non_option.arguments('foo').should == ['foo']
35
+ end
36
+
37
+ it "should return an Array when passed multiple values" do
38
+ @non_option.arguments(['foo', 'bar']).should == ['foo', 'bar']
39
+ end
40
+
41
+ it "should return an Array when passed a Hash of keys" do
42
+ @non_option.arguments({:foo => true, :bar => false}).should == ['foo']
43
+ end
44
+
45
+ it "should return an Array when passed a Hash of values" do
46
+ @non_option.arguments({:foo => 'bar'}).should == ['foo=bar']
47
+ end
48
+ end
@@ -0,0 +1,17 @@
1
+ require 'rprogram/option'
2
+
3
+ require 'spec_helper'
4
+
5
+ shared_examples_for 'Option' do
6
+ it "should return an empty Array when passed nil" do
7
+ @option.arguments(nil).should == []
8
+ end
9
+
10
+ it "should return an empty Array when passed false" do
11
+ @option.arguments(false).should == []
12
+ end
13
+
14
+ it "should return a single flag when passed true" do
15
+ @option.arguments(true).should == ['-f']
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ require 'rprogram/option_list'
2
+
3
+ require 'spec_helper'
4
+
5
+ describe OptionList do
6
+ before(:each) do
7
+ @options = OptionList.new
8
+ end
9
+
10
+ it "should behave like a Hash" do
11
+ @options[:bla] = 2
12
+ @options[:bla].should == 2
13
+ end
14
+
15
+ it "should provide reader and writer methods" do
16
+ @options.bla = 5
17
+ @options.bla.should == 5
18
+ end
19
+
20
+ it "should raise a NoMethodError exception when calling other methods" do
21
+ lambda {
22
+ @options.bla(5)
23
+ }.should raise_error(NoMethodError)
24
+ end
25
+ end
@@ -0,0 +1,178 @@
1
+ require 'rprogram/option'
2
+
3
+ require 'spec_helper'
4
+ require 'option_examples'
5
+
6
+ describe Option do
7
+ describe "single flag" do
8
+ before(:all) do
9
+ @option = Option.new(:flag => '-f')
10
+ end
11
+
12
+ it_should_behave_like 'Option'
13
+
14
+ it "should render a single flag with an optional value" do
15
+ value = 'foo'
16
+
17
+ @option.arguments('foo').should == ['-f', 'foo']
18
+ end
19
+
20
+ it "should render a single flag with multiple values" do
21
+ value = ['foo','bar','baz']
22
+
23
+ @option.arguments(value).should == ['-f','foo','bar','baz']
24
+ end
25
+
26
+ it "should render a single flag with a Hash of keys" do
27
+ value = {:foo => true, :bar => false}
28
+
29
+ @option.arguments(value).should == ['-f','foo']
30
+ end
31
+
32
+ it "should render a single flag with a Hash of keys and values" do
33
+ value = {:foo => 'bar'}
34
+
35
+ @option.arguments(value).should == ['-f','foo=bar']
36
+ end
37
+ end
38
+
39
+ describe "equals flag" do
40
+ before(:all) do
41
+ @option = Option.new(:equals => true, :flag => '-f')
42
+ end
43
+
44
+ it_should_behave_like 'Option'
45
+
46
+ it "should render a single flag with a value" do
47
+ value = 'foo'
48
+
49
+ @option.arguments('foo').should == ['-f=foo']
50
+ end
51
+
52
+ it "should render a single flag with multiple values" do
53
+ value = ['foo', 'bar', 'baz']
54
+
55
+ @option.arguments(value).should == ['-f=foo bar baz']
56
+ end
57
+
58
+ it "should render a single flag with a Hash of keys" do
59
+ value = {:foo => true, :bar => false}
60
+
61
+ @option.arguments(value).should == ['-f=foo']
62
+ end
63
+ end
64
+
65
+ describe "multiple flags" do
66
+ before(:all) do
67
+ @option = Option.new(:multiple => true, :flag => '-f')
68
+ end
69
+
70
+ it_should_behave_like 'Option'
71
+
72
+ it "should render a single flag with a value" do
73
+ value = 'foo'
74
+
75
+ @option.arguments(value).should == ['-f', 'foo']
76
+ end
77
+
78
+ it "should render multiple flags for multiple values" do
79
+ value = ['foo','bar','baz']
80
+
81
+ @option.arguments(value).should == ['-f', 'foo', '-f', 'bar', '-f', 'baz']
82
+ end
83
+
84
+ it "should render multiple flags for a Hash of keys" do
85
+ value = {:foo => true, :bar => true, :baz => false}
86
+ args = @option.arguments(value)
87
+
88
+ (args & ['-f', 'foo']).should == ['-f', 'foo']
89
+ (args & ['-f', 'bar']).should == ['-f', 'bar']
90
+ end
91
+
92
+ it "should render multiple flags for a Hash of values" do
93
+ value = {:foo => 'bar'}
94
+
95
+ @option.arguments(value).should == ['-f', 'foo=bar']
96
+ end
97
+ end
98
+
99
+ describe "multiple equals flags" do
100
+ before(:all) do
101
+ @option = Option.new(:multiple => true, :equals => true, :flag => '-f')
102
+ end
103
+
104
+ it_should_behave_like 'Option'
105
+
106
+ it "should render a single flag with a value" do
107
+ value = 'foo'
108
+
109
+ @option.arguments(value).should == ['-f=foo']
110
+ end
111
+
112
+ it "should render multiple equal flags for multiple values" do
113
+ value = ['foo', 'bar']
114
+
115
+ @option.arguments(value).should == ['-f=foo', '-f=bar']
116
+ end
117
+
118
+ it "should render multiple equal flags for a Hash of keys" do
119
+ value = {:foo => true, :bar => true, :baz => false}
120
+ args = @option.arguments(value)
121
+
122
+ args.include?('-f=foo').should == true
123
+ args.include?('-f=bar').should == true
124
+ end
125
+
126
+ it "should render multiple equal flags for a Hash of values" do
127
+ value = {:foo => 'bar', :bar => 'baz'}
128
+ args = @option.arguments(value)
129
+
130
+ args.include?('-f=foo=bar').should == true
131
+ args.include?('-f=bar=baz').should == true
132
+ end
133
+ end
134
+
135
+ describe "separated values" do
136
+ before(:all) do
137
+ @option = Option.new(:separator => ',', :flag => '-f')
138
+ end
139
+
140
+ it_should_behave_like 'Option'
141
+
142
+ it "should render a single flag with a value" do
143
+ value = 'foo'
144
+
145
+ @option.arguments('foo').should == ['-f', 'foo']
146
+ end
147
+
148
+ it "should render a single flag with multiple values" do
149
+ value = ['foo', 'bar', 'baz']
150
+
151
+ @option.arguments(value).should == ['-f', 'foo,bar,baz']
152
+ end
153
+
154
+ it "should render a single flag with a Hash of keys" do
155
+ value = {:foo => true, :bar => true, :baz => false}
156
+ args = @option.arguments(value)
157
+
158
+ args[0].should == '-f'
159
+
160
+ sub_args = args[1].split(',')
161
+
162
+ sub_args.include?('foo').should == true
163
+ sub_args.include?('bar').should == true
164
+ end
165
+
166
+ it "should render a single flag with a Hash of values" do
167
+ value = {:foo => 'bar', :bar => 'baz'}
168
+ args = @option.arguments(value)
169
+
170
+ args[0].should == '-f'
171
+
172
+ sub_args = args[1].split(',')
173
+
174
+ sub_args.include?('foo=bar').should == true
175
+ sub_args.include?('bar=baz').should == true
176
+ end
177
+ end
178
+ end