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

Constant Summary

Instance Method Summary (collapse)

Constructor Details

- (OptionParserProxy) initialize(option_parser, options)

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



416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/methadone/main.rb', line 416

def initialize(option_parser,options)
  @option_parser = option_parser
  @options = options
  @user_specified_banner = false
  @accept_options = false
  @args = []
  @arg_options = {}
  @arg_documentation = {}
  @description = nil
  @version = nil
  set_banner
  document_help
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(sym, *args, &block)

Defers all calls save #on to the underlying OptionParser instance



490
491
492
# File 'lib/methadone/main.rb', line 490

def method_missing(sym,*args,&block)
  @option_parser.send(sym,*args,&block)
end

Instance Method Details

- (Object) arg(arg_name, *options)

Sets the banner to include these arg names



471
472
473
474
475
476
477
478
479
480
481
# File 'lib/methadone/main.rb', line 471

def arg(arg_name,*options)
  options << :optional if options.include?(:any) && !options.include?(:optional)
  options << :required unless options.include? :optional
  options << :one unless options.include?(:any) || options.include?(:many)
  @args << arg_name
  @arg_options[arg_name] = options
  options.select(&STRINGS_ONLY).each do |doc|
    @arg_documentation[arg_name] = doc + (options.include?(:optional) ? " (optional)" : "")
  end
  set_banner
end

- (Object) banner=(new_banner)

Proxies to underlying OptionParser



465
466
467
468
# File 'lib/methadone/main.rb', line 465

def banner=(new_banner)
  @option_parser.banner=new_banner
  @user_specified_banner = true
end

- (Object) check_args!



430
431
432
433
434
435
436
437
438
439
440
# File 'lib/methadone/main.rb', line 430

def check_args!
  ::Hash[@args.zip(::ARGV)].each do |arg_name,arg_value|
    if @arg_options[arg_name].include? :required
      if arg_value.nil?
        message = "'#{arg_name.to_s}' is required"
        message = "at least one " + message if @arg_options[arg_name].include? :many
        raise ::OptionParser::ParseError,message
      end
    end
  end
end

- (Object) description(desc)



483
484
485
486
# File 'lib/methadone/main.rb', line 483

def description(desc)
  @description = desc
  set_banner
end

- (Object) on(*args, &block)

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.



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/methadone/main.rb', line 447

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
  set_banner
end

- (Object) post_setup

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



507
508
509
510
511
512
513
514
515
516
517
# File 'lib/methadone/main.rb', line 507

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

- (Object) to_s

Since we extend Object on 1.8.x, to_s is defined and thus not proxied by method_missing



495
496
497
# File 'lib/methadone/main.rb', line 495

def to_s #::nodoc::
  @option_parser.to_s
end

- (Object) version(version)

Sets the version for the banner



500
501
502
503
# File 'lib/methadone/main.rb', line 500

def version(version)
  @version = version
  set_banner
end