rubikon 0.5.2 → 0.5.3
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.
- data/lib/rubikon/application/instance_methods.rb +22 -56
- data/lib/rubikon/colored_io.rb +2 -1
- data/lib/rubikon/command.rb +0 -60
- data/lib/rubikon/exceptions.rb +0 -8
- data/lib/rubikon.rb +1 -1
- data/samples/helloworld/hello_world.rb +1 -1
- data/test/test_application.rb +59 -1
- data/test/test_has_arguments.rb +25 -24
- data/test/testapps.rb +1 -2
- metadata +4 -5
- data/lib/core_ext/enumerable.rb +0 -31
@@ -51,7 +51,6 @@ module Rubikon
|
|
51
51
|
@parameters = []
|
52
52
|
@sandbox = Sandbox.new(self)
|
53
53
|
@settings = {
|
54
|
-
:autohelp => true,
|
55
54
|
:autorun => true,
|
56
55
|
:colors => true,
|
57
56
|
:config_file => "#{self.class.to_s.downcase}.yml",
|
@@ -110,14 +109,9 @@ module Rubikon
|
|
110
109
|
rescue
|
111
110
|
raise $! if @settings[:raise_errors]
|
112
111
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
else
|
117
|
-
puts "r{Error:}\n #{$!.message}"
|
118
|
-
debug " at #{$!.backtrace.join("\n at ")}"
|
119
|
-
exit 1
|
120
|
-
end
|
112
|
+
puts "r{Error:}\n #{$!.message}"
|
113
|
+
puts " at #{$!.backtrace.join("\n at ")}" if $DEBUG
|
114
|
+
exit 1
|
121
115
|
ensure
|
122
116
|
InstanceMethods.instance_method(:reset).bind(self).call
|
123
117
|
end
|
@@ -155,38 +149,6 @@ module Rubikon
|
|
155
149
|
global_flag :d => :debug
|
156
150
|
end
|
157
151
|
|
158
|
-
# Prints a help screen for this application
|
159
|
-
#
|
160
|
-
# @param [String] info A additional information string to be displayed
|
161
|
-
# right after usage information
|
162
|
-
# @since 0.6.0
|
163
|
-
def help(info = nil)
|
164
|
-
help = {}
|
165
|
-
@commands.each_value do |command|
|
166
|
-
help[command.name.to_s] = command.description
|
167
|
-
end
|
168
|
-
help.delete('__default')
|
169
|
-
|
170
|
-
if @commands.key? :__default
|
171
|
-
puts " [command] [args]\n\n"
|
172
|
-
else
|
173
|
-
puts " command [args]\n\n"
|
174
|
-
end
|
175
|
-
|
176
|
-
puts "#{info}\n\n" unless info.nil?
|
177
|
-
|
178
|
-
puts 'Commands:'
|
179
|
-
max_command_length = help.keys.max_by { |a| a.size }.size
|
180
|
-
help.sort_by { |name, description| name }.each do |name, description|
|
181
|
-
puts " #{name.ljust(max_command_length)} #{description}"
|
182
|
-
end
|
183
|
-
|
184
|
-
if @commands.key?(:__default) && @commands[:__default].description != :hidden
|
185
|
-
put "\nYou can also call this application without a command:"
|
186
|
-
puts @commands[:__default].help(false) + "\n"
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
152
|
# Defines a command for displaying a help screen
|
191
153
|
#
|
192
154
|
# This takes any defined commands and it's corresponding options and
|
@@ -196,11 +158,16 @@ module Rubikon
|
|
196
158
|
global_parameters = @global_parameters
|
197
159
|
settings = @settings
|
198
160
|
|
199
|
-
command :help,
|
161
|
+
command :help, nil, 'Display this help screen' do
|
200
162
|
put settings[:help_banner]
|
201
163
|
|
164
|
+
help = {}
|
165
|
+
commands.each_value do |command|
|
166
|
+
help[command.name.to_s] = command.description
|
167
|
+
end
|
168
|
+
|
202
169
|
global_params = ''
|
203
|
-
global_parameters.values.uniq.
|
170
|
+
global_parameters.values.uniq.sort {|a,b| a.name.to_s <=> b.name.to_s }.each do |param|
|
204
171
|
global_params << ' ['
|
205
172
|
([param.name] + param.aliases).each_with_index do |name, index|
|
206
173
|
name = name.to_s
|
@@ -211,19 +178,19 @@ module Rubikon
|
|
211
178
|
global_params << ' ...' if param.is_a?(Option)
|
212
179
|
global_params << ']'
|
213
180
|
end
|
214
|
-
put global_params
|
215
181
|
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
command = args.first.to_sym
|
220
|
-
if commands.keys.include?(command)
|
221
|
-
puts commands[command].help
|
222
|
-
else
|
223
|
-
app_help.call("The command \"#{command}\" is undefined. The following commands are available:")
|
224
|
-
end
|
182
|
+
default_description = help.delete('__default')
|
183
|
+
if default_description.nil?
|
184
|
+
puts "#{global_params} command [args]\n\n"
|
225
185
|
else
|
226
|
-
|
186
|
+
puts "#{global_params} [command] [args]\n\n"
|
187
|
+
puts "Without command: #{default_description}\n\n"
|
188
|
+
end
|
189
|
+
|
190
|
+
puts 'Commands:'
|
191
|
+
max_command_length = help.keys.max { |a, b| a.size <=> b.size }.size
|
192
|
+
help.sort_by { |name, description| name }.each do |name, description|
|
193
|
+
puts " #{name.ljust(max_command_length)} #{description}"
|
227
194
|
end
|
228
195
|
end
|
229
196
|
end
|
@@ -273,8 +240,7 @@ module Rubikon
|
|
273
240
|
InstanceMethods.instance_method(:help_command).bind(self).call
|
274
241
|
InstanceMethods.instance_method(:verbose_flag).bind(self).call
|
275
242
|
|
276
|
-
if @settings[:help_as_default] &&
|
277
|
-
!@commands.key?(:__default)
|
243
|
+
if @settings[:help_as_default] && !@commands.keys.include?(:__default)
|
278
244
|
default :help
|
279
245
|
end
|
280
246
|
|
data/lib/rubikon/colored_io.rb
CHANGED
data/lib/rubikon/command.rb
CHANGED
@@ -58,66 +58,6 @@ module Rubikon
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
# Generate help for this command
|
62
|
-
#
|
63
|
-
# @param [Boolean] show_usage If +true+, the returned String will also
|
64
|
-
# include usage information
|
65
|
-
# @return [String] The contents of the help screen for this command
|
66
|
-
# @since 0.6.0
|
67
|
-
def help(show_usage = true)
|
68
|
-
help = ''
|
69
|
-
|
70
|
-
if show_usage
|
71
|
-
help << " #{name}" if name != :__default
|
72
|
-
|
73
|
-
@params.values.uniq.sort_by {|a| a.name.to_s }.each do |param|
|
74
|
-
help << ' ['
|
75
|
-
([param.name] + param.aliases).each_with_index do |name, index|
|
76
|
-
name = name.to_s
|
77
|
-
help << '|' if index > 0
|
78
|
-
help << '-' if name.size > 1
|
79
|
-
help << "-#{name}"
|
80
|
-
end
|
81
|
-
help << ' ...' if param.is_a?(Option)
|
82
|
-
help << ']'
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
help << "\n\n#{description}" unless description.nil?
|
87
|
-
|
88
|
-
help_flags = {}
|
89
|
-
help_options = {}
|
90
|
-
params.each_value do |param|
|
91
|
-
if param.is_a? Flag
|
92
|
-
help_flags[param.name.to_s] = param
|
93
|
-
else
|
94
|
-
help_options[param.name.to_s] = param
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
param_name = lambda { |name| "#{name.size > 1 ? '-' : ' '}-#{name}" }
|
99
|
-
unless help_flags.empty? && help_options.empty?
|
100
|
-
max_param_length = (help_flags.keys + help_options.keys).
|
101
|
-
max_by { |a| a.size }.size + 2
|
102
|
-
end
|
103
|
-
|
104
|
-
unless help_flags.empty?
|
105
|
-
help << "\n\nFlags:"
|
106
|
-
help_flags.sort_by { |name, param| name }.each do |name, param|
|
107
|
-
help << "\n #{param_name.call(name).ljust(max_param_length)}"
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
unless help_options.empty?
|
112
|
-
help << "\n\nOptions:\n"
|
113
|
-
help_options.sort_by { |name, param| name }.each do |name, param|
|
114
|
-
help << " #{param_name.call(name).ljust(max_param_length)} ...\n"
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
help
|
119
|
-
end
|
120
|
-
|
121
61
|
private
|
122
62
|
|
123
63
|
# Add a new parameter for this command
|
data/lib/rubikon/exceptions.rb
CHANGED
@@ -62,16 +62,8 @@ module Rubikon
|
|
62
62
|
# @since 0.3.0
|
63
63
|
class UnknownCommandError < ArgumentError
|
64
64
|
|
65
|
-
# @return [Symbol] The name of the command that has been tried to access
|
66
|
-
attr_reader :command
|
67
|
-
|
68
|
-
# Creates a new error and stores the name of the command that could not be
|
69
|
-
# found
|
70
|
-
#
|
71
|
-
# @param [Symbol] name The name of the unknown command
|
72
65
|
def initialize(name)
|
73
66
|
super "Unknown command: #{name}"
|
74
|
-
@command = name
|
75
67
|
end
|
76
68
|
|
77
69
|
end
|
data/lib/rubikon.rb
CHANGED
@@ -38,7 +38,7 @@ class HelloWorld < Rubikon::Application::Base
|
|
38
38
|
# Ask the user for his name and greet him
|
39
39
|
command :interactive, 'Greet interactively' do
|
40
40
|
name = input 'Please enter your name'
|
41
|
-
call :
|
41
|
+
call :__default, '--name', name
|
42
42
|
end
|
43
43
|
|
44
44
|
# Show a progress bar while iterating through a loop
|
data/test/test_application.rb
CHANGED
@@ -120,7 +120,7 @@ class TestApplication < Test::Unit::TestCase
|
|
120
120
|
|
121
121
|
should 'have a working help command' do
|
122
122
|
@app.run(%w{help})
|
123
|
-
assert_match /Usage: [^ ]* \[--debug\|-d\] \[--gflag\|--gf1\|--gf2\] \[--gopt\|--go1\|--go2 \.\.\.\] \[--verbose\|-v\]
|
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
|
124
124
|
end
|
125
125
|
|
126
126
|
should 'have a working DSL for command parameters' do
|
@@ -167,6 +167,64 @@ class TestApplication < Test::Unit::TestCase
|
|
167
167
|
$VERBOSE = false
|
168
168
|
end
|
169
169
|
|
170
|
+
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
|
174
|
+
assert_equal [], global_params
|
175
|
+
assert_equal [], args
|
176
|
+
|
177
|
+
cmd, global_params, args = @app.parse_arguments(%w{-d -v})
|
178
|
+
assert_instance_of Command, cmd
|
179
|
+
assert_equal cmd.name, :__default
|
180
|
+
assert_instance_of Array, global_params
|
181
|
+
assert_equal 2, global_params.size
|
182
|
+
assert_instance_of Flag, global_params[0]
|
183
|
+
assert_equal :debug, global_params[0].name
|
184
|
+
assert_instance_of Flag, global_params[1]
|
185
|
+
assert_equal :verbose, global_params[1].name
|
186
|
+
assert_equal [], args
|
187
|
+
|
188
|
+
cmd, global_params, args = @app.parse_arguments(%w{-dv})
|
189
|
+
assert_instance_of Command, cmd
|
190
|
+
assert_equal cmd.name, :__default
|
191
|
+
assert_instance_of Array, global_params
|
192
|
+
assert_equal 2, global_params.size
|
193
|
+
assert_instance_of Flag, global_params[0]
|
194
|
+
assert_equal :debug, global_params[0].name
|
195
|
+
assert_instance_of Flag, global_params[1]
|
196
|
+
assert_equal :verbose, global_params[1].name
|
197
|
+
assert_equal [], args
|
198
|
+
|
199
|
+
cmd, global_params, args = @app.parse_arguments(%w{-x})
|
200
|
+
assert_instance_of Command, cmd
|
201
|
+
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
|
205
|
+
|
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
|
209
|
+
assert_instance_of Array, global_params
|
210
|
+
assert_equal 2, global_params.size
|
211
|
+
assert_instance_of Flag, global_params[0]
|
212
|
+
assert_equal :debug, global_params[0].name
|
213
|
+
assert_instance_of Flag, global_params[1]
|
214
|
+
assert_equal :verbose, global_params[1].name
|
215
|
+
assert_equal [], args
|
216
|
+
|
217
|
+
cmd, global_params, args = @app.parse_arguments(%w{sandbox --gopt test puts})
|
218
|
+
assert_instance_of Command, cmd
|
219
|
+
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
|
226
|
+
end
|
227
|
+
|
170
228
|
end
|
171
229
|
|
172
230
|
end
|
data/test/test_has_arguments.rb
CHANGED
@@ -10,6 +10,7 @@ class HasArg
|
|
10
10
|
include HasArguments
|
11
11
|
|
12
12
|
attr_reader :arg_names
|
13
|
+
public :<<, :active!, :arg_count, :args_full?, :check_args, :more_args?
|
13
14
|
|
14
15
|
def initialize(arg_count)
|
15
16
|
super(DummyApp.instance, 'dummy', arg_count)
|
@@ -22,71 +23,71 @@ class TestHasArguments < Test::Unit::TestCase
|
|
22
23
|
|
23
24
|
should 'allow a Numeric as argument count' do
|
24
25
|
has_arg = HasArg.new(1)
|
25
|
-
assert_equal 1..1, has_arg.
|
26
|
+
assert_equal 1..1, has_arg.arg_count
|
26
27
|
assert_nil has_arg.arg_names
|
27
28
|
end
|
28
29
|
|
29
30
|
should 'allow a Range as argument count' do
|
30
31
|
has_arg = HasArg.new(1..3)
|
31
|
-
assert_equal 1..3, has_arg.
|
32
|
+
assert_equal 1..3, has_arg.arg_count
|
32
33
|
assert_nil has_arg.arg_names
|
33
34
|
end
|
34
35
|
|
35
36
|
should 'allow an Array as argument count' do
|
36
37
|
has_arg = HasArg.new([2, 5, 6])
|
37
|
-
assert_equal 2..6, has_arg.
|
38
|
+
assert_equal 2..6, has_arg.arg_count
|
38
39
|
assert_nil has_arg.arg_names
|
39
40
|
end
|
40
41
|
|
41
42
|
should 'allow a Symbol Array as argument names' do
|
42
43
|
has_arg = HasArg.new([:arg1, :arg2, :arg3])
|
43
|
-
assert_equal 3..3, has_arg.
|
44
|
+
assert_equal 3..3, has_arg.arg_count
|
44
45
|
assert_equal [:arg1, :arg2, :arg3], has_arg.arg_names
|
45
46
|
end
|
46
47
|
|
47
48
|
should 'only have required arguments if argument count is > 0' do
|
48
49
|
has_arg = HasArg.new(2)
|
49
|
-
assert !has_arg.
|
50
|
-
assert has_arg.
|
51
|
-
has_arg
|
50
|
+
assert !has_arg.args_full?
|
51
|
+
assert has_arg.more_args?
|
52
|
+
has_arg << 'argument'
|
52
53
|
assert_equal %w{argument}, has_arg.args
|
53
54
|
assert_raise MissingArgumentError do
|
54
|
-
has_arg.
|
55
|
+
has_arg.check_args
|
55
56
|
end
|
56
|
-
has_arg
|
57
|
-
assert has_arg.
|
58
|
-
assert !has_arg.
|
57
|
+
has_arg << 'argument'
|
58
|
+
assert has_arg.args_full?
|
59
|
+
assert !has_arg.more_args?
|
59
60
|
assert_equal %w{argument argument}, has_arg.args
|
60
61
|
assert_raise ExtraArgumentError do
|
61
|
-
has_arg
|
62
|
+
has_arg << 'argument'
|
62
63
|
end
|
63
64
|
assert_equal %w{argument argument}, has_arg.args
|
64
65
|
end
|
65
66
|
|
66
67
|
should 'have required and optional arguments if argument count is < 0' do
|
67
68
|
has_arg = HasArg.new(-1)
|
68
|
-
assert !has_arg.
|
69
|
-
assert has_arg.
|
69
|
+
assert !has_arg.args_full?
|
70
|
+
assert has_arg.more_args?
|
70
71
|
assert_raise MissingArgumentError do
|
71
|
-
has_arg.
|
72
|
+
has_arg.check_args
|
72
73
|
end
|
73
|
-
has_arg
|
74
|
-
assert has_arg.
|
75
|
-
assert has_arg.
|
74
|
+
has_arg << 'argument'
|
75
|
+
assert has_arg.args_full?
|
76
|
+
assert has_arg.more_args?
|
76
77
|
assert_equal %w{argument}, has_arg.args
|
77
78
|
end
|
78
79
|
|
79
80
|
should 'only have optional arguments if argument count is 0' do
|
80
81
|
has_arg = HasArg.new(0)
|
81
|
-
assert has_arg.
|
82
|
-
assert has_arg.
|
83
|
-
has_arg
|
82
|
+
assert has_arg.args_full?
|
83
|
+
assert has_arg.more_args?
|
84
|
+
has_arg << 'argument'
|
84
85
|
assert_equal %w{argument}, has_arg.args
|
85
86
|
end
|
86
87
|
|
87
88
|
should 'provide named arguments' do
|
88
89
|
has_arg = HasArg.new([:named])
|
89
|
-
has_arg
|
90
|
+
has_arg << 'argument'
|
90
91
|
assert_equal 'argument', has_arg[:named]
|
91
92
|
assert_equal 'argument', has_arg.named
|
92
93
|
assert_raise NoMethodError do
|
@@ -99,8 +100,8 @@ class TestHasArguments < Test::Unit::TestCase
|
|
99
100
|
has_arg = HasArg.new nil do
|
100
101
|
block_run = true
|
101
102
|
end
|
102
|
-
has_arg.
|
103
|
-
assert has_arg.
|
103
|
+
has_arg.active!
|
104
|
+
assert has_arg.active?
|
104
105
|
assert block_run
|
105
106
|
end
|
106
107
|
|
data/test/testapps.rb
CHANGED
@@ -13,7 +13,6 @@ end
|
|
13
13
|
|
14
14
|
class TestApp < Application::Base
|
15
15
|
|
16
|
-
set :autohelp, false
|
17
16
|
set :autorun, false
|
18
17
|
set :name, 'Rubikon test application'
|
19
18
|
set :raise_errors, true
|
@@ -30,7 +29,7 @@ class TestApp < Application::Base
|
|
30
29
|
end
|
31
30
|
global_option :go2 => :gopt
|
32
31
|
|
33
|
-
default
|
32
|
+
default do
|
34
33
|
'default command'
|
35
34
|
end
|
36
35
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubikon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 3
|
10
|
+
version: 0.5.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sebastian Staudt
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-09 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -74,7 +74,6 @@ files:
|
|
74
74
|
- LICENSE
|
75
75
|
- README.md
|
76
76
|
- Rakefile
|
77
|
-
- lib/core_ext/enumerable.rb
|
78
77
|
- lib/core_ext/object.rb
|
79
78
|
- lib/core_ext/string.rb
|
80
79
|
- lib/rubikon.rb
|
data/lib/core_ext/enumerable.rb
DELETED
@@ -1,31 +0,0 @@
|
|
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) 2010, Sebastian Staudt
|
5
|
-
|
6
|
-
unless Enumerable.method_defined?(:max_by)
|
7
|
-
|
8
|
-
# Extends Ruby's own Enumrable module with method #max_by? for Ruby < 1.8.7
|
9
|
-
#
|
10
|
-
# @author Sebastian Staudt
|
11
|
-
# @since 0.5.0
|
12
|
-
module Enumerable
|
13
|
-
|
14
|
-
def find_index(obj)
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
# Returns the object in enum that gives the maximum value from the given
|
19
|
-
# block.
|
20
|
-
#
|
21
|
-
# @yield [obj] The block to call on each element in the enum
|
22
|
-
# @yieldparam [Object] obj A single object in the enum
|
23
|
-
# @yieldreturn [Comparable] A value that can be compared (+<=>+) with the
|
24
|
-
# values of the other objects in the enum
|
25
|
-
def max_by(&block)
|
26
|
-
max { |a , b| block.call(a) <=> block.call(b) }
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|