1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
module Clamp
module Option
module Parsing
protected
def parse_options
set_options_from_command_line
default_options_from_environment
verify_required_options_are_set
end
private
def set_options_from_command_line
while remaining_arguments.first && remaining_arguments.first.start_with?("-")
switch = remaining_arguments.shift
break if switch == "--"
case switch
when /\A(-\w)(.+)\z/m # combined short options
switch = Regexp.last_match(1)
if find_option(switch).flag?
remaining_arguments.unshift("-" + Regexp.last_match(2))
else
remaining_arguments.unshift(Regexp.last_match(2))
end
when /\A(--[^=]+)=(.*)\z/m
switch = Regexp.last_match(1)
remaining_arguments.unshift(Regexp.last_match(2))
end
option = find_option(switch)
value = option.extract_value(switch, remaining_arguments)
begin
option.of(self).take(value)
rescue ArgumentError => e
signal_usage_error Clamp.message(:option_argument_error, :switch => switch, :message => e.message)
end
end
end
def default_options_from_environment
self.class.recognised_options.each do |option|
option.of(self).default_from_environment
end
end
def verify_required_options_are_set
self.class.recognised_options.each do |option|
# If this option is required and the value is nil, there's an error.
next unless option.required? && send(option.attribute_name).nil?
if option.environment_variable
message = Clamp.message(:option_or_env_required,
:option => option.switches.first,
:env => option.environment_variable)
else
message = Clamp.message(:option_required,
:option => option.switches.first)
end
signal_usage_error message
end
end
def find_option(switch)
self.class.find_option(switch) ||
signal_usage_error(Clamp.message(:unrecognised_option, :switch => switch))
end
end
end
end
|