Class: MxxRu::CmdLineOptionProcessor
- Inherits:
-
Object
- Object
- MxxRu::CmdLineOptionProcessor
- Includes:
- Singleton
- Defined in:
- lib/mxx_ru/cmd_line_option_processor.rb
Overview
Singleton for centralized mechanizm of command line options parsing.
Since v.1.4.0
Each part of Mxx_ru which need some command line option must provider special option processor for CmdLineOptionProcessor singleton:
module MxxRu
module SomeLangSupport
class OptionParser
include Singleton
...
def prepare( parser ) # Instance of OptionParser from stdlib.
parser.separator ''
parser.separator 'Some Language Support options:'
parser.on( '-i', '--include PATH', 'Set up include path' ) do
|path|
... some processig ...
end
... setting another option handlers ...
end
end
end
end
# OptionParser must be added to CmdLineOptionProcessor.
MxxRu::CmdLineOptionProcessor.add_processor(
MxxRu::SomeLangSupport::OptionParser.instance )
See examples of using this technique in MxxRu::Util::Mode and MxxRu::Cpp::Mode classes.
Instance Method Summary collapse
-
#add_processor(processor) ⇒ Object
Add yet another option processor for processors list.
-
#initialize ⇒ CmdLineOptionProcessor
constructor
A new instance of CmdLineOptionProcessor.
-
#parse ⇒ Object
Performs parsing of ARGV content by current processors.
Constructor Details
#initialize ⇒ CmdLineOptionProcessor
Returns a new instance of CmdLineOptionProcessor.
72 73 74 75 |
# File 'lib/mxx_ru/cmd_line_option_processor.rb', line 72 def initialize @processors = [] @parsed = false end |
Instance Method Details
#add_processor(processor) ⇒ Object
Add yet another option processor for processors list.
78 79 80 81 |
# File 'lib/mxx_ru/cmd_line_option_processor.rb', line 78 def add_processor( processor ) @processors << processor @parsed = false end |
#parse ⇒ Object
Performs parsing of ARGV content by current processors.
Finishes work of script if –help or –version found in ARGV.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mxx_ru/cmd_line_option_processor.rb', line 86 def parse unless @parsed parser = OptionParser.new do |p| prepare_option_parser( p ) end begin parser.parse( ARGV ) rescue => x $stderr.puts "command line parsing error '#{x.}'" exit( 2 ) end @parsed = true end end |