rubikon 0.5.3 → 0.6.0

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.
@@ -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
@@ -0,0 +1,4 @@
1
+ :boolean: false
2
+ :global: true
3
+ :number: 1
4
+ :string: boring
@@ -0,0 +1,4 @@
1
+ :boolean: true
2
+ :local: true
3
+ :number: 2
4
+ :string: smart
@@ -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, [:who]
21
- option :names, -1
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
- greet parameters[:name].who
26
+ names = [who]
26
27
  elsif given? :names
27
- names.args.each do |name|
28
- greet name
29
- end
28
+ names = who
29
+ elsif given? :special
30
+ names = [who]
30
31
  else
31
- greet 'World'
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
@@ -0,0 +1,2 @@
1
+ :value: 0
2
+ :overriden: 0
@@ -0,0 +1,3 @@
1
+ :value1: 1
2
+ :overriden: 1
3
+
@@ -0,0 +1,3 @@
1
+ :value2: 2
2
+ :overriden: 2
3
+
@@ -0,0 +1,10 @@
1
+ # This is a comment
2
+ ; and another one
3
+
4
+ [section]
5
+ value = 0
6
+ test1 = 1
7
+
8
+ [section]
9
+ value = 1 ; This is overriden
10
+ test2 = 2 # This is not
@@ -10,7 +10,3 @@ include Rubikon
10
10
 
11
11
  require 'rubygems'
12
12
  require 'shoulda'
13
-
14
- unless RUBY_VERSION[0..2] == '1.9'
15
- begin require 'redgreen'; rescue LoadError; end
16
- end
@@ -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-2010, Sebastian Staudt
4
+ # Copyright (c) 2009-2011, Sebastian Staudt
5
5
 
6
- require 'test_helper'
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, @ostream.gets
52
- assert_equal " Unknown command: unknown\n", @ostream.gets
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
- input_string = 'test'
76
- @istream.puts input_string
77
+ @istream.puts "test\ntest\nx\n"
77
78
  @istream.rewind
78
- assert_equal input_string, @app.run(%w{input})
79
- assert_equal 'input: ', @ostream.gets
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 Display this help screen\n input \n object_id \n parameters \n progressbar \n sandbox \n throbber \n/, @ostream.string
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
- cmd, global_params, args = @app.parse_arguments(%w{})
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
- assert_instance_of Array, global_params
203
- assert_equal 0, global_params.size
204
- assert_equal %w{-x}, args
199
+ assert_equal [], cmd_params
205
200
 
206
- cmd, global_params, args = @app.parse_arguments(%w{-d -v object_id})
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
- assert_equal [], args
208
+ assert_instance_of Command, cmd
209
+ assert_equal cmd.name, :object_id
210
+ assert_equal [], cmd_params
216
211
 
217
- cmd, global_params, args = @app.parse_arguments(%w{sandbox --gopt test puts})
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
- assert_instance_of Array, global_params
221
- assert_equal 1, global_params.size
222
- assert_instance_of Option, global_params[0]
223
- assert_equal :gopt, global_params[0].name
224
- assert_equal %w{test}, global_params[0].args
225
- assert_equal %w{puts}, args
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
@@ -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-2010, Sebastian Staudt
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