Class: OptionScrapper::OptionsParser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/optionscrapper/optionsparser.rb

Constant Summary collapse

GLOBAL_PARSER =
:global
GLOBAL_OPTION_REGEX =
/^(-[-]?[[:alpha:]-]+)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#fail, #horizontal_line, #offset

Constructor Details

#initialize {|_self| ... } ⇒ OptionsParser

Returns a new instance of OptionsParser.

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



100
101
102
103
104
105
106
107
# 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

Instance Attribute Details

#parsersObject

Returns the value of attribute parsers.



22
23
24
# File 'lib/optionscrapper/optionsparser.rb', line 22

def parsers
  @parsers
end

Instance Method Details

#banner=(value) ⇒ Object



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

def banner=(value)
  @cursor.parser.banner = offset << value
end

#command(name, description) {|parser| ... } ⇒ Object

Yields:

  • (parser)


39
40
41
42
43
# File 'lib/optionscrapper/optionsparser.rb', line 39

def command(name, description)
  parser = create_parser(name,description)
  yield parser if block_given?
  parser
end

#on_command(&block) ⇒ Object



59
60
61
# File 'lib/optionscrapper/optionsparser.rb', line 59

def on_command &block
  @cursor.on_command = block
end

#parse!(arguments = ARGV) ⇒ Object



49
50
51
52
53
54
55
56
57
# 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

#usage(message = nil, name = GLOBAL_PARSER) ⇒ Object Also known as: print_usage



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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