Class: Pione::Command::OptionDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/pione/command/option.rb

Overview

OptionDefinition is a class for holding option definitions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionDefinition

Creata a command option context.



47
48
49
50
51
52
53
# File 'lib/pione/command/option.rb', line 47

def initialize
  extend OptionInterface
  @items = []      # option item definitions
  @default = {}    # default value table
  @validators = [] # option validators
  @parser_mode = :parse!
end

Instance Attribute Details

#parser_modeObject

Returns the value of attribute parser_mode.



44
45
46
# File 'lib/pione/command/option.rb', line 44

def parser_mode
  @parser_mode
end

Instance Method Details

#default(name, value) ⇒ Object

Define the default value.



88
89
90
# File 'lib/pione/command/option.rb', line 88

def default(name, value)
  @default[name] = value
end

#item(name) ⇒ Object



55
56
57
# File 'lib/pione/command/option.rb', line 55

def item(name)
  @items.find {|item| item.name == name}
end

#parse(argv, command_name, command_banner) ⇒ Object

Parse the command options.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pione/command/option.rb', line 60

def parse(argv, command_name, command_banner)
  data = Hash.new

  # parse options
  OptionParser.new do |opt|
    # set banner
    opt.banner = "Usage: %s [options]" % command_name
    opt.banner << "\n\n" + command_banner + "\n" if command_banner

    # set version
    opt.program_name = command_name
    opt.version = Pione::VERSION

    # default values
    @items.each {|item| data[item.name] = item.default if item.default}
    data.merge!(@default)

    # setup option parser
    @items.sort{|a,b| a.long <=> b.long}.each {|item| setup_item(command_name, opt, data, item)}
  end.send(@parser_mode, argv)

  # check option's validness
  check(data)

  return data
end

#validate(&b) ⇒ Object



92
93
94
# File 'lib/pione/command/option.rb', line 92

def validate(&b)
  @validators << b
end