Class: Nagi::OptionParser
- Inherits:
-
OptionParser
- Object
- OptionParser
- Nagi::OptionParser
- Defined in:
- lib/nagi/optionparser.rb
Instance Method Summary collapse
- #argument(name) ⇒ Object
-
#initialize(*args, &block) ⇒ OptionParser
constructor
A new instance of OptionParser.
- #parse!(args) ⇒ Object
- #switch(name, *args) ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ OptionParser
Returns a new instance of OptionParser.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/nagi/optionparser.rb', line 4 def initialize(*args, &block) @arguments = [] @options = {} super(*args, &block) self. = "Usage: #{$0} [options]" self.on_tail('-h', '--help', 'Display this help message') do puts help exit 0 end self.on_tail('-V', '--version', 'Display version information') do puts "#{program_name} #{version or "(unknown version)"}".strip exit 0 end end |
Instance Method Details
#argument(name) ⇒ Object
23 24 25 26 |
# File 'lib/nagi/optionparser.rb', line 23 def argument(name) @arguments.push(name) self. += " <#{name}>" end |
#parse!(args) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/nagi/optionparser.rb', line 28 def parse!(args) begin @options.clear super(args) @arguments.each do |a| @options[a] = args.shift or raise ArgumentError.new("Argument '#{a}' not given") end raise ArgumentError.new("Too many arguments") if args.length > 0 rescue ::OptionParser::ParseError => e raise ArgumentError.new(e.) end return @options end |
#switch(name, *args) ⇒ Object
42 43 44 45 46 |
# File 'lib/nagi/optionparser.rb', line 42 def switch(name, *args) on(*args) do |value| @options[name] = value end end |