Skip to content

Commit

Permalink
* added possibility to parse global options, command and local option…
Browse files Browse the repository at this point in the history
…s separately
  • Loading branch information
gettalong committed Jul 5, 2005
1 parent 7c32582 commit 2568814
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions doc/src/index.page
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Have a look around the site!

h2. News

<b>2005-07-05 cmdparse 1.0.5 released!!!</b>

Changes:

* added possibility to parse global options, command and local options separately


<b>2005-06-16 cmdparse 1.0.4 released!!!</b>

Changes:
Expand Down
67 changes: 41 additions & 26 deletions lib/cmdparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 <tt>CommandParser#execute</tt> has to
# be called to execute the command. The optional +parse+ parameter specifies what should be
# parsed. If <tt>:global</tt> is included in the +parse+ array, global options are parsed; if
# <tt>:command</tt> is included, the command is parsed and if <tt>:local</tt> 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

Expand Down

0 comments on commit 2568814

Please sign in to comment.