Class: OptionScrapper::Parser

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/optionscrapper/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#fail, #horizontal_line, #offset

Constructor Details

#initialize(name, description) ⇒ Parser

Returns a new instance of Parser.



13
14
15
16
17
18
19
20
# File 'lib/optionscrapper/parser.rb', line 13

def initialize(name,description)
  @name         = name.to_sym
  @description  = description
  @switches     = {}
  @aliases      = []
  @parser       = ::OptionParser.new
  @on_command   = Proc::new {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/optionscrapper/parser.rb', line 57

def method_missing(method, *args, &block)
  if parser.respond_to? method
    case method
      when :banner=
        parser.send method, args.first, &block
      else
        parser.send method, args, &block if args and !args.empty?
        parser.send method, &block if !args or args.empty?
    end
  else
    super(method, args, block)
  end
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



10
11
12
# File 'lib/optionscrapper/parser.rb', line 10

def aliases
  @aliases
end

#descriptionObject (readonly)

Returns the value of attribute description.



10
11
12
# File 'lib/optionscrapper/parser.rb', line 10

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/optionscrapper/parser.rb', line 10

def name
  @name
end

#on_commandObject

Returns the value of attribute on_command.



11
12
13
# File 'lib/optionscrapper/parser.rb', line 11

def on_command
  @on_command
end

#parserObject (readonly)

Returns the value of attribute parser.



10
11
12
# File 'lib/optionscrapper/parser.rb', line 10

def parser
  @parser
end

#switchesObject (readonly)

Returns the value of attribute switches.



10
11
12
# File 'lib/optionscrapper/parser.rb', line 10

def switches
  @switches
end

Instance Method Details

#alias(name) ⇒ Object Also known as: command_alias

alias: a subcommand alias



23
# File 'lib/optionscrapper/parser.rb', line 23

def alias(name); aliases << name; end

#alias?(argument) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/optionscrapper/parser.rb', line 49

def alias? argument
  aliases.include? argument
end

#on(*args) ⇒ Object

on: is the standard means of adding a command line option to the parser. The method excepts OptParser format extracts some information for meta data reasons and passes the call down the OptParser lib



30
31
32
33
34
35
36
37
38
39
# File 'lib/optionscrapper/parser.rb', line 30

def on(*args)
  # step: we use this entry point to build of a list of switches
  parse_option_switches(*args) do |option_name|
    switches[option_name] = true
  end
  # step: pass the request to the underlining gem
  parser.on(*args) do |x|
    yield x if block_given?
  end
end

#parse!(arguments) ⇒ Object



41
42
43
# File 'lib/optionscrapper/parser.rb', line 41

def parse!(arguments)
  parser.parse!(arguments)
end

#switch?(argument) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/optionscrapper/parser.rb', line 45

def switch? argument;
  switches.has_key? argument
end