class OptionScrapper::OptionsParser

Constants

GLOBAL_OPTION_REGEX
GLOBAL_PARSER

Attributes

parsers[RW]

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 25
def initialize
  @cursor  = nil
  @parsers = {}
  # step: create the global parser
  create_parser(GLOBAL_PARSER,'the global parser')
  @cursor.parser.program_name = prog_name
  # step: inject a default help options for global
  @cursor.parser.on_tail( '-h', '--help', 'display this usage menu' ) do
    puts print_usage
    exit 0
  end
  yield self if block_given?
end

Public Instance Methods

banner=(value) click to toggle source
command(name, description) { |parser| ... } click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 39
def command(name, description)
  parser = create_parser(name,description)
  yield parser if block_given?
  parser
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/optionscrapper/optionsparser.rb, line 100
def method_missing(method, *args, &block)
  if @cursor.parser.respond_to? method
    @cursor.parser.send method, args, &block if args and !args.empty?
    @cursor.parser.send method, &block if !args or args.empty?
  else
    super(method, args, block)
  end
end
on_command(&block) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 59
def on_command &block
  @cursor.on_command = block
end
parse!(arguments = ARGV) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 49
def parse!(arguments = ARGV)
  # step: we need to separate into subcommand arguments
  batch_arguments(arguments) do |batch|
    # step: iterate the batches and fire off the parsers for each subcommand
    batch.batches do |name,options|
      parsers[name].parse! options
    end
  end
end
print_usage(message = nil, name = GLOBAL_PARSER)
Alias for: usage
usage(message = nil, name = GLOBAL_PARSER) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 63
def usage(message = nil, name = GLOBAL_PARSER)
  # step: if the parser is specified, print only that one
  unless name == GLOBAL_PARSER
    puts parsers[parser_name].print_usage
  else
    # step: else we generate the full parse usage menu
    newline
    puts global_parser.parser
    newline
    # step: we don't need to do this if there are no sub commands
    if parsers.size > 1
      puts offset << "commands : #{horizontal_line(62,'-')}"
      parsers.values.each do |parser|
        next if parser.name == GLOBAL_PARSER
        command_line = parser.name.to_s
        command_line << '(%s)' % [ parser.aliases.join(',') ] unless parser.aliases.empty?
        puts offset  << '%-32s %s' % [ command_line, parser.description ]
      end
      puts offset << horizontal_line(72,'-')
      newline
      parsers.values.each do |parser|
        # step: skip the global, we have already displayed it
        next if parser.name == GLOBAL_PARSER
        # step: we don't need to show this if the subcommand has no options / switches
        next if parser.switches.empty?
        # step: else we can show the parser usage
        puts parser.parser
        newline
      end
    end
  end
  fail message if message
  newline
  exit 0
end
Also aliased as: print_usage

Private Instance Methods

alias?(name) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 174
def alias?(name)
  name = symbolize!(name)
  parsers.values.each do |parser|
    next if parser.name == GLOBAL_PARSER
    next if parser.aliases.empty?
    return true if parser.aliases.include? name
  end
  false
end
batch_arguments(arguments) { |batch| ... } click to toggle source

batch: takes the command line options, iterates the options and places them into the correct batch

# File lib/optionscrapper/optionsparser.rb, line 125
def batch_arguments(arguments)
  batch = Batch.new do |x|
    arguments.each do |argument|
      # step: is the argument a subcommand?
      if subcommand?(argument)
        parser = subcommand(argument)
        # step: get the command and set the cursor to the parser
        x.cursor = parser.name
        # step: call the on_command block
        parser.on_command.call
      else
        # step: is this argument a parser argument
        parser = subcommand(x.cursor)
        if !is_switch?(argument)
          if x.previous
            x.global(argument)
          else
            x.add argument
          end
        elsif !parser.switch?(argument) and global_parser.switch?(argument)
          x.global(argument)
          x.previous = x.cursor
        else
          x.add argument
        end
      end
    end
  end
  yield batch if block_given?
  batch
end
create_parser(name,description) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 110
def create_parser(name,description)
  # step: create a new parser and add to hash collection
  parsers[name] = Parser.new(name,description)
  # step: update the cursor to the newly created parser
  @cursor = parsers[name]
  # step: create a usage for this command
  @cursor.parser.banner = "    #{name} : description: #{description}"
  @cursor.parser.separator "    #{horizontal_line(72)}"
  @cursor.parser.separator ''
  # step: return the parser
  parsers[name]
end
global_parser() click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 188
def global_parser
  subcommand(GLOBAL_PARSER)
end
is_switch?(argument) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 184
def is_switch?(argument)
  argument =~ GLOBAL_OPTION_REGEX
end
prog_name() click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 192
def prog_name
  File.basename($0)
end
subcommand(name) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 162
def subcommand(name)
  name = symbolize!(name)
  raise StandardError, "subcommand: parser: #{name} does not exists" unless subcommand? name
  unless parsers[name]
    # step: it must be an alias
    name = parsers.values.select { |parser|
      parser.aliases.include? name
    }.first.name
  end
  parsers[name]
end
subcommand?(name) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 157
def subcommand?(name)
  name = symbolize!(name)
  return ( parsers.has_key?(name) or alias?(name) ) ? true : false
end
symbolize!(name) click to toggle source
# File lib/optionscrapper/optionsparser.rb, line 196
def symbolize!(name)
  ( name.is_a? Symbol ) ? name : name.to_sym
end