diff --git a/Rakefile b/Rakefile index d9f169f..17c487c 100644 --- a/Rakefile +++ b/Rakefile @@ -130,13 +130,13 @@ else s.has_rdoc = true s.extra_rdoc_files = rd.rdoc_files.reject do |fn| fn =~ /\.rb$/ end.to_a - s.rdoc_options = ['--line-numbers', '-m README'] + s.rdoc_options = ['--line-numbers', '-m', 'README'] #### Author and project details s.author = "Thomas Leitner" s.email = "t_leitner@gmx.at" - s.homepage = "cmdparse.rubyforge.org" + s.homepage = "http://cmdparse.rubyforge.org" s.rubyforge_project = "cmdparse" end diff --git a/doc/src/index.page b/doc/src/index.page index 274257c..ed3d194 100644 --- a/doc/src/index.page +++ b/doc/src/index.page @@ -12,6 +12,13 @@ Have a look around the site! h2. News +2005-07-05 cmdparse 1.0.5 released!!! + +Changes: + +* added possibility to parse global options, command and local options separately + + 2005-06-16 cmdparse 1.0.4 released!!! Changes: diff --git a/lib/cmdparse.rb b/lib/cmdparse.rb index af853a8..4ad7eb4 100644 --- a/lib/cmdparse.rb +++ b/lib/cmdparse.rb @@ -107,7 +107,7 @@ def get_program_name class CommandParser # The version of the command parser - VERSION = [1, 0, 4] + VERSION = [1, 0, 5] # This error is thrown when an invalid command is encountered. class InvalidCommandError < OptionParser::ParseError @@ -313,6 +313,33 @@ def add_command( command, default = false ) command.init( self ) end + # Parses the global options. + def parse_global_options!( args ) + @options.order!( args ) + end + + # Parses the command. + def parse_command!( args ) + @parsed[:command] = args.shift + if @parsed[:command].nil? + if @default.nil? + raise NoCommandGivenError + else + @parsed[:command] = @default + end + else + raise InvalidCommandError.new( @parsed[:command] ) unless commands.include?( @parsed[:command] ) + end + end + + # Parses the local options. Attention: The command has to be parsed (invoke method + # +parse_command!+) before this method can be invoked. + def parse_local_options!( args ) + if @parsed[:command] + commands[@parsed[:command]].options.permute!( args ) unless commands[@parsed[:command]].options.nil? + end + end + # Calls +parse+ - implemented to mimic OptionParser def permute( args ); parse( args ); end # Calls +parse!+ - implemented to mimic OptionParser @@ -321,41 +348,29 @@ def permute!( args ); parse!( args ); end 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 # 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. If +execCommand+ - # is true, the command is executed immediately. If false, the +CommandParser#execute+ has to be - # called to execute the command. - def parse!( args, execCommand = true ) - # parse global options + # command name is analyzied and the options for the specific commands parsed. If +execCommand+ is + # true, the command is executed immediately. If false, the CommandParser#execute has to + # be called to execute the command. The optional +parse+ parameter specifies what should be + # parsed. If :global is included in the +parse+ array, global options are parsed; if + # :command is included, the command is parsed and if :local is included, the + # local options are parsed. + def parse!( args, execCommand = true, parse = [:global, :command, :local] ) begin - @options.order!( args ) - @parsed[:command] = args.shift - if @parsed[:command].nil? - if @default.nil? - raise NoCommandGivenError - else - @parsed[:command] = @default - end - else - raise InvalidCommandError.new( @parsed[:command] ) unless commands.include?( @parsed[:command] ) - end - rescue OptionParser::ParseError => e - handle_exception( e, :global ) - end + context = :global + parse_global_options!( args ) if parse.include?( :global ) + parse_command!( args ) if parse.include?( :command ) - # parse local options - begin - commands[@parsed[:command]].options.permute!( args ) unless commands[@parsed[:command]].options.nil? + context = :local + parse_local_options!( args ) if parse.include?( :local ) rescue OptionParser::ParseError => e - handle_exception( e, :local ) + handle_exception( e, context ) end @parsed[:args] = args - execute if execCommand end