Class: Methadone::OptionParserProxy
- Inherits:
- BasicObject
- Defined in:
- lib/methadone/main.rb
Overview
Methadone Internal - treat as private
A proxy to OptionParser that intercepts #on so that we can allow a simpler interface
Instance Method Summary collapse
-
#arg(arg_name, *options) ⇒ Object
Sets the banner to include these arg names.
-
#banner=(new_banner) ⇒ Object
Proxies to underlying OptionParser.
- #check_args! ⇒ Object
- #description(desc) ⇒ Object
-
#initialize(option_parser, options) ⇒ OptionParserProxy
constructor
Create the proxy.
-
#method_missing(sym, *args, &block) ⇒ Object
Defers all calls save #on to the underlying OptionParser instance.
-
#on(*args, &block) ⇒ Object
If invoked as with OptionParser, behaves the exact same way.
-
#post_setup ⇒ Object
We need some documentation to appear at the end, after all OptionParser setup has occured, but before we actually start.
-
#to_s ⇒ Object
Since we extend Object on 1.8.x, to_s is defined and thus not proxied by method_missing.
-
#version(version) ⇒ Object
Sets the version for the banner.
Constructor Details
#initialize(option_parser, options) ⇒ OptionParserProxy
Create the proxy
option_parser
-
An OptionParser instance
options
-
a hash that will store the options set via automatic setting. The caller should retain a reference to this
420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/methadone/main.rb', line 420 def initialize(option_parser,) @option_parser = option_parser @options = @user_specified_banner = false @accept_options = false @args = [] @arg_options = {} @arg_documentation = {} @description = nil @version = nil document_help end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
Defers all calls save #on to the underlying OptionParser instance
494 495 496 |
# File 'lib/methadone/main.rb', line 494 def method_missing(sym,*args,&block) @option_parser.send(sym,*args,&block) end |
Instance Method Details
#arg(arg_name, *options) ⇒ Object
Sets the banner to include these arg names
475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/methadone/main.rb', line 475 def arg(arg_name,*) << :optional if .include?(:any) && !.include?(:optional) << :required unless .include? :optional << :one unless .include?(:any) || .include?(:many) @args << arg_name @arg_options[arg_name] = .select(&STRINGS_ONLY).each do |doc| @arg_documentation[arg_name] = doc + (.include?(:optional) ? " (optional)" : "") end end |
#banner=(new_banner) ⇒ Object
Proxies to underlying OptionParser
469 470 471 472 |
# File 'lib/methadone/main.rb', line 469 def () @option_parser.= @user_specified_banner = true end |
#check_args! ⇒ Object
434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/methadone/main.rb', line 434 def check_args! ::Hash[@args.zip(::ARGV)].each do |arg_name,arg_value| if @arg_options[arg_name].include? :required if arg_value.nil? = "'#{arg_name.to_s}' is required" = "at least one " + if @arg_options[arg_name].include? :many raise ::OptionParser::ParseError, end end end end |
#description(desc) ⇒ Object
487 488 489 490 |
# File 'lib/methadone/main.rb', line 487 def description(desc) @description = desc end |
#on(*args, &block) ⇒ Object
If invoked as with OptionParser, behaves the exact same way. If invoked without a block, however, the options hash given to the constructor will be used to store the parsed command-line value. See #opts in the Main module for how that works.
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/methadone/main.rb', line 451 def on(*args,&block) @accept_options = true args = add_default_value_to_docstring(*args) if block @option_parser.on(*args,&block) else opt_names = option_names(*args) @option_parser.on(*args) do |value| opt_names.each do |name| @options[name] = value @options[name.to_s] = value end end end end |
#post_setup ⇒ Object
We need some documentation to appear at the end, after all OptionParser setup has occured, but before we actually start. This method serves that purpose
511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/methadone/main.rb', line 511 def post_setup unless @arg_documentation.empty? @option_parser.separator '' @option_parser.separator "Arguments:" @option_parser.separator '' @args.each do |arg| @option_parser.separator " #{arg}" @option_parser.separator " #{@arg_documentation[arg]}" end end end |
#to_s ⇒ Object
Since we extend Object on 1.8.x, to_s is defined and thus not proxied by method_missing
499 500 501 |
# File 'lib/methadone/main.rb', line 499 def to_s #::nodoc:: @option_parser.to_s end |
#version(version) ⇒ Object
Sets the version for the banner
504 505 506 507 |
# File 'lib/methadone/main.rb', line 504 def version(version) @version = version end |