rubikon 0.5.3 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +1 -1
- data/README.md +13 -10
- data/Rakefile +4 -23
- data/gemspec.yml +17 -0
- data/lib/core_ext/enumerable.rb +27 -0
- data/lib/rubikon.rb +3 -5
- data/lib/rubikon/application/dsl_methods.rb +139 -40
- data/lib/rubikon/application/instance_methods.rb +149 -99
- data/lib/rubikon/application/sandbox.rb +0 -0
- data/lib/rubikon/argument_vector.rb +119 -0
- data/lib/rubikon/command.rb +88 -55
- data/lib/rubikon/config/auto_provider.rb +28 -3
- data/lib/rubikon/config/factory.rb +16 -3
- data/lib/rubikon/config/ini_provider.rb +29 -1
- data/lib/rubikon/config/yaml_provider.rb +16 -1
- data/lib/rubikon/{exceptions.rb → errors.rb} +25 -1
- data/lib/rubikon/has_arguments.rb +140 -40
- data/lib/rubikon/parameter.rb +4 -0
- data/lib/rubikon/version.rb +11 -0
- data/samples/config/global/config.yml +4 -0
- data/samples/config/local/config.yml +4 -0
- data/samples/helloworld/hello_world.rb +14 -10
- data/test/config/0/config.yml +2 -0
- data/test/config/1/config.yml +3 -0
- data/test/config/2/config.yml +3 -0
- data/test/config/test.ini +10 -0
- data/test/{test_helper.rb → helper.rb} +0 -4
- data/test/test_application.rb +37 -35
- data/test/test_argument_vector.rb +156 -0
- data/test/test_command.rb +1 -35
- data/test/test_config.rb +1 -1
- data/test/test_has_arguments.rb +161 -20
- data/test/test_ini_provider.rb +1 -1
- data/test/test_parameter.rb +1 -1
- data/test/test_progress_bar.rb +1 -1
- data/test/test_throbber.rb +1 -1
- data/test/testapps.rb +10 -5
- metadata +73 -69
data/lib/rubikon/parameter.rb
CHANGED
@@ -19,6 +19,10 @@ module Rubikon
|
|
19
19
|
# @return [Array<Symbol>] The alias names of this parameter
|
20
20
|
attr_reader :aliases
|
21
21
|
|
22
|
+
# @return [String] The description of this parameter
|
23
|
+
# @since 0.6.0
|
24
|
+
attr_accessor :description
|
25
|
+
|
22
26
|
# @return [Symbol] The primary name of this parameter
|
23
27
|
attr_reader :name
|
24
28
|
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under
|
2
|
+
# the terms of the new BSD License.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2011, Sebastian Staudt
|
5
|
+
|
6
|
+
module Rubikon
|
7
|
+
|
8
|
+
# This is the current version of the Rubikon gem
|
9
|
+
VERSION = '0.6.0'
|
10
|
+
|
11
|
+
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
# This code is free software; you can redistribute it and/or modify it under
|
4
4
|
# the terms of the new BSD License.
|
5
5
|
#
|
6
|
-
# Copyright (c) 2010, Sebastian Staudt
|
6
|
+
# Copyright (c) 2010-2011, Sebastian Staudt
|
7
7
|
|
8
8
|
if ENV['RUBIKON_DEV']
|
9
9
|
require File.join(File.expand_path(File.dirname(__FILE__)), '..', '..', 'lib', 'rubikon')
|
@@ -16,20 +16,24 @@ end
|
|
16
16
|
class HelloWorld < Rubikon::Application::Base
|
17
17
|
|
18
18
|
# Greet the whole world per default
|
19
|
-
flag :more
|
20
|
-
option :name,
|
21
|
-
option :names,
|
19
|
+
flag :more, 'Display more information while greeting'
|
20
|
+
option :name, 'A single name to greet', :who
|
21
|
+
option :names, 'One or more names to greet', :who => :remainder
|
22
|
+
option :special, 'A special name', :who => ['Guybrush', /LeChuck/, :numeric]
|
22
23
|
default 'Simple hello world' do
|
23
24
|
debug 'Starting to greet the world...'
|
24
25
|
if given? :name
|
25
|
-
|
26
|
+
names = [who]
|
26
27
|
elsif given? :names
|
27
|
-
names
|
28
|
-
|
29
|
-
|
28
|
+
names = who
|
29
|
+
elsif given? :special
|
30
|
+
names = [who]
|
30
31
|
else
|
31
|
-
|
32
|
+
names = %w{World}
|
32
33
|
end
|
34
|
+
|
35
|
+
names.each { |name| greet name }
|
36
|
+
|
33
37
|
puts 'Nice to see you.' if given? :more
|
34
38
|
end
|
35
39
|
|
@@ -42,7 +46,7 @@ class HelloWorld < Rubikon::Application::Base
|
|
42
46
|
end
|
43
47
|
|
44
48
|
# Show a progress bar while iterating through a loop
|
45
|
-
flag :brackets
|
49
|
+
flag :brackets, 'Show brackets around the progress bar'
|
46
50
|
command :progress, 'Display a progress bar' do
|
47
51
|
put 'Watch my progress while I greet the world: '
|
48
52
|
x = 1000000
|
data/test/test_application.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# This code is free software; you can redistribute it and/or modify it under
|
2
2
|
# the terms of the new BSD License.
|
3
3
|
#
|
4
|
-
# Copyright (c) 2009-
|
4
|
+
# Copyright (c) 2009-2011, Sebastian Staudt
|
5
5
|
|
6
|
-
require '
|
6
|
+
require 'helper'
|
7
7
|
require 'testapps'
|
8
8
|
|
9
9
|
class TestApplication < Test::Unit::TestCase
|
@@ -30,7 +30,9 @@ class TestApplication < Test::Unit::TestCase
|
|
30
30
|
|
31
31
|
setup do
|
32
32
|
@app = TestApp
|
33
|
+
@estream = StringIO.new
|
33
34
|
@ostream = StringIO.new
|
35
|
+
@app.set :estream, @estream
|
34
36
|
@app.set :ostream, @ostream
|
35
37
|
@app.set :raise_errors, true
|
36
38
|
end
|
@@ -48,8 +50,8 @@ class TestApplication < Test::Unit::TestCase
|
|
48
50
|
else
|
49
51
|
error_text = "Error:\n"
|
50
52
|
end
|
51
|
-
assert_equal error_text, @
|
52
|
-
assert_equal " Unknown command: unknown\n", @
|
53
|
+
assert_equal error_text, @estream.gets
|
54
|
+
assert_equal " Unknown command: unknown\n", @estream.gets
|
53
55
|
end
|
54
56
|
|
55
57
|
should 'run its default command without arguments' do
|
@@ -72,11 +74,11 @@ class TestApplication < Test::Unit::TestCase
|
|
72
74
|
@istream = StringIO.new
|
73
75
|
@app.set :istream, @istream
|
74
76
|
|
75
|
-
|
76
|
-
@istream.puts input_string
|
77
|
+
@istream.puts "test\ntest\nx\n"
|
77
78
|
@istream.rewind
|
78
|
-
|
79
|
-
assert_equal
|
79
|
+
|
80
|
+
assert_equal %w{test x}, @app.run(%w{input})
|
81
|
+
assert_equal 'input: validated [x]: Please provide valid input [x]: ', @ostream.gets
|
80
82
|
end
|
81
83
|
|
82
84
|
should 'not break output while displaying a throbber or progress bar' do
|
@@ -120,7 +122,7 @@ class TestApplication < Test::Unit::TestCase
|
|
120
122
|
|
121
123
|
should 'have a working help command' do
|
122
124
|
@app.run(%w{help})
|
123
|
-
assert_match /Usage: [^ ]* \[--debug\|-d\] \[--gflag\|--gf1\|--gf2\] \[--gopt\|--go1\|--go2 \.\.\.\] \[--verbose\|-v\] command \[args\]\n\nCommands:\n arguments \n globalopt \n help
|
125
|
+
assert_match /Usage: [^ ]* \[--debug\|-d\] \[--gflag\|--gf1\|--gf2\] \[--gopt\|--go1\|--go2 \.\.\.\] \[--verbose\|-v\] \[command\] \[args\]\n\nCommands:\n arguments \n globalopt \n help Show help for the application or a single command\n input \n object_id \n parameters \n progressbar \n sandbox \n throbber \n/, @ostream.string
|
124
126
|
end
|
125
127
|
|
126
128
|
should 'have a working DSL for command parameters' do
|
@@ -168,61 +170,61 @@ class TestApplication < Test::Unit::TestCase
|
|
168
170
|
end
|
169
171
|
|
170
172
|
should 'parse arguments correctly' do
|
171
|
-
|
172
|
-
assert_instance_of Command, cmd
|
173
|
-
assert_equal cmd.name, :__default
|
173
|
+
global_params, cmd, cmd_params = @app.parse_arguments(%w{})
|
174
174
|
assert_equal [], global_params
|
175
|
-
assert_equal [], args
|
176
|
-
|
177
|
-
cmd, global_params, args = @app.parse_arguments(%w{-d -v})
|
178
175
|
assert_instance_of Command, cmd
|
179
176
|
assert_equal cmd.name, :__default
|
177
|
+
assert_equal [], cmd_params
|
178
|
+
|
179
|
+
global_params, cmd, cmd_params = @app.parse_arguments(%w{-d -v})
|
180
180
|
assert_instance_of Array, global_params
|
181
181
|
assert_equal 2, global_params.size
|
182
182
|
assert_instance_of Flag, global_params[0]
|
183
183
|
assert_equal :debug, global_params[0].name
|
184
184
|
assert_instance_of Flag, global_params[1]
|
185
185
|
assert_equal :verbose, global_params[1].name
|
186
|
-
assert_equal [], args
|
187
|
-
|
188
|
-
cmd, global_params, args = @app.parse_arguments(%w{-dv})
|
189
186
|
assert_instance_of Command, cmd
|
190
187
|
assert_equal cmd.name, :__default
|
188
|
+
assert_equal [], cmd_params
|
189
|
+
|
190
|
+
global_params, cmd, cmd_params = @app.parse_arguments(%w{-dv})
|
191
191
|
assert_instance_of Array, global_params
|
192
192
|
assert_equal 2, global_params.size
|
193
193
|
assert_instance_of Flag, global_params[0]
|
194
194
|
assert_equal :debug, global_params[0].name
|
195
195
|
assert_instance_of Flag, global_params[1]
|
196
196
|
assert_equal :verbose, global_params[1].name
|
197
|
-
assert_equal [], args
|
198
|
-
|
199
|
-
cmd, global_params, args = @app.parse_arguments(%w{-x})
|
200
197
|
assert_instance_of Command, cmd
|
201
198
|
assert_equal cmd.name, :__default
|
202
|
-
|
203
|
-
assert_equal 0, global_params.size
|
204
|
-
assert_equal %w{-x}, args
|
199
|
+
assert_equal [], cmd_params
|
205
200
|
|
206
|
-
|
207
|
-
assert_instance_of Command, cmd
|
208
|
-
assert_equal cmd.name, :object_id
|
201
|
+
global_params, cmd, cmd_params = @app.parse_arguments(%w{-d -v object_id})
|
209
202
|
assert_instance_of Array, global_params
|
210
203
|
assert_equal 2, global_params.size
|
211
204
|
assert_instance_of Flag, global_params[0]
|
212
205
|
assert_equal :debug, global_params[0].name
|
213
206
|
assert_instance_of Flag, global_params[1]
|
214
207
|
assert_equal :verbose, global_params[1].name
|
215
|
-
|
208
|
+
assert_instance_of Command, cmd
|
209
|
+
assert_equal cmd.name, :object_id
|
210
|
+
assert_equal [], cmd_params
|
216
211
|
|
217
|
-
|
212
|
+
global_params, cmd, cmd_params = @app.parse_arguments(%w{-d sandbox --gopt test puts})
|
213
|
+
assert_instance_of Array, global_params
|
214
|
+
assert_equal 2, global_params.size
|
215
|
+
assert_instance_of Flag, global_params[0]
|
216
|
+
assert_equal :debug, global_params[0].name
|
217
|
+
assert_instance_of Option, global_params[1]
|
218
|
+
assert_equal :gopt, global_params[1].name
|
219
|
+
assert_equal %w{test}, global_params[1].args
|
218
220
|
assert_instance_of Command, cmd
|
219
221
|
assert_equal cmd.name, :sandbox
|
220
|
-
|
221
|
-
assert_equal
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
222
|
+
assert_equal %w{puts}, cmd.args
|
223
|
+
assert_equal [], cmd_params
|
224
|
+
|
225
|
+
assert_raise UnknownParameterError do
|
226
|
+
@app.parse_arguments(%w{-x})
|
227
|
+
end
|
226
228
|
end
|
227
229
|
|
228
230
|
end
|
@@ -0,0 +1,156 @@
|
|
1
|
+
# This code is free software; you can redistribute it and/or modify it under the
|
2
|
+
# terms of the new BSD License.
|
3
|
+
#
|
4
|
+
# Copyright (c) 2011, Sebastian Staudt
|
5
|
+
|
6
|
+
require 'helper'
|
7
|
+
require 'testapps'
|
8
|
+
|
9
|
+
class TestArgumentVector < Test::Unit::TestCase
|
10
|
+
|
11
|
+
context 'An argument vector' do
|
12
|
+
|
13
|
+
setup do
|
14
|
+
@app = TestApp.instance
|
15
|
+
@argv = []
|
16
|
+
@argv.extend ArgumentVector
|
17
|
+
end
|
18
|
+
|
19
|
+
should 'expand arguments correctly' do
|
20
|
+
@argv.expand!
|
21
|
+
assert @argv.empty?
|
22
|
+
|
23
|
+
@argv.replace %w{command --param arg}
|
24
|
+
@argv.expand!
|
25
|
+
assert_equal %w{command --param arg}, @argv
|
26
|
+
|
27
|
+
@argv.replace %w{command -pq arg}
|
28
|
+
@argv.expand!
|
29
|
+
assert_equal %w{command -p -q arg}, @argv
|
30
|
+
|
31
|
+
@argv.replace %w{command -pp arg}
|
32
|
+
@argv.expand!
|
33
|
+
assert_equal %w{command -p arg}, @argv
|
34
|
+
|
35
|
+
@argv.replace %w{command --param=arg}
|
36
|
+
@argv.expand!
|
37
|
+
assert_equal %w{command --param arg}, @argv
|
38
|
+
|
39
|
+
@argv.replace %w{command -pq --param=arg}
|
40
|
+
@argv.expand!
|
41
|
+
assert_equal %w{command -p -q --param arg}, @argv
|
42
|
+
end
|
43
|
+
|
44
|
+
should 'select the command correctly' do
|
45
|
+
assert_equal [nil, 0], @argv.command!(TestAppWithoutDefault.instance.commands)
|
46
|
+
assert @argv.empty?
|
47
|
+
|
48
|
+
command, pos = @argv.command!(@app.commands)
|
49
|
+
assert_equal :__default, command.name
|
50
|
+
assert_equal 0, pos
|
51
|
+
assert @argv.empty?
|
52
|
+
|
53
|
+
@argv.replace %w{unknown}
|
54
|
+
assert_equal [nil, 0], @argv.command!(TestAppWithoutDefault.instance.commands)
|
55
|
+
assert_equal %w{unknown}, @argv
|
56
|
+
|
57
|
+
@argv.replace %w{input}
|
58
|
+
command, pos = @argv.command!(@app.commands)
|
59
|
+
assert_equal :input, command.name
|
60
|
+
assert_equal 0, pos
|
61
|
+
assert @argv.empty?
|
62
|
+
|
63
|
+
@argv.replace %w{--debug input}
|
64
|
+
command, pos = @argv.command!(@app.commands)
|
65
|
+
assert_equal :input, command.name
|
66
|
+
assert_equal 1, pos
|
67
|
+
assert_equal %w{--debug}, @argv
|
68
|
+
|
69
|
+
@argv.replace %w{--debug input input}
|
70
|
+
command, pos = @argv.command!(@app.commands)
|
71
|
+
assert_equal :input, command.name
|
72
|
+
assert_equal 1, pos
|
73
|
+
assert_equal %w{--debug input}, @argv
|
74
|
+
|
75
|
+
@argv.replace %w{--debug -- input}
|
76
|
+
command, pos = @argv.command!(@app.commands)
|
77
|
+
assert_equal :__default, command.name
|
78
|
+
assert_equal 0, pos
|
79
|
+
assert_equal %w{--debug input}, @argv
|
80
|
+
end
|
81
|
+
|
82
|
+
should 'parse parameters correctly' do
|
83
|
+
assert @argv.params!(@app.global_parameters).empty?
|
84
|
+
assert @argv.empty?
|
85
|
+
|
86
|
+
@argv.replace %w{--gopt}
|
87
|
+
params = @argv.params!(@app.global_parameters)
|
88
|
+
assert_equal 1, params.size
|
89
|
+
assert_equal :gopt, params.first.name
|
90
|
+
assert @argv.empty?
|
91
|
+
|
92
|
+
@argv.replace %w{dummy --gopt}
|
93
|
+
params = @argv.params!(@app.global_parameters, 1)
|
94
|
+
assert_equal 1, params.size
|
95
|
+
assert_equal :gopt, params.first.name
|
96
|
+
assert_equal %w{dummy}, @argv
|
97
|
+
|
98
|
+
@argv.replace %w{--gopt arg}
|
99
|
+
params = @argv.params!(@app.global_parameters)
|
100
|
+
assert_equal 1, params.size
|
101
|
+
assert_equal :gopt, params.first.name
|
102
|
+
assert_equal %w{arg}, params.first.args
|
103
|
+
assert @argv.empty?
|
104
|
+
|
105
|
+
@app.global_parameters[:gopt].send :reset
|
106
|
+
|
107
|
+
@argv.replace %w{--gopt arg --gflag}
|
108
|
+
params = @argv.params!(@app.global_parameters)
|
109
|
+
assert_equal 2, params.size
|
110
|
+
assert_equal :gopt, params.first.name
|
111
|
+
assert_equal %w{arg}, params.first.args
|
112
|
+
assert_equal :gflag, params.last.name
|
113
|
+
assert @argv.empty?
|
114
|
+
end
|
115
|
+
|
116
|
+
should 'parse arguments correctly' do
|
117
|
+
gopt = @app.global_parameters[:gopt]
|
118
|
+
|
119
|
+
@argv.scoped_args!(gopt)
|
120
|
+
assert gopt.args.empty?
|
121
|
+
assert @argv.empty?
|
122
|
+
|
123
|
+
@argv.replace %w{arg}
|
124
|
+
@argv.scoped_args!(gopt)
|
125
|
+
assert_equal %w{arg}, gopt.args
|
126
|
+
assert @argv.empty?
|
127
|
+
|
128
|
+
gopt.send :reset
|
129
|
+
|
130
|
+
@argv.replace %w{arg1 arg2}
|
131
|
+
@argv.scoped_args!(gopt)
|
132
|
+
assert_equal %w{arg1}, gopt.args
|
133
|
+
assert_equal %w{arg2}, @argv
|
134
|
+
|
135
|
+
gopt.send :reset
|
136
|
+
|
137
|
+
@argv.replace %w{--gflag arg}
|
138
|
+
@argv.scoped_args!(gopt)
|
139
|
+
assert gopt.args.empty?
|
140
|
+
assert_equal %w{--gflag arg}, @argv
|
141
|
+
|
142
|
+
gopt.send :reset
|
143
|
+
|
144
|
+
@argv.replace %w{--gflag arg}
|
145
|
+
@argv.scoped_args!(gopt, 1)
|
146
|
+
assert_equal %w{arg}, gopt.args
|
147
|
+
assert_equal %w{--gflag}, @argv
|
148
|
+
end
|
149
|
+
|
150
|
+
teardown do
|
151
|
+
@app.send :reset
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
data/test/test_command.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# This code is free software; you can redistribute it and/or modify it under
|
2
2
|
# the terms of the new BSD License.
|
3
3
|
#
|
4
|
-
# Copyright (c) 2009-
|
4
|
+
# Copyright (c) 2009-2011, Sebastian Staudt
|
5
5
|
|
6
6
|
require 'test_parameter'
|
7
7
|
|
@@ -39,40 +39,6 @@ class TestCommand < Test::Unit::TestCase
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
should 'correctly parse given parameters' do
|
43
|
-
command = Command.new @app, :command, [:cmd_arg] do end
|
44
|
-
option = Option.new(@app, :test, [:opt_arg])
|
45
|
-
command.send(:add_param, option)
|
46
|
-
flag = Flag.new(@app, :t)
|
47
|
-
command.send(:add_param, flag)
|
48
|
-
command.send(:run, *%w{--test arg -t test})
|
49
|
-
assert option.active?
|
50
|
-
assert flag.active?
|
51
|
-
assert_equal %w{test}, command.arguments
|
52
|
-
assert_equal 'test', command[0]
|
53
|
-
assert_equal 'test', command.cmd_arg
|
54
|
-
assert_equal %w{arg}, command.parameters[:test].args
|
55
|
-
assert_equal 'arg', command.test[0]
|
56
|
-
assert_equal 'arg', command.test.opt_arg
|
57
|
-
|
58
|
-
assert_raise UnknownParameterError do
|
59
|
-
command.send(:run, *%w{--unknown})
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
should 'allow parameter aliases' do
|
64
|
-
command = Command.new @app, :command do end
|
65
|
-
command.send(:add_param, { :t => :test })
|
66
|
-
flag1 = Flag.new(@app, :test)
|
67
|
-
command.send(:add_param, flag1)
|
68
|
-
flag2 = Flag.new(@app, :test2)
|
69
|
-
command.send(:add_param, flag2)
|
70
|
-
command.send(:add_param, { :t2 => :test2 })
|
71
|
-
command.send(:run, *%w{-t --t2})
|
72
|
-
assert flag1.send(:active?)
|
73
|
-
assert flag2.send(:active?)
|
74
|
-
end
|
75
|
-
|
76
42
|
should 'run the code supplied inside its block' do
|
77
43
|
block_run = false
|
78
44
|
command = Command.new @app, :command do
|