lib/cmdparse.rb in cmdparse-1.0.0 vs lib/cmdparse.rb in cmdparse-1.0.1
- old
+ new
@@ -1,9 +1,9 @@
#
#--
#
-# $Id: cmdparse.rb 8 2004-09-27 16:22:39Z thomas $
+# $Id: cmdparse.rb 240 2005-04-13 11:40:39Z thomas $
#
# cmdparse: an advanced command line parser using optparse which supports commands
# Copyright (C) 2004 Thomas Leitner
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU
@@ -89,25 +89,30 @@
# opt.release = "1.0"
# opt.separator "Global options:"
# opt.on("-r", "--require TEST", "Require the TEST")
# opt.on("--delay N", Integer, "Delay test for N seconds before executing")
# end
-# cmd.add_command TestCommand.new
+# cmd.add_command TestCommand.new, true # sets this command as default command
# cmd.add_command CommandParser::HelpCommand.new
# cmd.add_command CommandParser::VersionCommand.new
# cmd.parse!( ARGV )
#
class CommandParser
# The version of the command parser
- VERSION = [1, 0, 0]
+ VERSION = [1, 0, 1]
# This error is thrown when an invalid command is encountered.
- class InvalidCommand < OptionParser::ParseError
- const_set(:Reason, 'invalid command'.freeze)
+ class InvalidCommandError < OptionParser::ParseError
+ const_set( :Reason, 'invalid command'.freeze )
end
+ # This error is thrown when no command was given and no default command was specified.
+ class NoCommandGivenError < OptionParser::ParseError
+ const_set( :Reason, 'no command given'.freeze )
+ end
+
# Base class for the commands. This class implements all needed methods so that it can be used by
# the +OptionParser+ class.
class Command
# The name of the command
@@ -169,12 +174,12 @@
super( 'help' )
end
def init( commandParser )
commandParser.options do |opt|
- opt.on_tail( "-h", "--help", "Show help" ) do
- execute( commandParser, [] )
+ opt.on_tail( "-h", "--help [command]", "Show help" ) do |cmd|
+ execute( commandParser, cmd.nil? ? [] : [cmd] )
end
end
end
def description
@@ -260,15 +265,16 @@
end
end
# Holds the registered commands
- attr_reader :commands
+ attr_reader :commands
def initialize
@options = OptionParser.new
@commands = {}
+ @default = nil
end
# If called with a block, this method yields the global options of the +CommandParser+. If no
# block is specified, it returns the global options.
def options # :yields: options
@@ -277,27 +283,44 @@
else
@options
end
end
- # Adds a command to the command list.
- def add_command( command )
+ # Adds a command to the command list. If the optional parameter +default+ is true, then this
+ # command is used when no command is specified on the command line.
+ def add_command( command, default = false )
@commands[command.name] = command
+ @default = command.name if default
command.init( self )
end
+ # Calls +parse+ - implemented to mimic OptionParser
+ def permute( args ); parse( args ); end
+ # Calls +parse!+ - implemented to mimic OptionParser
+ def permute!( args ); parse!( args ); end
+ # Calls +parse+ - implemented to mimic OptionParser
+ def order( args ); parse( args ); end
+ # Calls +parse!+ - implemented to mimic OptionParser
+ def order!( args ); parse!( args ); end
+
# see CommandParser#parse!
- def parse( args )
- parse!( args.dup )
- end
+ def parse( args ); parse!( args.dup ); end
# Parses the given argument. First it tries to parse global arguments if given. After that the
# command name is analyzied and the options for the specific commands parsed. After that the
# command is executed by invoking its +execute+ method.
def parse!( args )
@options.order!( args )
- command = args.shift || 'no command given'
- raise InvalidCommand.new( command ) unless commands.include?( command )
+ command = args.shift
+ if command.nil?
+ if @default.nil?
+ raise NoCommandGivenError
+ else
+ command = @default
+ end
+ else
+ raise InvalidCommandError.new( command ) unless commands.include?( command )
+ end
commands[command].options.permute!( args ) unless commands[command].options.nil?
commands[command].execute( self, args )
end
end