CommandLine 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
@@ -34,7 +34,6 @@ class Application
|
|
34
34
|
DEFAULT_BODY_INDENT = 4
|
35
35
|
|
36
36
|
def initialize
|
37
|
-
|
38
37
|
@synopsis = ""
|
39
38
|
@arg_arity = [0,0]
|
40
39
|
@options = []
|
@@ -42,7 +41,7 @@ class Application
|
|
42
41
|
@args = []
|
43
42
|
@argv ||= ARGV
|
44
43
|
|
45
|
-
|
44
|
+
__init_format
|
46
45
|
|
47
46
|
__child_initialize if
|
48
47
|
self.class.private_instance_methods(false).include?("__child_initialize")
|
@@ -50,49 +49,6 @@ class Application
|
|
50
49
|
@option_parser ||= CommandLine::OptionParser.new(@options)
|
51
50
|
end
|
52
51
|
|
53
|
-
def parse_command_line(argv)
|
54
|
-
if argv.empty? && [0,0] != @arg_arity
|
55
|
-
puts usage
|
56
|
-
exit(0)
|
57
|
-
end
|
58
|
-
|
59
|
-
@option_data = @option_parser.parse(argv)
|
60
|
-
|
61
|
-
validate_args(@option_data.args)
|
62
|
-
@arg_names.each_with_index { |name, idx|
|
63
|
-
instance_variable_set("@#{name}", @option_data.args[idx])
|
64
|
-
}
|
65
|
-
end
|
66
|
-
|
67
|
-
def _init_format
|
68
|
-
#
|
69
|
-
# Formatting defaults
|
70
|
-
#
|
71
|
-
console_width = ENV["COLUMNS"]
|
72
|
-
@columns =
|
73
|
-
if console_width.nil?
|
74
|
-
DEFAULT_CONSOLE_WIDTH
|
75
|
-
elsif console_width < MIN_CONSOLE_WIDTH
|
76
|
-
console_width
|
77
|
-
else
|
78
|
-
console_width - DEFAULT_BODY_INDENT
|
79
|
-
end
|
80
|
-
@body_indent = DEFAULT_BODY_INDENT
|
81
|
-
@tag_paragraph = false
|
82
|
-
@order = :index # | :alpha
|
83
|
-
end
|
84
|
-
|
85
|
-
def validate_args(od_args)
|
86
|
-
size = od_args.size
|
87
|
-
min, max = @arg_arity
|
88
|
-
max = 1.0/0.0 if -1 == max
|
89
|
-
raise(ArgumentError,
|
90
|
-
"Missing expected arguments. Found #{size} but expected #{min}.\n"+
|
91
|
-
"#{usage}") if size < min
|
92
|
-
raise(ArgumentError, "Too many arguments. Found #{size} but "+
|
93
|
-
"expected #{max}.\n#{usage}") if size > max
|
94
|
-
end
|
95
|
-
|
96
52
|
def options(*opts)
|
97
53
|
opts.each { |opt| option(*[opt].flatten) }
|
98
54
|
end
|
@@ -103,10 +59,10 @@ class Application
|
|
103
59
|
args.each { |arg|
|
104
60
|
new_list <<
|
105
61
|
case arg
|
106
|
-
when :help then
|
107
|
-
when :debug then
|
108
|
-
when :verbose then
|
109
|
-
when :version then
|
62
|
+
when :help then __help
|
63
|
+
when :debug then __debug
|
64
|
+
when :verbose then __verbose
|
65
|
+
when :version then __version
|
110
66
|
else arg
|
111
67
|
end
|
112
68
|
}
|
@@ -177,7 +133,7 @@ class Application
|
|
177
133
|
@arg_arity = [1,1]
|
178
134
|
when Array
|
179
135
|
v = expected_args[0]
|
180
|
-
|
136
|
+
__validate_arg_arity(v)
|
181
137
|
@arg_arity = v
|
182
138
|
else
|
183
139
|
raise(InvalidArgumentArityError,
|
@@ -190,22 +146,12 @@ class Application
|
|
190
146
|
end
|
191
147
|
end
|
192
148
|
|
193
|
-
def validate_arg_arity(arity)
|
194
|
-
min, max = *arity
|
195
|
-
raise(InvalidArgumentArityError, "Minimum argument arity '#{min}' must be "+
|
196
|
-
"greater than or equal to 0.") unless min >= 0
|
197
|
-
raise(InvalidArgumentArityError, "Maximum argument arity '#{max}' must be "+
|
198
|
-
"greater than or equal to -1.") if max < -1
|
199
|
-
raise(InvalidArgumentArityError, "Maximum argument arity '#{max}' must be "+
|
200
|
-
"greater than minimum arg_arity '#{min}'.") if max < min && max != -1
|
201
|
-
end
|
202
|
-
|
203
149
|
def usage
|
204
150
|
" Usage: #{name} #{synopsis}"
|
205
151
|
end
|
206
152
|
|
207
153
|
def man
|
208
|
-
require '
|
154
|
+
require 'text/format'
|
209
155
|
f = Text::Format.new
|
210
156
|
f = Text::Format.new
|
211
157
|
f.columns = @columns
|
@@ -219,7 +165,7 @@ class Application
|
|
219
165
|
nm = "#{short_description}".empty? ? name : "#{name} - #{short_description}"
|
220
166
|
s << f.format(nm)
|
221
167
|
|
222
|
-
sn = "#{synopsis}"
|
168
|
+
sn = "#{synopsis}".empty? ? "" : "#{name} #{synopsis}"
|
223
169
|
unless sn.empty?
|
224
170
|
s << "SYNOPSIS\n"
|
225
171
|
s << f.format(sn)
|
@@ -251,14 +197,14 @@ class Application
|
|
251
197
|
end
|
252
198
|
alias :help :man
|
253
199
|
|
254
|
-
def pathname
|
255
|
-
@@appname
|
256
|
-
end
|
257
|
-
|
258
200
|
def name
|
259
201
|
File.basename(pathname)
|
260
202
|
end
|
261
203
|
|
204
|
+
def pathname
|
205
|
+
@@appname
|
206
|
+
end
|
207
|
+
|
262
208
|
def get_arg
|
263
209
|
CommandLine::OptionParser::GET_ARGS
|
264
210
|
end
|
@@ -278,7 +224,7 @@ class Application
|
|
278
224
|
$VERBOSE = verbose
|
279
225
|
end
|
280
226
|
obj = self.new
|
281
|
-
obj.
|
227
|
+
obj.__parse_command_line(argv)
|
282
228
|
obj.main
|
283
229
|
|
284
230
|
#alias :user_init :initialize
|
@@ -303,7 +249,61 @@ class Application
|
|
303
249
|
#self.class_eval %{ def main; end }
|
304
250
|
end
|
305
251
|
|
306
|
-
def
|
252
|
+
def __parse_command_line(argv)
|
253
|
+
if argv.empty? && [0,0] != @arg_arity
|
254
|
+
puts usage
|
255
|
+
exit(0)
|
256
|
+
end
|
257
|
+
|
258
|
+
@option_data = @option_parser.parse(argv)
|
259
|
+
|
260
|
+
__validate_args(@option_data.args)
|
261
|
+
@arg_names.each_with_index { |name, idx|
|
262
|
+
instance_variable_set("@#{name}", @option_data.args[idx])
|
263
|
+
}
|
264
|
+
end
|
265
|
+
|
266
|
+
def __validate_arg_arity(arity)
|
267
|
+
min, max = *arity
|
268
|
+
raise(InvalidArgumentArityError, "Minimum argument arity '#{min}' must be "+
|
269
|
+
"greater than or equal to 0.") unless min >= 0
|
270
|
+
raise(InvalidArgumentArityError, "Maximum argument arity '#{max}' must be "+
|
271
|
+
"greater than or equal to -1.") if max < -1
|
272
|
+
raise(InvalidArgumentArityError, "Maximum argument arity '#{max}' must be "+
|
273
|
+
"greater than minimum arg_arity '#{min}'.") if max < min && max != -1
|
274
|
+
end
|
275
|
+
|
276
|
+
def __init_format
|
277
|
+
#
|
278
|
+
# Formatting defaults
|
279
|
+
#
|
280
|
+
console_width = ENV["COLUMNS"]
|
281
|
+
@columns =
|
282
|
+
if console_width.nil?
|
283
|
+
DEFAULT_CONSOLE_WIDTH
|
284
|
+
elsif console_width < MIN_CONSOLE_WIDTH
|
285
|
+
console_width
|
286
|
+
else
|
287
|
+
console_width - DEFAULT_BODY_INDENT
|
288
|
+
end
|
289
|
+
@body_indent = DEFAULT_BODY_INDENT
|
290
|
+
@tag_paragraph = false
|
291
|
+
@order = :index # | :alpha
|
292
|
+
end
|
293
|
+
|
294
|
+
def __validate_args(od_args)
|
295
|
+
size = od_args.size
|
296
|
+
min, max = @arg_arity
|
297
|
+
max = 1.0/0.0 if -1 == max
|
298
|
+
raise(ArgumentError,
|
299
|
+
"Missing expected arguments. Found #{size} but expected #{min}.\n"+
|
300
|
+
"#{usage}") if size < min
|
301
|
+
raise(ArgumentError, "Too many arguments. Found #{size} but "+
|
302
|
+
"expected #{max}.\n#{usage}") if size > max
|
303
|
+
end
|
304
|
+
|
305
|
+
|
306
|
+
def __help
|
307
307
|
{
|
308
308
|
:names => %w(--help -h),
|
309
309
|
:arity => [0,0],
|
@@ -314,7 +314,7 @@ class Application
|
|
314
314
|
}
|
315
315
|
end
|
316
316
|
|
317
|
-
def
|
317
|
+
def __verbose
|
318
318
|
{
|
319
319
|
:names => %w(--verbose -v),
|
320
320
|
:arity => [0,0],
|
@@ -326,7 +326,7 @@ class Application
|
|
326
326
|
}
|
327
327
|
end
|
328
328
|
|
329
|
-
def
|
329
|
+
def __version
|
330
330
|
{
|
331
331
|
:names => %w(--version -V),
|
332
332
|
:arity => [0,0],
|
@@ -344,7 +344,7 @@ class Application
|
|
344
344
|
}
|
345
345
|
end
|
346
346
|
|
347
|
-
def
|
347
|
+
def __debug
|
348
348
|
{
|
349
349
|
:names => %w(--debug -d),
|
350
350
|
:arity => [0,0],
|
@@ -252,8 +252,8 @@ raise "Wrong data type '#{option.name}." unless Option === option
|
|
252
252
|
@unknown_options << user_option
|
253
253
|
elsif :ignore == @unknown_options_action
|
254
254
|
else
|
255
|
-
raise(UnknownOptionError, "Unknown option '#{user_option}'
|
256
|
-
"#{@opt_lookup_by_any_name.inspect}.")
|
255
|
+
raise(UnknownOptionError, "Unknown option '#{user_option}'"+
|
256
|
+
"#{$DEBUG ? ' in ' + @opt_lookup_by_any_name.keys.inspect : ''}.")
|
257
257
|
end
|
258
258
|
}
|
259
259
|
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
|
|
3
3
|
specification_version: 1
|
4
4
|
name: CommandLine
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.7.
|
7
|
-
date: 2005-07-
|
6
|
+
version: 0.7.1
|
7
|
+
date: 2005-07-14
|
8
8
|
summary: Tools to facilitate creation of command line applications and flexible parsing of command line options.
|
9
9
|
require_paths:
|
10
10
|
- lib
|