cmdparse 1.0.1 → 1.0.2
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/ChangeLog +13 -0
- data/VERSION +1 -1
- data/doc/src/index.page +7 -1
- data/lib/cmdparse.rb +23 -12
- metadata +3 -3
data/ChangeLog
CHANGED
@@ -1,4 +1,17 @@
|
|
1
1
|
------------------------------------------------------------------------
|
2
|
+
r265 | thomas | 2005-04-21 12:57:51 +0200 (Thu, 21 Apr 2005) | 1 line
|
3
|
+
Changed paths:
|
4
|
+
M /trunk/cmdparse/doc/src/index.page
|
5
|
+
|
6
|
+
* updated doc
|
7
|
+
------------------------------------------------------------------------
|
8
|
+
r246 | thomas | 2005-04-13 15:36:19 +0200 (Wed, 13 Apr 2005) | 1 line
|
9
|
+
Changed paths:
|
10
|
+
M /trunk/cmdparse/lib/cmdparse.rb
|
11
|
+
M /trunk/cmdparse/test.rb
|
12
|
+
|
13
|
+
* splitted parsing of the arguments and executing the command into two methods
|
14
|
+
------------------------------------------------------------------------
|
2
15
|
r242 | thomas | 2005-04-13 13:42:24 +0200 (Wed, 13 Apr 2005) | 1 line
|
3
16
|
Changed paths:
|
4
17
|
M /trunk/cmdparse/README
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.2
|
data/doc/src/index.page
CHANGED
@@ -12,7 +12,13 @@ Have a look around the site!
|
|
12
12
|
|
13
13
|
h2. News
|
14
14
|
|
15
|
-
|
15
|
+
<b>cmdparse 1.0.2 released!!!</b>
|
16
|
+
|
17
|
+
Changes:
|
18
|
+
|
19
|
+
* splitted parsing of the arguments and executing the command into two methods
|
20
|
+
|
21
|
+
<b>cmdparse 1.0.1 released!!!</b>
|
16
22
|
|
17
23
|
Changes:
|
18
24
|
|
data/lib/cmdparse.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#
|
2
2
|
#--
|
3
3
|
#
|
4
|
-
# $Id: cmdparse.rb
|
4
|
+
# $Id: cmdparse.rb 246 2005-04-13 13:36:19Z thomas $
|
5
5
|
#
|
6
6
|
# cmdparse: an advanced command line parser using optparse which supports commands
|
7
7
|
# Copyright (C) 2004 Thomas Leitner
|
@@ -99,7 +99,7 @@ end
|
|
99
99
|
class CommandParser
|
100
100
|
|
101
101
|
# The version of the command parser
|
102
|
-
VERSION = [1, 0,
|
102
|
+
VERSION = [1, 0, 2]
|
103
103
|
|
104
104
|
# This error is thrown when an invalid command is encountered.
|
105
105
|
class InvalidCommandError < OptionParser::ParseError
|
@@ -215,7 +215,9 @@ class CommandParser
|
|
215
215
|
puts "Available commands:"
|
216
216
|
width = commandParser.commands.keys.max {|a,b| a.length <=> b.length }.length
|
217
217
|
commandParser.commands.sort.each do |name, command|
|
218
|
-
|
218
|
+
print commandParser.options.summary_indent + name.ljust( width + 4 ) + command.description
|
219
|
+
print " (default)" if name == commandParser.default
|
220
|
+
print "\n"
|
219
221
|
end
|
220
222
|
puts ""
|
221
223
|
puts commandParser.options.summarize
|
@@ -268,11 +270,13 @@ class CommandParser
|
|
268
270
|
|
269
271
|
# Holds the registered commands
|
270
272
|
attr_reader :commands
|
273
|
+
attr_reader :default
|
271
274
|
|
272
275
|
def initialize
|
273
276
|
@options = OptionParser.new
|
274
277
|
@commands = {}
|
275
278
|
@default = nil
|
279
|
+
@parsed = {}
|
276
280
|
end
|
277
281
|
|
278
282
|
# If called with a block, this method yields the global options of the +CommandParser+. If no
|
@@ -306,22 +310,29 @@ class CommandParser
|
|
306
310
|
def parse( args ); parse!( args.dup ); end
|
307
311
|
|
308
312
|
# Parses the given argument. First it tries to parse global arguments if given. After that the
|
309
|
-
# command name is analyzied and the options for the specific commands parsed.
|
310
|
-
# command is executed
|
311
|
-
|
313
|
+
# command name is analyzied and the options for the specific commands parsed. If +execCommand+
|
314
|
+
# is true, the command is executed immediately. If false, the +CommandParser#execute+ has to be
|
315
|
+
# called to execute the command.
|
316
|
+
def parse!( args, execCommand = true )
|
312
317
|
@options.order!( args )
|
313
|
-
command = args.shift
|
314
|
-
if command.nil?
|
318
|
+
@parsed[:command] = args.shift
|
319
|
+
if @parsed[:command].nil?
|
315
320
|
if @default.nil?
|
316
321
|
raise NoCommandGivenError
|
317
322
|
else
|
318
|
-
command = @default
|
323
|
+
@parsed[:command] = @default
|
319
324
|
end
|
320
325
|
else
|
321
|
-
raise InvalidCommandError.new( command ) unless commands.include?( command )
|
326
|
+
raise InvalidCommandError.new( @parsed[:command] ) unless commands.include?( @parsed[:command] )
|
322
327
|
end
|
323
|
-
commands[command].options.permute!( args ) unless commands[command].options.nil?
|
324
|
-
|
328
|
+
commands[@parsed[:command]].options.permute!( args ) unless commands[@parsed[:command]].options.nil?
|
329
|
+
@parsed[:args] = args
|
330
|
+
execute if execCommand
|
331
|
+
end
|
332
|
+
|
333
|
+
# Executes the command. The method +CommandParser#parse!+ has to be called before this one!
|
334
|
+
def execute
|
335
|
+
commands[@parsed[:command]].execute( self, @parsed[:args] ) if @parsed[:command]
|
325
336
|
end
|
326
337
|
|
327
338
|
end
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.
|
2
|
+
rubygems_version: 0.8.6
|
3
3
|
specification_version: 1
|
4
4
|
name: cmdparse
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.0.
|
7
|
-
date: 2005-04-
|
6
|
+
version: 1.0.2
|
7
|
+
date: 2005-04-21
|
8
8
|
summary: An advanced command line parser using optparse which supports commands
|
9
9
|
require_paths:
|
10
10
|
- lib
|