cmdparse 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,16 +1,48 @@
1
1
  ------------------------------------------------------------------------
2
- r398 | thomas | 2006-04-05 16:13:43 +0200 (Wed, 05 Apr 2006) | 1 line
3
- Changed paths:
4
- A /versions/cmdparse-2.0.1 (from /trunk/cmdparse:340)
5
- R /versions/cmdparse-2.0.1/Rakefile (from /trunk/cmdparse/Rakefile:396)
6
- R /versions/cmdparse-2.0.1/TODO (from /trunk/cmdparse/TODO:396)
7
- R /versions/cmdparse-2.0.1/doc/src/default.template (from /trunk/cmdparse/doc/src/default.template:396)
8
- D /versions/cmdparse-2.0.1/doc/src/documentation.page
9
- R /versions/cmdparse-2.0.1/doc/src/index.page (from /trunk/cmdparse/doc/src/index.page:396)
10
- A /versions/cmdparse-2.0.1/doc/src/tutorial.page (from /trunk/cmdparse/doc/src/tutorial.page:397)
11
- R /versions/cmdparse-2.0.1/lib/cmdparse.rb (from /trunk/cmdparse/lib/cmdparse.rb:396)
12
-
13
- * created version 2.0.1 of cmdparse
2
+ r417 | thomas | 2006-06-17 21:35:12 +0200 (Sat, 17 Jun 2006) | 1 line
3
+ Changed paths:
4
+ M /trunk/cmdparse/doc/src/index.page
5
+
6
+ * fixed spelling error
7
+ ------------------------------------------------------------------------
8
+ r416 | thomas | 2006-06-17 21:32:55 +0200 (Sat, 17 Jun 2006) | 7 lines
9
+ Changed paths:
10
+ A /trunk/cmdparse/.emacs.desktop
11
+ M /trunk/cmdparse/Rakefile
12
+ M /trunk/cmdparse/doc/src/index.page
13
+ M /trunk/cmdparse/doc/src/tutorial.page
14
+ M /trunk/cmdparse/lib/cmdparse.rb
15
+ M /trunk/cmdparse/net.rb
16
+ A /trunk/cmdparse/test
17
+ A /trunk/cmdparse/test/tc_commandhash.rb
18
+
19
+ * included patches #4756 and #4729 from Assaph Mehr and slightly modified them
20
+ * added the test file for CommandHash from Assaph Mehr
21
+ * updated Rakefile to run test the test file
22
+ * updated the tutorial to include information about partial command matching
23
+ * minor update to index page
24
+ * update sampe net.rb script to use partial command matching
25
+ * added emacs desktop file
26
+ ------------------------------------------------------------------------
27
+ r397 | thomas | 2006-04-05 16:12:46 +0200 (Wed, 05 Apr 2006) | 1 line
28
+ Changed paths:
29
+ M /trunk/cmdparse/doc/src/tutorial.page
30
+
31
+ * small changes
32
+ ------------------------------------------------------------------------
33
+ r396 | thomas | 2006-04-04 17:43:24 +0200 (Tue, 04 Apr 2006) | 3 lines
34
+ Changed paths:
35
+ M /trunk/cmdparse/Rakefile
36
+ M /trunk/cmdparse/TODO
37
+ M /trunk/cmdparse/doc/src/default.template
38
+ D /trunk/cmdparse/doc/src/documentation.page
39
+ M /trunk/cmdparse/doc/src/index.page
40
+ A /trunk/cmdparse/doc/src/tutorial.page (from /trunk/cmdparse/doc/src/documentation.page:340)
41
+ M /trunk/cmdparse/lib/cmdparse.rb
42
+
43
+ * bumped version number to 2.0.1
44
+ * fixed bug with GemSpec reported by Marcus Werne: lib/cmdparse.rb get automatically loaded now
45
+ * split documentation page into main docu page and tutorial page
14
46
  ------------------------------------------------------------------------
15
47
  r332 | thomas | 2005-08-16 10:54:01 +0200 (Tue, 16 Aug 2005) | 1 line
16
48
  Changed paths:
data/Rakefile CHANGED
@@ -44,7 +44,7 @@ SRC_RB = FileList['lib/**/*.rb']
44
44
  # The default task is run if rake is given no explicit arguments.
45
45
 
46
46
  desc "Default Task"
47
- task :default => :doc
47
+ task :default => :test
48
48
 
49
49
 
50
50
  # End user tasks ################################################################
@@ -67,6 +67,9 @@ task :clean do
67
67
  ruby "setup.rb clean"
68
68
  end
69
69
 
70
+ task :test do
71
+ ruby "-Ilib test/tc_commandhash.rb"
72
+ end
70
73
 
71
74
  CLOBBER << "doc/output"
72
75
  desc "Builds the documentation"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.1
1
+ 2.0.2
@@ -12,6 +12,14 @@ Have a look around the site!
12
12
 
13
13
  h2. News
14
14
 
15
+ <b>2006-06-17 cmdparse 2.0.2 released!!!</b>
16
+
17
+ Changes:
18
+
19
+ * Included two patches from Assaph Mehr:
20
+ * partial command matching can now be used (see <a href="{relocatable: tutorial.page}">tutorial page</a>)
21
+ * now a banner for the help and version commands can be specified
22
+
15
23
  <b>2006-04-05 cmdparse 2.0.1 released!!!</b>
16
24
 
17
25
  Changes:
@@ -65,9 +65,12 @@ Create a new new file and write the necessary @require@ statements.
65
65
  h3. The @CommandParser@ class
66
66
 
67
67
  Next we will define our basic @CommandParser@ by defining the name of the program, its version and
68
- the global options. The boolean argument to the constructor of the @CommandParser@ class defines
69
- whether exceptions should be handled gracefully, i.e. by showing an appropriate error message and
70
- the help screen.
68
+ the global options. The first boolean argument to the constructor of the @CommandParser@ class
69
+ defines whether exceptions should be handled gracefully, i.e. by showing an appropriate error
70
+ message and the help screen. The second boolean argument defines whether the top level commands
71
+ should use partial command matching instead of full command matching. If partial command matching is
72
+ used, then the shortest unambiguous part of a command name can be used instead of always specifing
73
+ the full command name.
71
74
 
72
75
  <notextile>{extract: {file: ../net.rb, lines: !ruby/range 30..36}}</notextile>
73
76
 
@@ -97,10 +100,10 @@ However, we want the program to do something "useful". Therefore we define a new
97
100
 
98
101
  <notextile>{extract: {file: ../net.rb, lines: !ruby/range 41..44}}</notextile>
99
102
 
100
- This command is defined by using the default @Command@ class. First an instance is created
101
- assigning a name to the command and defining whether this command takes subcommands. Next we add a
102
- short description so that the @help@ command can produce something useful. And at last, we add this
103
- command to our @CommandParser@ instance.
103
+ This command is defined by using the default @Command@ class. First an instance is created assigning
104
+ a name to the command and defining whether this command takes subcommands and uses partial command
105
+ matching. Next we add a short description so that the @help@ command can produce something useful.
106
+ And at last, we add this command to our @CommandParser@ instance.
104
107
 
105
108
  We specified that our @ipaddr@ command takes subcommands. So we have to define them, too:
106
109
 
@@ -158,6 +161,15 @@ Why? As the @ipaddr@ command takes subcommands there should be an additional com
158
161
  @list@) on the command line. However, as the @list@ command is the default command for @ipaddr@ you
159
162
  do not need to type it.
160
163
 
164
+ *By the way:* You get the same output if you type
165
+ <pre>
166
+ $ ruby net.rb ip
167
+ </pre>
168
+
169
+ Why? As partial command matching is used for the top level commands, the shortest unambiguous name
170
+ for a command can be used. As there is no other command starting with @ip@ (or even with the letter
171
+ @i@), it is sufficient to write the above to select the @ipaddr@ command.
172
+
161
173
  *Notice:* The options of a command which does not take subcommands do not need to be at the front;
162
174
  they can be anywhere, like this
163
175
  <pre>
@@ -1,7 +1,7 @@
1
1
  #
2
2
  #--
3
3
  #
4
- # $Id: cmdparse.rb 398 2006-04-05 14:13:43Z thomas $
4
+ # $Id: cmdparse.rb 416 2006-06-17 19:32:55Z thomas $
5
5
  #
6
6
  # cmdparse: advanced command line parser supporting commands
7
7
  # Copyright (C) 2004 Thomas Leitner
@@ -25,7 +25,7 @@
25
25
  module CmdParse
26
26
 
27
27
  # The version of this cmdparse implemention
28
- VERSION = [2, 0, 1]
28
+ VERSION = [2, 0, 2]
29
29
 
30
30
 
31
31
  # Base class for all cmdparse errors.
@@ -103,6 +103,18 @@ module CmdParse
103
103
  # Require default option parser wrapper
104
104
  require 'cmdparse/wrappers/optparse'
105
105
 
106
+ # Command Hash - will return partial key matches as well if there is a single
107
+ # non-ambigous matching key
108
+ class CommandHash < Hash
109
+
110
+ def []( cmd_name )
111
+ super or begin
112
+ possible = keys.select {|key| key =~ /^#{cmd_name}.*/ }
113
+ fetch( possible[0] ) if possible.size == 1
114
+ end
115
+ end
116
+
117
+ end
106
118
 
107
119
  # Base class for the commands. This class implements all needed methods so that it can be used by
108
120
  # the +CommandParser+ class.
@@ -131,13 +143,21 @@ module CmdParse
131
143
  attr_reader :commands
132
144
 
133
145
  # Initializes the command called +name+. The parameter +has_commands+ specifies if this command
134
- # takes other commands as argument.
135
- def initialize( name, has_commands )
146
+ # takes other commands as argument. The optional argument +partial_commands+ specifies, if
147
+ # partial command matching should be used.
148
+ def initialize( name, has_commands, partial_commands = false )
136
149
  @name = name
137
150
  @options = ParserWrapper.new
138
151
  @has_commands = has_commands
139
- @commands = {}
152
+ @commands = Hash.new
140
153
  @default_command = nil
154
+ use_partial_commands( partial_commands )
155
+ end
156
+
157
+ def use_partial_commands( use_partial )
158
+ temp = ( use_partial ? CommandHash.new : Hash.new )
159
+ temp.update( @commands )
160
+ @commands = temp
141
161
  end
142
162
 
143
163
  # Returns +true+ if this command supports sub commands.
@@ -272,7 +292,7 @@ module CmdParse
272
292
  if args.length > 0
273
293
  cmd = commandparser.main_command
274
294
  arg = args.shift
275
- while !arg.nil? && cmd.commands.keys.include?( arg )
295
+ while !arg.nil? && cmd.commands[ arg ]
276
296
  cmd = cmd.commands[arg]
277
297
  arg = args.shift
278
298
  end
@@ -292,6 +312,7 @@ module CmdParse
292
312
  #######
293
313
 
294
314
  def show_program_help
315
+ puts commandparser.banner + "\n" if commandparser.banner
295
316
  puts "Usage: #{commandparser.program_name} [options] COMMAND [options] [COMMAND [options] ...] [args]"
296
317
  puts ""
297
318
  list_commands( 1, commandparser.main_command )
@@ -331,6 +352,7 @@ module CmdParse
331
352
  def execute( args )
332
353
  version = commandparser.program_version
333
354
  version = version.join( '.' ) if version.instance_of?( Array )
355
+ puts commandparser.banner + "\n" if commandparser.banner
334
356
  puts version
335
357
  exit
336
358
  end
@@ -341,6 +363,9 @@ module CmdParse
341
363
  # The main class for creating a command based CLI program.
342
364
  class CommandParser
343
365
 
366
+ # A standard banner for help & version screens
367
+ attr_accessor :banner
368
+
344
369
  # The top level command representing the program itself.
345
370
  attr_reader :main_command
346
371
 
@@ -354,10 +379,12 @@ module CmdParse
354
379
  attr_reader :handle_exceptions
355
380
 
356
381
  # Create a new CommandParser object. The optional argument +handleExceptions+ specifies if the
357
- # object should handle exceptions gracefully.
358
- def initialize( handleExceptions = false )
382
+ # object should handle exceptions gracefully. Set +partial_commands+ to +true+, if you want
383
+ # partial command matching for the top level commands.
384
+ def initialize( handleExceptions = false, partial_commands = false )
359
385
  @main_command = Command.new( 'mainCommand', true )
360
386
  @main_command.super_command = self
387
+ @main_command.use_partial_commands( partial_commands )
361
388
  @program_name = $0
362
389
  @program_version = "0.0.0"
363
390
  @handle_exceptions = handleExceptions
@@ -402,7 +429,7 @@ module CmdParse
402
429
  cmdName = command.default_command
403
430
  end
404
431
  else
405
- raise InvalidCommandError.new( cmdName ) unless command.commands.include?( cmdName )
432
+ raise InvalidCommandError.new( cmdName ) unless command.commands[ cmdName ]
406
433
  end
407
434
 
408
435
  command = command.commands[cmdName]
@@ -1,7 +1,7 @@
1
1
  #
2
2
  #--
3
3
  #
4
- # $Id: optparse.rb 398 2006-04-05 14:13:43Z thomas $
4
+ # $Id: optparse.rb 329 2005-08-14 15:39:05Z thomas $
5
5
  #
6
6
  # cmdparse: advanced command line parser supporting commands
7
7
  # Copyright (C) 2004 Thomas Leitner
data/net.rb CHANGED
@@ -27,7 +27,7 @@ class NetStatCommand < CmdParse::Command
27
27
 
28
28
  end
29
29
 
30
- cmd = CmdParse::CommandParser.new( true )
30
+ cmd = CmdParse::CommandParser.new( true, true )
31
31
  cmd.program_name = "net"
32
32
  cmd.program_version = [0, 1, 1]
33
33
  cmd.options = CmdParse::OptionParserWrapper.new do |opt|
@@ -39,7 +39,7 @@ cmd.add_command( CmdParse::VersionCommand.new )
39
39
  cmd.add_command( NetStatCommand.new )
40
40
 
41
41
  # ipaddr
42
- ipaddr = CmdParse::Command.new( 'ipaddr', true )
42
+ ipaddr = CmdParse::Command.new( 'ipaddr', true, true )
43
43
  ipaddr.short_desc = "Manage IP addresses"
44
44
  cmd.add_command( ipaddr )
45
45
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: cmdparse
5
5
  version: !ruby/object:Gem::Version
6
- version: 2.0.1
7
- date: 2006-04-05 00:00:00 +02:00
6
+ version: 2.0.2
7
+ date: 2006-06-17 00:00:00 +02:00
8
8
  summary: Advanced command line parser supporting commands
9
9
  require_paths:
10
10
  - lib