Class: OptionScrapper::OptionsParser
- Inherits:
-
Object
- Object
- OptionScrapper::OptionsParser
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.
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 = {}
create_parser(GLOBAL_PARSER,'the global parser')
@cursor.parser.program_name = prog_name
@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
#parsers ⇒ Object
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
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)
batch_arguments(arguments) do |batch|
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)
unless name == GLOBAL_PARSER
puts parsers[parser_name].print_usage
else
newline
puts global_parser.parser
newline
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|
next if parser.name == GLOBAL_PARSER
next if parser.switches.empty?
puts parser.parser
newline
end
end
end
fail message if message
newline
exit 0
end
|