Class: CommandLine::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/commandline/optionparser/option.rb

Defined Under Namespace

Classes: InvalidArgumentArityError, InvalidArgumentError, InvalidConstructionError, InvalidOptionNameError, InvalidPropertyError, MissingOptionNameError, MissingPropertyError, OptionError

Constant Summary collapse

GENERAL_OPT_EQ_ARG_RE =

:nodoc:

/^(-{1,2}[a-zA-Z]+[-_a-zA-Z0-9]*)=(.*)$/
GNU_OPT_EQ_ARG_RE =
/^(--[a-zA-Z]+[-_a-zA-Z0-9]*)=(.*)$/
POSIX_OPTION_RE =

OPTION_RE = /^-1,2([a-zA-Z]+w*)(.*)/ UNIX_OPT_EQ_ARG_RE = /^(-[a-zA-Z])=(.*)$/ UNIX_OPT_EQorSP_ARG_RE = /^(-[a-zA-Z])(=|s+)(.*)$/

/^-[a-zA-Z]?$/
NON_POSIX_OPTION_RE =

need to change this to support - and –

/^(-|-{1,2}[a-zA-Z_]+[-_a-zA-Z0-9]*)/
PROPERTIES =
[ :arg_arity, :opt_description, :arg_description,
  :opt_found, :opt_not_found, :posix
]
FLAG_BASE_OPTS =
{
  :arg_arity       => [0,0],
#    :opt_description => nil,
  :arg_description => "",
  :opt_found       => true,
  :opt_not_found   => false
}
DEFAULT_OPTS =

You get these without asking for them

{
  :arg_arity       => [1,1],
  :opt_description => "",
  :arg_description => "",
  :opt_found       => true,
  :opt_not_found   => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*all) ⇒ Option

Option.new(:posix => true, :names => %w(–opt), :flag)

TODO: Should we test and raise key is not one of :names, opt_description, … This will prevent typos. Can users add properties to an Option that are their own?



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/commandline/optionparser/option.rb', line 65

def initialize(*all)
  @posix = false

  raise(MissingPropertyError,
    "No properties specified for new #{self.class}.") if all.empty?

  until Hash === all[0]
    case (prop = all.shift)
    when :flag then @flag = true
    when :posix then @posix = true
    else 
      raise(InvalidPropertyError, "Unknown option setting '#{prop}'.")
    end
  end

  properties = all[0]

  type = @flag.nil? ? :default : :flag
  merge_hash = 
    case type
    when :flag then FLAG_BASE_OPTS
    when :default then DEFAULT_OPTS
    else raise(InvalidConstructionError, 
      "Invalid arguments to Option.new. Must be a property hash with "+
      "keys [:names, :arg_arity, :opt_description, :arg_description, "+
      ":opt_found, :opt_not_found] or "+
      "an option type [:flag, :default].")
    end

  raise(InvalidPropertyError, 
    "Don't understand argument of type '#{properties.class}' => "+
        "#{properties.inspect} passed to #{self.class}.new. Looking "+
        "for type Hash.") unless properties.kind_of?(Hash)

  if properties.has_key?(:posix) 
    @posix = properties[:posix] 
    properties.delete(:posix)
  end
  @properties = merge_hash.merge(properties)
  
  @properties[:names] = [@properties[:names]] unless 
    @properties[:names].kind_of?(Array)

  arg_arity = @properties[:arg_arity]
  @properties[:arg_arity] = [arg_arity, arg_arity] unless 
    arg_arity.kind_of?(Array)

  raise "Invalid value for arg_arity '#{arg_arity}'." unless 
    arg_arity.kind_of?(Array) || arg_arity.kind_of?(Fixnum)

  raise(InvalidArgumentArityError,
    "Conflicting value given to new option: :flag "+
    "and :arg_arity = #{properties[:arg_arity].inspect}.") if 
      :flag == type && [0,0] != @properties[:arg_arity]

  names = @properties[:names]
  raise(MissingOptionNameError, 
    "Attempt to create an Option without :names defined.") if 
    names.nil? || names.empty?

  names.each { |name| check_option_name(name) }
  validate_arity @properties[:arg_arity]

  create_opt_description if :flag == type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



170
171
172
173
174
175
# File 'lib/commandline/optionparser/option.rb', line 170

def method_missing(sym, *args)
  raise "Unknown property '#{sym}' for option 
    #{@properties[:names].inspect unless @properties[:names].nil?}." unless 
      @properties.has_key?(sym) || PROPERTIES.include?(sym)
  @properties[sym, *args]
end

Instance Attribute Details

#posixObject

Returns the value of attribute posix.



26
27
28
# File 'lib/commandline/optionparser/option.rb', line 26

def posix
  @posix
end

Instance Method Details

#check_option_name(name) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/commandline/optionparser/option.rb', line 141

def check_option_name(name)
  raise(InvalidOptionNameError, 
    "Option name '#{name}' contains invalid space.") if /\s+/.match(name)

  if @posix
    raise(InvalidOptionNameError, 
      "Option name '#{name}' is invalid.") unless POSIX_OPTION_RE.match(name)
  else
    raise(InvalidOptionNameError, 
      "Option name '#{name}' is invalid.") unless NON_POSIX_OPTION_RE.match(name)
  end
end

#create_opt_descriptionObject



131
132
133
134
135
136
137
138
139
# File 'lib/commandline/optionparser/option.rb', line 131

def create_opt_description
  return if @properties.has_key?(:opt_description)
  word = @properties[:names].grep(/^--\w.+/)
  if word.empty?
    @properties[:opt_description] = ""
  else
    @properties[:opt_description] = "Sets #{word.first[2..-1]} to true."
  end
end

#to_hashObject



177
178
179
# File 'lib/commandline/optionparser/option.rb', line 177

def to_hash
  Marshal.load(Marshal.dump(@properties))
end

#validate_arity(arity) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/commandline/optionparser/option.rb', line 154

def validate_arity(arity)
raise ":arg_arity is nil" if arity.nil?
  min, max = *arity

  raise(InvalidArgumentArityError, "Minimum argument arity '#{min}' must be "+
    "greater than or equal to 0.") unless min >= 0
  raise(InvalidArgumentArityError, "Maximum argument arity '#{max}' must be "+
    "greater than or equal to -1.") if max < -1
  raise(InvalidArgumentArityError, "Maximum argument arity '#{max}' must be "+
    "greater than minimum arg_arity '#{min}'.") if max < min && max != -1
  if @posix
    raise(InvalidArgumentArityError, "Posix options only support :arg_arity "+
      "of [0,0] or [1,1].") unless ([0,0] == arity) || ([1,1] == arity)
  end
end