rprogram 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,12 @@
1
+ === 0.1.8 / 2009-12-24
2
+
3
+ * Allow Program to run commands under sudo:
4
+ * Added Compat.sudo.
5
+ * Added Task#sudo.
6
+ * Added Task#sudo=.
7
+ * Added Task#sudo?.
8
+ * Added Program#sudo.
9
+
1
10
  === 0.1.7 / 2009-09-21
2
11
 
3
12
  * Require Hoe >= 2.3.3.
data/Manifest.txt CHANGED
@@ -25,6 +25,8 @@ lib/rprogram/yard/handlers/ruby.rb
25
25
  lib/rprogram/yard/handlers/ruby/metaclass_eval_handler.rb
26
26
  lib/rprogram/yard/handlers/ruby/legacy.rb
27
27
  lib/rprogram/yard/handlers/ruby/legacy/metaclass_eval_handler.rb
28
+ tasks/spec.rb
29
+ tasks/yard.rb
28
30
  spec/spec_helper.rb
29
31
  spec/classes/named_program.rb
30
32
  spec/classes/aliased_program.rb
data/README.txt CHANGED
@@ -15,15 +15,12 @@ system.
15
15
 
16
16
  * Uses Kernel.system for safe execution of individual programs and their
17
17
  separate command-line arguments.
18
+ * Allows running programs under +sudo+.
18
19
  * Provides cross-platform access to the PATH variable.
19
20
  * Supports leading/tailing non-options.
20
21
  * Supports long-options and short-options.
21
22
  * Supports custom formating of options.
22
23
 
23
- == REQUIREMENTS:
24
-
25
- * {YARD}[http://yard.soen.ca/] >= 0.2.3.5
26
-
27
24
  == INSTALL:
28
25
 
29
26
  $ sudo gem install rprogram
data/Rakefile CHANGED
@@ -11,12 +11,9 @@ Hoe.spec('rprogram') do
11
11
  self.developer('Postmodern', 'postmodern.mod3@gmail.com')
12
12
  self.remote_rdoc_dir = ''
13
13
 
14
- self.extra_deps = [
15
- ['yard', '>=0.2.3.5']
16
- ]
17
-
18
14
  self.extra_dev_deps = [
19
- ['rspec', '>=1.2.8']
15
+ ['rspec', '>=1.2.8'],
16
+ ['yard', '>=0.5.2']
20
17
  ]
21
18
 
22
19
  self.spec_extras = {:has_rdoc => 'yard'}
@@ -1,9 +1,12 @@
1
+ require 'rprogram/exceptions/program_not_found'
2
+
1
3
  module RProgram
2
4
  module Compat
3
5
  #
4
6
  # Determines the native platform.
5
7
  #
6
- # @return [String] The native platform.
8
+ # @return [String]
9
+ # The native platform.
7
10
  #
8
11
  # @example
9
12
  # Compat.arch #=> "linux"
@@ -15,7 +18,8 @@ module RProgram
15
18
  #
16
19
  # Determines the contents of the +PATH+ environment variable.
17
20
  #
18
- # @return [Array] The contents of the +PATH+ environment variable.
21
+ # @return [Array]
22
+ # The contents of the +PATH+ environment variable.
19
23
  #
20
24
  # @example
21
25
  # Compat.paths #=> ["/bin", "/usr/bin"]
@@ -35,9 +39,11 @@ module RProgram
35
39
  #
36
40
  # Finds the full-path of the program with the matching name.
37
41
  #
38
- # @param [String] name The name of the program to find.
42
+ # @param [String] name
43
+ # The name of the program to find.
39
44
  #
40
- # @return [String, nil] The full-path of the desired program.
45
+ # @return [String, nil]
46
+ # The full-path of the desired program.
41
47
  #
42
48
  # @example
43
49
  # Compat.find_program('as') #=> "/usr/bin/as"
@@ -55,10 +61,11 @@ module RProgram
55
61
  #
56
62
  # Finds the program matching one of the matching names.
57
63
  #
58
- # @param [Array] names The names of the program to use while
59
- # searching for the program.
64
+ # @param [Array] names
65
+ # The names of the program to use while searching for the program.
60
66
  #
61
- # @return [String, nil] The first full-path for the program.
67
+ # @return [String, nil]
68
+ # The first full-path for the program.
62
69
  #
63
70
  # @example
64
71
  # Compat.find_program_by_names("gas","as") #=> "/usr/bin/as"
@@ -66,5 +73,54 @@ module RProgram
66
73
  def Compat.find_program_by_names(*names)
67
74
  names.map { |name| Compat.find_program(name) }.compact.first
68
75
  end
76
+
77
+ #
78
+ # Runs a program.
79
+ #
80
+ # @param [String] path
81
+ # The path to the program.
82
+ #
83
+ # @param [Array] args
84
+ # Additional arguments to run the program with.
85
+ #
86
+ # @return [Boolean]
87
+ # Specifies whether the program exited successfully.
88
+ #
89
+ def Compat.run(path,*args)
90
+ args = args.map { |arg| arg.to_s }
91
+
92
+ if RProgram.debug
93
+ STDERR.puts ">>> #{path} #{args.join(' ')}"
94
+ end
95
+
96
+ return Kernel.system(path,*args)
97
+ end
98
+
99
+ #
100
+ # Runs a program under sudo.
101
+ #
102
+ # @param [String] path
103
+ # Path of the program to run.
104
+ #
105
+ # @param [Array] args
106
+ # Additional arguments to run the program with.
107
+ #
108
+ # @return [Boolean]
109
+ # Specifies whether the program exited successfully.
110
+ #
111
+ # @raise [ProgramNotFound]
112
+ # Indicates that the +sudo+ program could not be located.
113
+ #
114
+ # @since 0.1.8
115
+ #
116
+ def Compat.sudo(path,*args)
117
+ sudo_path = Compat.find_program('sudo')
118
+
119
+ unless sudo_path
120
+ raise(ProgramNotFound,'could not find the "sudo" program',caller)
121
+ end
122
+
123
+ return Compat.run(sudo_path,path,*args)
124
+ end
69
125
  end
70
126
  end
@@ -6,14 +6,16 @@ module RProgram
6
6
  def self.included(base)
7
7
  base.metaclass_eval do
8
8
  #
9
- # @return [String] The name of the program.
9
+ # @return [String]
10
+ # The name of the program.
10
11
  #
11
12
  def program_name
12
13
  @program_name ||= nil
13
14
  end
14
15
 
15
16
  #
16
- # @return [Array] The program's aliases.
17
+ # @return [Array]
18
+ # The program's aliases.
17
19
  #
18
20
  def program_aliases
19
21
  @program_aliases ||= []
@@ -22,7 +24,8 @@ module RProgram
22
24
  #
23
25
  # Combines program_name with program_aliases.
24
26
  #
25
- # @return [Array] Names the program is known by.
27
+ # @return [Array]
28
+ # Names the program is known by.
26
29
  #
27
30
  def program_names
28
31
  ([program_name] + program_aliases).compact
@@ -31,7 +34,8 @@ module RProgram
31
34
  #
32
35
  # Sets the program name for the class.
33
36
  #
34
- # @param [String, Symbol] name The new program name.
37
+ # @param [String, Symbol] name
38
+ # The new program name.
35
39
  #
36
40
  # @example
37
41
  # name_program 'ls'
@@ -43,7 +47,8 @@ module RProgram
43
47
  #
44
48
  # Sets the program aliases for the class.
45
49
  #
46
- # @param [Array] aliases The new program aliases.
50
+ # @param [Array] aliases
51
+ # The new program aliases.
47
52
  #
48
53
  # @example
49
54
  # alias_program 'vim', 'vi'
@@ -55,21 +60,24 @@ module RProgram
55
60
  end
56
61
 
57
62
  #
58
- # @return [String] The program name of the class.
63
+ # @return [String]
64
+ # The program name of the class.
59
65
  #
60
66
  def program_name
61
67
  self.class.program_name
62
68
  end
63
69
 
64
70
  #
65
- # @return [Array] The program aliases of the class.
71
+ # @return [Array]
72
+ # The program aliases of the class.
66
73
  #
67
74
  def program_aliases
68
75
  self.class.program_aliases
69
76
  end
70
77
 
71
78
  #
72
- # @return [Array] The program names of the class.
79
+ # @return [Array]
80
+ # The program names of the class.
73
81
  #
74
82
  def program_names
75
83
  self.class.program_names
@@ -10,17 +10,20 @@ module RProgram
10
10
  #
11
11
  # Creates a new NonOption object.
12
12
  #
13
- # @param [Hash] options Additional options.
14
- # @option options [Symbol] :name The name of the non-option.
13
+ # @param [Hash] options
14
+ # Additional options.
15
+ #
16
+ # @option options [Symbol] :name
17
+ # The name of the non-option.
18
+ #
15
19
  # @option options [true, false] :leading (true)
16
- # Implies the non-option is a
17
- # leading non-option.
20
+ # Implies the non-option is a leading non-option.
21
+ #
18
22
  # @option options [false, true] :tailing (false)
19
- # Implies the non-option is a
20
- # tailing non-option.
23
+ # Implies the non-option is a tailing non-option.
24
+ #
21
25
  # @option options [false, true] :multiple (false)
22
- # Implies the non-option maybe
23
- # given an Array of values.
26
+ # Implies the non-option maybe given an Array of values.
24
27
  #
25
28
  def initialize(options={})
26
29
  @name = options[:name]
@@ -39,8 +42,8 @@ module RProgram
39
42
  #
40
43
  # Determines whether the non-option's arguments are tailing.
41
44
  #
42
- # @return [true, false] Specifies whether the non-option's arguments are
43
- # tailing.
45
+ # @return [true, false]
46
+ # Specifies whether the non-option's arguments are tailing.
44
47
  #
45
48
  def tailing?
46
49
  @tailing == true
@@ -49,8 +52,8 @@ module RProgram
49
52
  #
50
53
  # Determines whether the non-option's arguments are leading.
51
54
  #
52
- # @return [true, false] Specifies whether the non-option's arguments are
53
- # leading.
55
+ # @return [true, false]
56
+ # Specifies whether the non-option's arguments are leading.
54
57
  #
55
58
  def leading?
56
59
  !(@tailing)
@@ -59,10 +62,11 @@ module RProgram
59
62
  #
60
63
  # Formats the arguments for the non-option.
61
64
  #
62
- # @param [Hash, Array, String, nil] value The value to use for the
63
- # arguments of the non-option.
65
+ # @param [Hash, Array, String, nil] value
66
+ # The value to use for the arguments of the non-option.
64
67
  #
65
- # @return [Array] The arguments for the non-option.
68
+ # @return [Array]
69
+ # The arguments for the non-option.
66
70
  #
67
71
  def arguments(value)
68
72
  return [] unless value
@@ -22,30 +22,35 @@ module RProgram
22
22
  # _block_ is not given, the option will use the default_format when
23
23
  # generating the arguments.
24
24
  #
25
- # @param [Hash] options Additional options.
26
- # @option options [String] :flag The command-line flag to use.
25
+ # @param [Hash] options
26
+ # Additional options.
27
+ #
28
+ # @option options [String] :flag
29
+ # The command-line flag to use.
30
+ #
27
31
  # @option options [true, false] :equals (false)
28
- # Implies the option maybe
29
- # formated as
30
- # <tt>"--flag=value"</tt>.
32
+ # Implies the option maybe formated as <tt>"--flag=value"</tt>.
31
33
  #
32
34
  # @option options [true, false] :multiple (false)
33
- # Specifies the option maybe
34
- # given an Array of values.
35
- # @option options [String] :separator The separator to use for
36
- # formating multiple arguments into
37
- # one +String+. Cannot be used with
38
- # the +:multiple+ option.
35
+ # Specifies the option maybe given an Array of values.
36
+ #
37
+ # @option options [String] :separator
38
+ # The separator to use for formating multiple arguments into one
39
+ # +String+. Cannot be used with the +:multiple+ option.
40
+ #
39
41
  # @option options [true, false] :sub_options (false)
40
- # Specifies that the option
41
- # contains sub-options.
42
+ # Specifies that the option contains sub-options.
43
+ #
44
+ # @yield [option, value]
45
+ # If a block is given, it will be used to format each value of the
46
+ # option.
47
+ #
48
+ # @yieldparam [Option] option
49
+ # The option that is being formatted.
42
50
  #
43
- # @yield [option, value] If a block is given, it will be used to format
44
- # each value of the option.
45
- # @yieldparam [Option] option The option that is being formatted.
46
- # @yieldparam [String, Array] value The value to format for the
47
- # option. May be an Array, if multiple
48
- # values are allowed with the option.
51
+ # @yieldparam [String, Array] value
52
+ # The value to format for the option. May be an Array, if multiple
53
+ # values are allowed with the option.
49
54
  #
50
55
  def initialize(options={},&block)
51
56
  @flag = options[:flag]
@@ -75,9 +80,11 @@ module RProgram
75
80
  #
76
81
  # Formats the arguments for the option.
77
82
  #
78
- # @param [Hash, Array, String] value The arguments to format.
83
+ # @param [Hash, Array, String] value
84
+ # The arguments to format.
79
85
  #
80
- # @return [Array] The formatted arguments of the option.
86
+ # @return [Array]
87
+ # The formatted arguments of the option.
81
88
  #
82
89
  def arguments(value)
83
90
  return [@flag] if value == true
@@ -4,7 +4,8 @@ module RProgram
4
4
  #
5
5
  # Creates a new OptionList object.
6
6
  #
7
- # @param [Hash{Symbol => String}] options The options to start with.
7
+ # @param [Hash{Symbol => String}] options
8
+ # The options to start with.
8
9
  #
9
10
  def initialize(options={})
10
11
  super(options)
@@ -7,7 +7,8 @@ module RProgram
7
7
  def self.included(base)
8
8
  base.metaclass_eval do
9
9
  #
10
- # @return [Hash] All defined non-options of the class.
10
+ # @return [Hash]
11
+ # All defined non-options of the class.
11
12
  #
12
13
  def non_options
13
14
  @non_options ||= {}
@@ -17,10 +18,12 @@ module RProgram
17
18
  # Searches for the non-option with the matching name in the class
18
19
  # and it's ancestors.
19
20
  #
20
- # @param [Symbol, String] name The name to search for.
21
+ # @param [Symbol, String] name
22
+ # The name to search for.
21
23
  #
22
- # @return [true, false] Specifies whether the non-option with the
23
- # matching name was defined.
24
+ # @return [true, false]
25
+ # Specifies whether the non-option with the matching name was
26
+ # defined.
24
27
  #
25
28
  def has_non_option?(name)
26
29
  name = name.to_sym
@@ -38,9 +41,11 @@ module RProgram
38
41
  # Searches for the non-option with the matching name in the class
39
42
  # and it's ancestors.
40
43
  #
41
- # @param [Symbol, String] name The name to search for.
44
+ # @param [Symbol, String] name
45
+ # The name to search for.
42
46
  #
43
- # @return [NonOption] The non-option with the matching name.
47
+ # @return [NonOption]
48
+ # The non-option with the matching name.
44
49
  #
45
50
  def get_non_option(name)
46
51
  name = name.to_sym
@@ -57,7 +62,8 @@ module RProgram
57
62
  end
58
63
 
59
64
  #
60
- # @return [Hash] All defined options for the class.
65
+ # @return [Hash]
66
+ # All defined options for the class.
61
67
  #
62
68
  def options
63
69
  @options ||= {}
@@ -67,10 +73,11 @@ module RProgram
67
73
  # Searches for the option with the matching name in the class and
68
74
  # it's ancestors.
69
75
  #
70
- # @param [Symbol, String] name The name to search for.
76
+ # @param [Symbol, String] name
77
+ # The name to search for.
71
78
  #
72
- # @return [true, false] Specifies whether the option with the
73
- # matching name was defined.
79
+ # @return [true, false]
80
+ # Specifies whether the option with the matching name was defined.
74
81
  #
75
82
  def has_option?(name)
76
83
  name = name.to_sym
@@ -88,9 +95,11 @@ module RProgram
88
95
  # Searches for the option with the matching name in the class and
89
96
  # it's ancestors.
90
97
  #
91
- # @param [Symbol, String] name The name to search for.
98
+ # @param [Symbol, String] name
99
+ # The name to search for.
92
100
  #
93
- # @return [Option] The option with the matching name.
101
+ # @return [Option]
102
+ # The option with the matching name.
94
103
  #
95
104
  def get_option(name)
96
105
  name = name.to_sym
@@ -18,14 +18,18 @@ module RProgram
18
18
  #
19
19
  # Creates a new Program object.
20
20
  #
21
- # @param [String] path The full-path of the program.
21
+ # @param [String] path
22
+ # The full-path of the program.
22
23
  #
23
- # @yield [prog] If a block is given, it will be passed the newly
24
- # created Program object.
25
- # @yieldparam [Program] prog The newly created program object.
24
+ # @yield [prog]
25
+ # If a block is given, it will be passed the newly created Program
26
+ # object.
26
27
  #
27
- # @raise [ProgramNotFound] Specifies the given path was not a valid
28
- # file.
28
+ # @yieldparam [Program] prog
29
+ # The newly created program object.
30
+ #
31
+ # @raise [ProgramNotFound]
32
+ # Specifies the given path was not a valid file.
29
33
  #
30
34
  # @example
31
35
  # Program.new('/usr/bin/ls')
@@ -46,17 +50,22 @@ module RProgram
46
50
  #
47
51
  # Creates a new program object.
48
52
  #
49
- # @param [String] path The full-path of the program.
50
- # @param [Array] arguments Additional arguments to initialize the
51
- # program with.
53
+ # @param [String] path
54
+ # The full-path of the program.
55
+ #
56
+ # @param [Array] arguments
57
+ # Additional arguments to initialize the program with.
52
58
  #
53
- # @yield [prog] If a block is given, it will be passed the newly
54
- # created Program object.
55
- # @yieldparam [Program] prog The newly created program object.
59
+ # @yield [prog]
60
+ # If a block is given, it will be passed the newly created Program
61
+ # object.
56
62
  #
57
- # @return [Program, nil] Returns the newly created Program object.
58
- # If the given path was not a valid file,
59
- # +nil+ will be returned.
63
+ # @yieldparam [Program] prog
64
+ # The newly created program object.
65
+ #
66
+ # @return [Program, nil]
67
+ # Returns the newly created Program object. If the given path was
68
+ # not a valid file, +nil+ will be returned.
60
69
  #
61
70
  # @example
62
71
  # Program.find_with_path('/bin/cd')
@@ -75,17 +84,22 @@ module RProgram
75
84
  # if a path within _paths_ is a valid file. Any given _arguments_ or
76
85
  # a given _block_ will be used in creating the new program.
77
86
  #
78
- # @param [Array] paths The Array of paths to search for the program.
79
- # @param [Array] arguments Additional arguments to initialize
80
- # the program with.
87
+ # @param [Array] paths
88
+ # The Array of paths to search for the program.
89
+ #
90
+ # @param [Array] arguments
91
+ # Additional arguments to initialize the program with.
81
92
  #
82
- # @yield [prog] If a block is given, it will be passed the newly
83
- # created Program object.
84
- # @yieldparam [Program] prog The newly created program object.
93
+ # @yield [prog]
94
+ # If a block is given, it will be passed the newly created Program
95
+ # object.
85
96
  #
86
- # @return [Program, nil] Returns the newly created Program object.
87
- # If none of the given paths were valid files,
88
- # +nil+ will be returned.
97
+ # @yieldparam [Program] prog
98
+ # The newly created program object.
99
+ #
100
+ # @return [Program, nil]
101
+ # Returns the newly created Program object. If none of the given
102
+ # paths were valid files, +nil+ will be returned.
89
103
  #
90
104
  # @example
91
105
  # Program.find_with_paths(['/bin/cd','/usr/bin/cd'])
@@ -104,17 +118,21 @@ module RProgram
104
118
  #
105
119
  # Finds and creates the program using it's +program_names+.
106
120
  #
107
- # @param [Array] arguments Additional arguments to initialize the
108
- # program object with.
121
+ # @param [Array] arguments
122
+ # Additional arguments to initialize the program object with.
123
+ #
124
+ # @yield [prog]
125
+ # If a block is given, it will be passed the newly created Program
126
+ # object.
109
127
  #
110
- # @yield [prog] If a block is given, it will be passed the newly
111
- # created Program object.
112
- # @yieldparam [Program] prog The newly created program object.
128
+ # @yieldparam [Program] prog
129
+ # The newly created program object.
113
130
  #
114
- # @return [Program] The newly created program object.
131
+ # @return [Program]
132
+ # The newly created program object.
115
133
  #
116
- # @raise [ProgramNotFound] Non of the +program_names+ represented
117
- # valid programs on the system.
134
+ # @raise [ProgramNotFound]
135
+ # Non of the +program_names+ represented valid programs on the system.
118
136
  #
119
137
  # @example
120
138
  # Program.find
@@ -140,9 +158,11 @@ module RProgram
140
158
  #
141
159
  # Runs the program.
142
160
  #
143
- # @param [Array] args Addition arguments to run the program with.
161
+ # @param [Array] args
162
+ # Addition arguments to run the program with.
144
163
  #
145
- # @return [true, false] Specifies the exit status of the program.
164
+ # @return [true, false]
165
+ # Specifies the exit status of the program.
146
166
  #
147
167
  # @example
148
168
  # echo = Program.find_by_name('echo')
@@ -153,33 +173,51 @@ module RProgram
153
173
  # @see Kernel.system
154
174
  #
155
175
  def run(*args)
156
- args = args.map { |arg| arg.to_s }
157
-
158
- if RProgram.debug
159
- STDERR.puts ">>> #{@path} #{args.join(' ')}"
160
- end
176
+ Compat.run(@path,*args)
177
+ end
161
178
 
162
- return Kernel.system(@path,*args)
179
+ #
180
+ # Runs the program under sudo.
181
+ #
182
+ # @param [Array] args
183
+ # Additional arguments to run the program with.
184
+ #
185
+ # @return [Boolean]
186
+ # Specifies whether the program exited successfully.
187
+ #
188
+ # @raise [ProgramNotFound]
189
+ # Indicates that the +sudo+ program could not be located.
190
+ #
191
+ # @since 0.1.8
192
+ #
193
+ def sudo(*args)
194
+ Compat.sudo(@path,*args)
163
195
  end
164
196
 
165
197
  #
166
198
  # Runs the program with the arguments from the given task.
167
199
  #
168
- # @param [Task] task The task who's arguments will be used to run the
169
- # program.
200
+ # @param [Task] task
201
+ # The task who's arguments will be used to run the program.
170
202
  #
171
- # @return [true, false] Specifies the exit status of the program.
203
+ # @return [true, false]
204
+ # Specifies the exit status of the program.
172
205
  #
173
206
  # @see Kernel.system
174
207
  #
175
208
  def run_task(task)
176
- run(*(task.arguments))
209
+ if task.sudo?
210
+ return sudo(*(task.arguments))
211
+ else
212
+ return run(*(task.arguments))
213
+ end
177
214
  end
178
215
 
179
216
  #
180
217
  # Converts the program to a String.
181
218
  #
182
- # @return [String] The path of the program.
219
+ # @return [String]
220
+ # The path of the program.
183
221
  #
184
222
  # @example
185
223
  # Program.find_by_name('echo').to_s
@@ -1,7 +1,8 @@
1
1
  module RProgram
2
2
  #
3
- # @return [true, false] Specifies whether debugging messages are enabled
4
- # for RProgram. Defaults to false, if not set.
3
+ # @return [true, false]
4
+ # Specifies whether debugging messages are enabled for RProgram.
5
+ # Defaults to false, if not set.
5
6
  #
6
7
  def RProgram.debug
7
8
  @@rprogram_debug ||= false
@@ -10,8 +11,11 @@ module RProgram
10
11
  #
11
12
  # Enables or disables debugging messages for RProgram.
12
13
  #
13
- # @param [true, false] value The new value of RProgram.debug.
14
- # @return [true, false] The new value of RProgram.debug.
14
+ # @param [true, false] value
15
+ # The new value of RProgram.debug.
16
+ #
17
+ # @return [true, false]
18
+ # The new value of RProgram.debug.
15
19
  #
16
20
  def RProgram.debug=(value)
17
21
  @@rprogram_debug = value
data/lib/rprogram/task.rb CHANGED
@@ -6,14 +6,20 @@ module RProgram
6
6
 
7
7
  include Options
8
8
 
9
+ # Specifies whether the task will be run under sudo
10
+ attr_accessor :sudo
11
+
9
12
  #
10
13
  # Creates a new Task object.
11
14
  #
12
- # @param [Hash{Symbol => Object}] options Additional task options.
15
+ # @param [Hash{Symbol => Object}] options
16
+ # Additional task options.
17
+ #
18
+ # @yield [task]
19
+ # If a block is given, it will be passed the newly created task.
13
20
  #
14
- # @yield [task] If a block is given, it will be passed the newly
15
- # created task.
16
- # @yieldparam [Task] task The newly created Task object.
21
+ # @yieldparam [Task] task
22
+ # The newly created Task object.
17
23
  #
18
24
  # @example
19
25
  # Task.new(:test => 'example', :count => 2, :verbose => true)
@@ -24,6 +30,7 @@ module RProgram
24
30
  # end
25
31
  #
26
32
  def initialize(options={},&block)
33
+ @sudo = (options.delete(:sudo) || false)
27
34
  @subtasks = {}
28
35
  @options = options
29
36
 
@@ -34,13 +41,17 @@ module RProgram
34
41
  # Creates a new Task object, then formats command-line arguments
35
42
  # using the Task object.
36
43
  #
37
- # @param [Hash{Symbol => Object}] options Additional task options.
44
+ # @param [Hash{Symbol => Object}] options
45
+ # Additional task options.
38
46
  #
39
- # @yield [task] If a block is given, it will be passed the newly
40
- # created task.
41
- # @yieldparam [Task] task The newly created Task object.
47
+ # @yield [task]
48
+ # If a block is given, it will be passed the newly created task.
42
49
  #
43
- # @return [Array] The formatted arguments from a Task object.
50
+ # @yieldparam [Task] task
51
+ # The newly created Task object.
52
+ #
53
+ # @return [Array]
54
+ # The formatted arguments from a Task object.
44
55
  #
45
56
  # @example
46
57
  # MyTask.arguments(:verbose => true, :count => 2)
@@ -57,11 +68,24 @@ module RProgram
57
68
  self.new(options,&block).arguments
58
69
  end
59
70
 
71
+ #
72
+ # Specifies whether the task will be ran under sudo.
73
+ #
74
+ # @return [Boolean]
75
+ # Returns +true+ if sudo is enabled, returns +false+ otherwise.
76
+ #
77
+ # @since 0.1.8
78
+ #
79
+ def sudo?
80
+ @sudo == true
81
+ end
82
+
60
83
  #
61
84
  # Generates the command-line arguments for all leading non-options.
62
85
  #
63
- # @return [Array] The command-line arguments generated from all the
64
- # leading non-options of the task and it's sub-tasks.
86
+ # @return [Array]
87
+ # The command-line arguments generated from all the leading
88
+ # non-options of the task and it's sub-tasks.
65
89
  #
66
90
  def leading_non_options
67
91
  args = []
@@ -86,8 +110,9 @@ module RProgram
86
110
  #
87
111
  # Generates the command-line arguments from all options.
88
112
  #
89
- # @return [Array] The command-line arguments generated from all the
90
- # options of the task and it's sub-tasks.
113
+ # @return [Array]
114
+ # The command-line arguments generated from all the options of the
115
+ # task and it's sub-tasks.
91
116
  #
92
117
  def options
93
118
  args = []
@@ -109,8 +134,9 @@ module RProgram
109
134
  #
110
135
  # Generates the command-line arguments from all tailing non-options.
111
136
  #
112
- # @return [Array] The command-line arguments generated from all the
113
- # tailing non-options of the task and it's sub-tasks.
137
+ # @return [Array]
138
+ # The command-line arguments generated from all the tailing
139
+ # non-options of the task and it's sub-tasks.
114
140
  #
115
141
  def tailing_non_options
116
142
  args = []
@@ -135,9 +161,9 @@ module RProgram
135
161
  #
136
162
  # Generates the command-line arguments from the task.
137
163
  #
138
- # @return [Array] The command-line arguments compiled from the
139
- # leading non-options, options and tailing non-options
140
- # of the task and it's sub-tasks.
164
+ # @return [Array]
165
+ # The command-line arguments compiled from the leading non-options,
166
+ # options and tailing non-options of the task and it's sub-tasks.
141
167
  #
142
168
  def arguments
143
169
  leading_non_options + options + tailing_non_options
@@ -148,8 +174,11 @@ module RProgram
148
174
  #
149
175
  # Defines a sub-task.
150
176
  #
151
- # @param [String, Symbol] name The name of the sub-task.
152
- # @param [Task] task The task class of the sub-task.
177
+ # @param [String, Symbol] name
178
+ # The name of the sub-task.
179
+ #
180
+ # @param [Task] task
181
+ # The task class of the sub-task.
153
182
  #
154
183
  # @example
155
184
  # subtask :extra, ExtraTask
@@ -175,17 +204,20 @@ module RProgram
175
204
  #
176
205
  # Defines a non-option.
177
206
  #
178
- # @param [Hash] options Additional options for the non-option.
179
- # @option options [Symbol] :name The name of the non-option.
207
+ # @param [Hash] options
208
+ # Additional options for the non-option.
209
+ #
210
+ # @option options [Symbol] :name
211
+ # The name of the non-option.
212
+ #
180
213
  # @option options [true, false] :leading (true)
181
- # Implies the non-option is a
182
- # leading non-option.
214
+ # Implies the non-option is a leading non-option.
215
+ #
183
216
  # @option options [false, true] :tailing (false)
184
- # Implies the non-option is a
185
- # tailing non-option.
217
+ # Implies the non-option is a tailing non-option.
218
+ #
186
219
  # @option options [false, true] :multiple (false)
187
- # Implies the non-option maybe
188
- # given an Array of values.
220
+ # Implies the non-option maybe given an Array of values.
189
221
  #
190
222
  # @example
191
223
  # non_option :name => 'input_file', :tailing => true
@@ -214,19 +246,22 @@ module RProgram
214
246
  #
215
247
  # Defines a long-option.
216
248
  #
217
- # @param [Hash] options Additional options of the long-option.
218
- # @option options [String] :flag The flag to use for the option.
219
- # @option options [Symbol] :name The name of the option. Defaults to
220
- # the flag_namify'ed form of
221
- # <tt>options[:flag]</tt>, if not given.
249
+ # @param [Hash] options
250
+ # Additional options of the long-option.
251
+ #
252
+ # @option options [String] :flag
253
+ # The flag to use for the option.
254
+ #
255
+ # @option options [Symbol] :name
256
+ # The name of the option. Defaults to the flag_namify'ed form of
257
+ # <tt>options[:flag]</tt>, if not given.
258
+ #
222
259
  # @option options [true, false] :multiply (false)
223
- # Specifies that the option may
224
- # appear multiple times in the
225
- # arguments.
260
+ # Specifies that the option may appear multiple times in the
261
+ # arguments.
262
+ #
226
263
  # @option options [true, false] :sub_options (false)
227
- # Specifies that the option
228
- # contains multiple
229
- # sub-options.
264
+ # Specifies that the option contains multiple sub-options.
230
265
  #
231
266
  # @example
232
267
  # long_option :flag => '--output'
@@ -245,17 +280,21 @@ module RProgram
245
280
  #
246
281
  # Defines a short_option.
247
282
  #
248
- # @param [Hash] options Additional options for the short-option.
249
- # @option options [Symbol, String] :name The name of the short-option.
250
- # @option options [String] :flag The flag to use for the short-option.
283
+ # @param [Hash] options
284
+ # Additional options for the short-option.
285
+ #
286
+ # @option options [Symbol, String] :name
287
+ # The name of the short-option.
288
+ #
289
+ # @option options [String] :flag
290
+ # The flag to use for the short-option.
291
+ #
251
292
  # @option options [true, false] :multiply (false)
252
- # Specifies that the option may
253
- # appear multiple times in the
254
- # arguments.
293
+ # Specifies that the option may appear multiple times in the
294
+ # arguments.
295
+ #
255
296
  # @option options [true, false] :sub_options (false)
256
- # Specifies that the option
257
- # contains multiple
258
- # sub-options.
297
+ # Specifies that the option contains multiple sub-options.
259
298
  #
260
299
  # @example
261
300
  # short_option :flag => '-c', :name => :count
@@ -267,17 +306,21 @@ module RProgram
267
306
  #
268
307
  # Defines an option.
269
308
  #
270
- # @param [Hash] options Additional options.
271
- # @option options [Symbol, String] :name The name of the option.
272
- # @option options [String] :flag The flag to use for the option.
309
+ # @param [Hash] options
310
+ # Additional options.
311
+ #
312
+ # @option options [Symbol, String] :name
313
+ # The name of the option.
314
+ #
315
+ # @option options [String] :flag
316
+ # The flag to use for the option.
317
+ #
273
318
  # @option options [true, false] :multiple (false)
274
- # Specifies that the option may
275
- # appear multiple times in the
276
- # arguments.
319
+ # Specifies that the option may appear multiple times in the
320
+ # arguments.
321
+ #
277
322
  # @option options [true, false] :sub_options (false)
278
- # Specifies that the option
279
- # contains multiple
280
- # sub-options.
323
+ # Specifies that the option contains multiple sub-options.
281
324
  #
282
325
  def self.define_option(options,&block)
283
326
  method_name = options[:name].to_sym
@@ -306,9 +349,11 @@ module RProgram
306
349
  #
307
350
  # Converts a long-option flag to a Ruby method name.
308
351
  #
309
- # @param [String] flag The command-line flag to convert.
352
+ # @param [String] flag
353
+ # The command-line flag to convert.
310
354
  #
311
- # @return [String] A method-name compatible version of the given flag.
355
+ # @return [String]
356
+ # A method-name compatible version of the given flag.
312
357
  #
313
358
  # @example
314
359
  # Task.flag_namify('--output-file')
@@ -1,4 +1,4 @@
1
1
  module RProgram
2
2
  # Version of RProgram
3
- VERSION = '0.1.7'
3
+ VERSION = '0.1.8'
4
4
  end
data/spec/task_spec.rb CHANGED
@@ -35,6 +35,21 @@ describe Task do
35
35
  end
36
36
  end
37
37
 
38
+ describe "sudo" do
39
+ it "should allow passing in the :sudo option" do
40
+ task = LSTask.new(:sudo => true)
41
+
42
+ task.should be_sudo
43
+ end
44
+
45
+ it "should allow enabling sudo after the task has been initialized" do
46
+ task = LSTask.new
47
+
48
+ task.sudo = true
49
+ task.should be_sudo
50
+ end
51
+ end
52
+
38
53
  before(:each) do
39
54
  @task = LSTask.new
40
55
  end
data/tasks/spec.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ desc "Run all specifications"
4
+ Spec::Rake::SpecTask.new(:spec) do |t|
5
+ t.libs += ['lib', 'spec']
6
+ t.spec_opts = ['--colour', '--format', 'specdoc']
7
+ end
8
+
9
+ task :test => :spec
10
+ task :default => :spec
data/tasks/yard.rb ADDED
@@ -0,0 +1,18 @@
1
+ lib_dir = File.expand_path(File.join(File.dirname(__FILE__),'..','lib'))
2
+ unless $LOAD_PATH.include?(lib_dir)
3
+ $LOAD_PATH.unshift(lib_dir)
4
+ end
5
+
6
+ require 'rprogram/yard/handlers/ruby'
7
+
8
+ YARD::Rake::YardocTask.new do |t|
9
+ t.files = ['lib/**/*.rb']
10
+ t.options = [
11
+ '--protected',
12
+ '--files', 'History.txt',
13
+ '--title', 'RProgram',
14
+ '--quiet'
15
+ ]
16
+ end
17
+
18
+ task :docs => :yard
data.tar.gz.sig CHANGED
Binary file
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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Postmodern
@@ -30,28 +30,28 @@ cert_chain:
30
30
  pDj+ws7QjtH/Qcrr1l9jfN0ehDs=
31
31
  -----END CERTIFICATE-----
32
32
 
33
- date: 2009-09-25 00:00:00 -07:00
33
+ date: 2009-12-24 00:00:00 -08:00
34
34
  default_executable:
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
- name: yard
38
- type: :runtime
37
+ name: rspec
38
+ type: :development
39
39
  version_requirement:
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: 0.2.3.5
44
+ version: 1.2.8
45
45
  version:
46
46
  - !ruby/object:Gem::Dependency
47
- name: rspec
47
+ name: yard
48
48
  type: :development
49
49
  version_requirement:
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: 1.2.8
54
+ version: 0.5.2
55
55
  version:
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: hoe
@@ -61,7 +61,7 @@ dependencies:
61
61
  requirements:
62
62
  - - ">="
63
63
  - !ruby/object:Gem::Version
64
- version: 2.3.3
64
+ version: 2.4.0
65
65
  version:
66
66
  description: |-
67
67
  RProgram is a library for creating wrappers around command-line programs.
@@ -107,6 +107,8 @@ files:
107
107
  - lib/rprogram/yard/handlers/ruby/metaclass_eval_handler.rb
108
108
  - lib/rprogram/yard/handlers/ruby/legacy.rb
109
109
  - lib/rprogram/yard/handlers/ruby/legacy/metaclass_eval_handler.rb
110
+ - tasks/spec.rb
111
+ - tasks/yard.rb
110
112
  - spec/spec_helper.rb
111
113
  - spec/classes/named_program.rb
112
114
  - spec/classes/aliased_program.rb
metadata.gz.sig CHANGED
Binary file